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()