[leetcode-2] Add Two Numbers
ยท
๐Ÿข One step
LeetCode-2 Add Two Numbers : Add Two Numbers in Linked List note : You may assume the two numbers do not contain any leading zero, except the number 0 itself.myAnswer :::python #Definition for singly-linked list. class ListNode: def init(self, val=0, next=None): self.val = val self.next = next class Solution: def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[Lis..
[leetcode-206] Reverse Linked List
ยท
๐Ÿข One step
LeetCode-206 Reverse Linked List : Reverse Linked List note : ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ :::python # Definition for singly-linked list. # class ListNode: # def init(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: def reverse(node: ListNode, prev: ListNode = None): if not node: return prev next, node.next = no..
[leetcode-21] Merge Two Sorted Lists
ยท
๐Ÿข One step
LeetCode-21 Merge Two Sorted Lists : Merge Two lists note : recursive :::python # Definition for singly-linked list. # class ListNode: # def init(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: if (not list1) or (list2 and list1.val > list2.val): list1, list2 = list2,..
[leetcode 234] Palindrom Linked List
ยท
๐Ÿข One step
LeetCode-234 Palindrome Linked List : ์ฃผ์–ด์ง„ head๊ฐ€ palindrome์ธ์ง€ ์•„๋‹Œ์ง€ note : range : 1~10^5MyAnswer :::python # Definition for singly-linked list. # class ListNode: # def init(self, val=0, next=None): # self.val = val # self.next = next class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: temp: List = [] if not head: return True node = head while node is not None: temp.append(nod..
[leetcode-121] Best Time to Buy and Sell Stock
ยท
๐Ÿข One step
LeetCode-121 Best Time to Buy and Sell Stock : ์‹ธ๊ฒŒ ์‚ฌ์„œ ๋น„์‹ธ๊ฒŒ ํŒ”์•„๋ผ! note : max(profit)myAnswer :::python class Solution: def maxProfit(self, prices: List[int]) -> int: len_prices = len(prices) if len_prices
[leetcode-238] Product of Array Except Self
ยท
๐Ÿข One step
LeetCode-238 Product of Array Except Self : Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. note : Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)Answer :::python class Solution: def productExceptSelf(self..
[leetcode-561] Array Partition
ยท
๐Ÿข One step
LeetCode-561 Array Partition : find maximum (minimum list) note : len(list) = 2nmyAnswer :::python class Solution: def arrayPairSum(self, nums: List[int]) -> int: nums.sort() return sum(nums[::2]) Result : 267ms Memory: 16.7mb
[leetcode-15] Three Sum
ยท
๐Ÿข One step
LeetCode-15 Match Three sum : Notice that the solution set must not contain duplicate triplets. note : myAnswerMyAnswer(Time Limit Exceeded) O(n^2) :::python class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: a = [] nums.sort() for i, n in enumerate(nums): d = {} for j in range(i+1,len(nums)): complete = - (n + nums[j]) if complete in d: if sorted([n, nums[j], complete]) not..
๋‹คํ–ˆ๋‹ค