欢迎关注我的leetcode习题解答集,不断完善中,希望可以带给你帮助,共同进步
leetcode习题解答集
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
count = len(nums)
for i in range(count):
for j in range(count-1-i):
if (nums[j]==0 and nums[j+1] != 0):
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp
超时了
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
nums.sort(key=lambda x: 1 if x == 0 else 0)
需要注意的是,key只是声明比较什么,具体怎么比需要重写cmp参数,但是默认的cmp就是 cmp()函数,每种类型都重定义了自己的cmp()函数