首页 > 移动平台 > 详细

力扣题解 283th 移动零

时间:2020-07-03 21:43:42      阅读:65      评论:0      收藏:0      [点我收藏+]

283th 移动零

  • 位置指示器法

    我们将cnt看作位置指示器,易于发现规律:某个不为0的元素前面有几个0(cnt),他就会向前移动cnt个位置。

    class Solution {
        public void moveZeroes(int[] nums) {
            int cnt = 0;
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] == 0) {
                    cnt++;
                    continue;
                }
                int t = nums[i];
                nums[i] = nums[i - cnt];
                nums[i - cnt] = t;
            }
        }
    }
    

力扣题解 283th 移动零

原文:https://www.cnblogs.com/fromneptune/p/13232518.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!