美文网首页
leetcode的每日一题更新(move Zeroes)

leetcode的每日一题更新(move Zeroes)

作者: 今天是晴天 | 来源:发表于2017-05-12 16:11 被阅读0次

题目:给一个包含0的数组,调用函数后该数组的0全部放在最后,前面的数字顺序不变。
思路:刚开始思路是对的但是想不全逻辑,怎么都做不出来,其实就是就是将非0的数字往前移,记录0的个数,在最后将0添加在最后。
代码:

if(nums==null || nums.length==0)return;
        int position=0;
        for(int num:nums){
            if(num!=0)nums[position++]=num;
        }
        while(position<nums.length){
            nums[position++]=0;
        }

相关文章

网友评论

      本文标题:leetcode的每日一题更新(move Zeroes)

      本文链接:https://www.haomeiwen.com/subject/xtttxxtx.html