leetCode 283 移动零

leetCode 283 移动零_第1张图片 

//right指向待处理序列的首部
//left指向处理后序列的尾部

class Solution {
    public void swap(int[] nums, int left, int right){
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        int left = 0;
        int right = 0;
        while(right < n){
            if(nums[right] != 0){
                swap(nums, left, right);
                left++;

            }
            right++;
        }
    }

}

你可能感兴趣的:(算法,leetcode,java)