Tuesday, December 27, 2016

Leetcode/F家Bloomberg -- 283. Move Zeroes (Two Ptr)

283. Move Zeroes (Two Ptr)
  • Difficulty: Easy
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

思路:定义一个 pos变量,要找到第一个插入0的位置。
loop array , 如果非0,overwrite nums[pos] = nums[i] , pos++
第二次loop将[pos-end]补0
关键字: insert position

public class Solution {
    public void moveZeroes(int[] nums) {
        if (nums == null || nums.length == 0) return;        
        int pos = 0;//the place to start insert 0
        for(int i =0; i<nums.length;i++){//reverse 0 at star: for(int i = nums.length-1;i > 0; i--)
            if(nums[i]!=0){
                nums[pos] = nums[i];
                pos++;//increment position only if non-zero
            }
        }
        for(int i=pos;i<nums.length;i++){
            nums[i]=0;
        }
    }
} 

No comments:

Post a Comment