26. 删除排序数组中的重复项

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

  1. class Solution:
  2. def removeDuplicates(self, nums: List[int]) -> int:
  3. if len(nums) < 2:
  4. return len(nums)
  5. j = 0
  6. for i in range(1,len(nums)):
  7. if nums[i]!=nums[i-1]:
  8. nums[j]=nums[i-1]
  9. j+=1
  10. nums[j] = nums[-1]
  11. j+=1
  12. return j

88. 合并两个有序数组

https://leetcode-cn.com/problems/merge-sorted-array/
我自己的解法, 代码有点冗长,但这道题逻辑很简单,是双指针入门题

  1. class Solution:
  2. def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
  3. """
  4. Do not return anything, modify nums1 in-place instead.
  5. """
  6. if n == 0:
  7. return nums1
  8. if m == 0:
  9. nums1[:]=nums2[:]
  10. return nums1
  11. if n == 0 and m == 0:
  12. return []
  13. i = m-1
  14. j = n-1
  15. point = -1
  16. while i>=0 and j>=0:
  17. if nums1[i] > nums2[j]:
  18. nums1[point] = nums1[i]
  19. i-=1
  20. else:
  21. nums1[point] = nums2[j]
  22. j-=1
  23. point-=1
  24. # 无需判断i>=0,因为如果i>=0,说明所有nums2的元素都已经按顺序放进了nums1,而nums1原本就是有序的
  25. while j>=0:
  26. nums1[point] = nums2[j]
  27. j-=1
  28. point-=1
  29. return nums1

167. 两数之和 II - 输入有序数组

https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/
two points最简单的入门题

  1. class Solution:
  2. def twoSum(self, numbers: List[int], target: int) -> List[int]:
  3. start = 0
  4. end =len(numbers)-1
  5. while start < end:
  6. if numbers[start] + numbers[end] == target:
  7. return [start+1,end+1]
  8. elif numbers[start] + numbers[end] > target:
  9. end-=1
  10. else:
  11. start+=1

283. 移动零

https://leetcode-cn.com/problems/move-zeroes/
一个point挨个遍历数组,一个point表示要把不为零的数往前挪的位置

  1. class Solution:
  2. def moveZeroes(self, nums: List[int]) -> None:
  3. """
  4. Do not return anything, modify nums in-place instead.
  5. """
  6. if len(nums) < 2:
  7. return nums
  8. point = 0
  9. for i in range(len(nums)):
  10. if nums[i]!=0:
  11. nums[i], nums[point] = nums[point], nums[i]
  12. point+=1
  13. return nums

633. 平方数之和

https://leetcode-cn.com/problems/sum-of-square-numbers/
数学上是费曼大定理,算法上用two points就可以很高效的完成

  1. from math import sqrt
  2. class Solution:
  3. def judgeSquareSum(self, c: int) -> bool:
  4. start = 0
  5. end = int(sqrt(c))
  6. while start <= end:
  7. if start*start + end*end > c:
  8. end-=1
  9. elif start*start + end*end < c:
  10. start+=1
  11. else:
  12. return True
  13. return False

665. 非递减数列

https://leetcode-cn.com/problems/non-decreasing-array/
这道题可以用two points来判断,主要考虑的点是:

  1. 是否有波谷
  2. 不能有2个及以上波谷
  3. 如果波谷发生在0 or len-1 处,没问题
  4. 如果波谷发生的中间, 需要考虑波谷两元素及其一前一后一共四个元素的关系

    1. class Solution:
    2. def checkPossibility(self, nums: List[int]) -> bool:
    3. if len(nums)<3:
    4. return True
    5. length = len(nums)
    6. left, right = 0, length-1
    7. while left<length-1 and nums[left]<=nums[left+1]:
    8. left+=1
    9. if left==length-1:
    10. return True
    11. while right>0 and nums[right]>=nums[right-1]:
    12. right-=1
    13. if right-left > 1:
    14. return False
    15. if left==0 or right==length-1:
    16. return True
    17. if nums[right+1]>=nums[left] or nums[left-1]<=nums[right]:
    18. return True
    19. else:
    20. return False

    74. 搜索二维矩阵

    https://leetcode-cn.com/problems/search-a-2d-matrix/
    这道题的关键就是从左下角开始遍历,根据target的大小决定是i-=1 or j+=1

    75. 颜色分类

    https://leetcode-cn.com/problems/sort-colors/