码迷,mamicode.com
首页 > 移动开发 > 详细

力扣题解 283th 移动零

时间:2020-07-03 21:11:39      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:int   就会   i++   lan   block   move   指示器   tin   ++   

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 移动零

标签:int   就会   i++   lan   block   move   指示器   tin   ++   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!