283. Move Zeroes

xiaoxiao2021-02-27  211

欢迎关注我的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)

关于sort和sorted的参数讲解

需要注意的是,key只是声明比较什么,具体怎么比需要重写cmp参数,但是默认的cmp就是 cmp()函数,每种类型都重定义了自己的cmp()函数

转载请注明原文地址: https://www.6miu.com/read-11521.html

最新回复(0)