寶寶刷 leetcode
阿新 • • 發佈:2018-12-03
12/3
1、Two Sum
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ dict = {} for i in range(len(nums)): j = target - nums[i] if j in dict: return [dict[j],i] else: dict[nums[i]] = i #return 0
2. Add Two Numbers
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
class Solution: def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ ans= 0 unit = 1 while l1 or l2: if l1: ans += l1.val * unit l1 = l1.next if l2: ans += l2.val * unit l2 = l2.next unit *= 10 alpha = cur = ListNode(0) for n in reversed(str(ans)): cur.next= ListNode(int(n)) cur = cur.next return alpha.next
補充:連結串列是由一些節點構成的,這些節點之間由指標連線,形成了一個鏈式結構。最基本的連結串列節點只需要儲存當前節點的值,和一個指向下一節點的指標。由這種只儲存下一節點地址的連結串列節點構成的連結串列被稱為單向連結串列。
在節點ListNode定義中,定義為節點為結構變數。節點儲存了兩個變數:value 和 next。value 是這個節點的值,next 是指向下一節點的指標,當 next 為空指標時,這個節點是連結串列的最後一個節點。建構函式包含兩個引數 _value 和 _next ,分別用來給節點賦值和指定下一節點。
struct ListNode { int val; //定義val變數值,儲存節點值 struct ListNode *next; //定義next指標,指向下一個節點,維持節點連線 }
203. Remove Linked List Elements
Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5
class Solution: def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ if head == None: return head dummy = ListNode(0) dummy.next = head pre = dummy while head: if head.val == val: pre.next = head.next head = pre pre = head head = head.next return dummy.next
206. Reverse Linked List
Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
class Solution: def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ tail = None cur = head while cur: next_node = cur.next cur.next = tail tail = cur cur = next_node return tail
26. Remove Duplicates from Sorted Array
Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length =5
, with the first five elements ofnums
being modified to0
,1
,2
,3
, and4
respectively. It doesn't matter what values are set beyond the returned length.
class Solution: def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ cnt=0 if (len(nums)==0): return cnt for i in sorted(set(nums)): nums[cnt]=i cnt+=1 return cnt
27. Remove Element
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length =5
, with the first five elements ofnums
containing0
,1
,3
,0
, and 4.
class Solution: def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ index = 0 while(index < len(nums)): if (nums[index] == val): nums.pop(nums.index(val)) else: index += 1
88. Merge Sorted Array
Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
class Solution: def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m: int :type nums2: List[int] :type n: int :rtype: void Do not return anything, modify nums1 in-place instead. """ for v in nums2: nums1[m] = v m+=1 nums1.sort()