给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
def moveZeroes(self,nums:list[int]):
if not nums:return 0
j = 0
for i in xrange(len(nums)):
if nums[i]:
nums[j] = nums[i]
j += 1
for i in xrange(j,len(nums)):
nums[i] = 0
return nums
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
def twoSum(self,nums:list[int],target:int):
hashitable = {}
for i,num in enumerate(nums):
if target - num in hashitable:
return [hashitable[target - num],i]
hashitable[num] = i
return []