Leetcode刷题记录-20181017
38. Count and Say
递归算法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution: 2 def handleN(self,l): 3 key = '' 4 tag = 0 5 result = '' 6 7 key = l[0] 8 for i in l: 9 if i == key: 10 tag +=1 11 else: 12 result +=str(tag)+key 13 key = i 14 tag = 1 15 result +=str(tag)+key 16 return result 17 18 def countAndSay(self, n): 19 """ 20 :type n: int 21 :rtype: str 22 """ 23 if n == 1: 24 return '1' 25 else: 26 return self.handleN(self.countAndSay(n-1))
66. Plus One
设定标记Tag,表明低位数字是否进1;
反向遍历digits,依次判断元素与1之和是否为10;如为10, 对元素进行置零处理,并标记Tag=1
最后判断Tag是否为1,为1表明digits[0]数字需要进1,更新数组输出。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution: def judge(self,n,tag): if tag: n +=1 def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ m = len(digits) tag = 1 for i in range(m-1,-1,-1): digits[i] +=tag tag = 0 if digits[i]==10: tag = 1 digits[i] = 0 if tag == 1: return [1] + digits return digits
67. Add Binary
return str(bin(int(a,2)+int(b,2)))[2:]
69. Sqrt(x)
牛顿迭代法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution: 2 def mySqrt(self, x): 3 """ 4 :type x: int 5 :rtype: int 6 """ 7 y = 1 8 while abs(y*y -x) > 0.000001: 9 y = 0.5*(x/y+y) 10 return int(y)
53. Maximum Subarray
动态规划方法、Kadane Algorithm算法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution: 2 def maxSubArray(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 sums = nums[0] 8 local = sums 9 for i in nums[1:]: 10 sums = sums + i 11 local = max(sums,local) 12 if sums < i: 13 sums = i 14 else: 15 pass 16 local = max(sums,local) 17 return local
递归思想, O(n) = O(n-1)+O(n-2)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution: 2 def climbStairs(self, n): 3 """ 4 :type n: int 5 :rtype: int 6 """ 7 a = 1 8 b = 2 9 if n ==1: 10 return a 11 if n == 2: 12 return b 13 else: 14 for i in range(2,n): 15 a,b = b,a+b 16 return b
83. Remove Duplicates from Sorted List
依次判断每个元素与前一个元素是否相等,相等则删除节点,不相等向后判断
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution: 8 def deleteDuplicates(self, head): 9 """ 10 :type head: ListNode 11 :rtype: ListNode 12 """ 13 14 l = ListNode(0) 15 l = head 16 m = l 17 if head == None or head.next == None : 18 return head 19 else: 20 aft = head.next 21 while aft != None: 22 if aft.val == l.val: 23 l.next = aft.next 24 aft = l.next 25 else: 26 l = aft 27 aft = aft.next 28 return m
双指针,从最后元素开始依次向前判断两个数组,大者插到nums1的末尾,指针向前
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution: 2 def merge(self, nums1, m, nums2, n): 3 """ 4 :type nums1: List[int] 5 :type m: int 6 :type nums2: List[int] 7 :type n: int 8 :rtype: void Do not return anything, modify nums1 in-place instead. 9 """ 10 i = m 11 j = n 12 while i!= 0 and j!=0: 13 if nums1[i-1] > nums2[j-1]: 14 nums1[i+j-1] = nums1[i-1] 15 i -=1 16 else: 17 nums1[i+j-1] = nums2[j-1] 18 j -=1 19 while j!=0: 20 nums1[i+j-1]=nums2[j-1] 21 j -=1