标签:
原题链接在这里:https://leetcode.com/problems/move-zeroes/
设置一个count = 0, 每当遇到非零数,就赋值到nums[count]上,同时count++.
最后count到nums.length位都填0.
Time O(n). Space O(1).
AC Java:
1 public class Solution { 2 public void moveZeroes(int[] nums) { 3 if(nums == null || nums.length == 0){ 4 return; 5 } 6 int count = 0; 7 for(int i = 0; i<nums.length; i++){ 8 if(nums[i] != 0){ 9 nums[count++] = nums[i]; 10 } 11 } 12 while(count < nums.length){ 13 nums[count++] = 0; 14 } 15 } 16 }
标签:
原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/4834590.html