首页 > 其他 > 详细

283. Move Zeroes

时间:2016-10-26 09:18:46      阅读:108      评论:0      收藏:0      [点我收藏+]

非常不要脸地做一个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     }

 

283. Move Zeroes

原文:http://www.cnblogs.com/warmland/p/5998969.html

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