283. Move Zeroes

非常不要脸地做一个easy,是fb面经= =……那个人人品也是可以

和remove duplicates from sorted array道理是一样的,用i对整个数组走一遍,然后用index保持有效的位置。最后把index及之后的都填成0就可以了

 

 1     public void moveZeroes(int[] nums) {
 2         if(nums.length == 0) {
 3             return;
 4         }
 5         int index = 0;
 6         int n = nums.length;
 7         for(int i = 0; i < n; i++) {
 8             if(nums[i] != 0) {
 9                 nums[index++] = nums[i];
10             }
11         }
12         for(int i = index; i < n; i++) {
13             nums[i] = 0;
14         }
15     }

 

posted @ 2016-10-26 06:47  warmland  阅读(102)  评论(0编辑  收藏  举报