python
1.如果要用到数组的值和下标:
1.1找出数组中大于0的值的下标
return [x+1 for x in range(len(nums)) if nums[x]>0]
return [i+1 for i,num in enumerate(nums) if num>0] #改进后
1.2找出数组中top5的值的下标
res2=[x[0] for x in sorted(enumerate(resl), key=lambda x: x[1])[-5:]]
计算只出现一次的数字之和
注意搞清楚 items. 和enumerate的区别
return sum(i for i,j in Counter(nums).items() if j == 1)
合并两个数组成最大数
[5,6,1]. [3,4,0] —>[5,6,3,4,1,0]
def merge(A, B):
ans = []
while A or B:
bigger = A if A > B else B
ans.append(bigger.pop(0))
return ans