目录
🍕题目
🍔思路:
1)双指针,移动0,原地移动
2)快指针遍历,慢指针被替换移动,相当于0替换到最后
🍟代码
class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ n = len(nums) slow = fast = 0 while fast < n: if nums[fast] != 0: nums[slow], nums[fast] = nums[fast], nums[slow] slow += 1 fast += 1
🍕题目
🍔思路
暴力遍历
先定义双指针,因为是非递减顺序排列,向后走数值越大,分三种情况讨论
🍟代码
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: p = 0 q = len(numbers) - 1 while p != q: if target < numbers[p] +numbers[q]: q -=1 elif target > numbers[p] +numbers[q]: p += 1 else: return [p+1, q+1]