# 方法一: defmoveZeroes1(nums): iflen(nums) == 0: return nums k = 0 lst1 = [] for i in nums: if i == 0: k += 1 lst1.append(i) for item in lst1: nums.append(0) k -= 1 nums.remove(item) return nums
# 方法二: 双指针 defmoveZeroes2(nums): iflen(nums) == 0: return nums left = 0 right = 1 n = len(nums) while right < n: if nums[left] == 0and nums[right] != 0: nums[left],nums[right] = nums[right],nums[left] right += 1 left += 1 elif nums[left] == 0and nums[right] == 0: right += 1 elif nums[left] != 0: left += 1 right += 1 return nums
# 方法一: deftwoSum(nums,target): for i inrange(len(nums)): for j inrange(i + 1,len(nums)): if nums[i] + nums[j] == target: return [i+1,j+1] # 方法二: deftwoSum2(nums,target): for i inrange(len(nums)): b = target - nums[i] if b in nums: returnsorted([i+1,nums.index(b)+1]) # 方法三: deftwoSum3(nums,target): for i inrange(len(nums)): b = target - nums[i] if b > nums[i]: left = i + 1 right = len(nums) - 1 while left <= right: mid = (left + right) // 2 if b < nums[mid]: right = mid - 1 elif b > nums[mid]: left = mid + 1 else: return [i+1,mid+1] elif b < nums[i]: left = 0 right = i - 1 if right >= 0: while left <= right: mid = (left + right) // 2 if b < nums[mid]: right = mid - 1 elif b > nums[mid]: left = mid + 1 else: return [i+1,mid+1] else: return [i+1,i+1+1]
# 方法四: deftwoSum4(nums,target): i,j=0,len(nums) - 1 while i < j: if nums[i] + nums[j] < target: i += 1 elif nums[i] + nums[j] > target: j -= 1 else: return [i+1,j+1]