leetcode 283. 移动零

leetcode 283. 移动零,第1张

leetcode 283. 移动零

java:

简单题重拳出击 

class Solution {
    public void moveZeroes(int[] nums) {
        int j = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] != 0)
                nums[j++] = nums[i];
        }
        while(j < nums.length){
            nums[j++] = 0;
        }
    }
}

python3:

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        i = 0
        for num in nums:
            if num != 0 :
                nums[i] = num
                i += 1
        while i < len(nums):
            nums[i] = 0
            i += 1

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5671441.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存