283. Move Zeroes – Two pointer / Array

https://leetcode.com/problems/move-zeroes/

Haoyang’s Solution: 24.1% 1ms

public class Solution {

public int moveZeroes(int[] nums) {

if (nums == null || nums.length == 0) {

return 0;

}

int start = 0;

int end = 0;

while (end < nums.length) {

if (nums[end] == 0) {

end++;

}

else {

nums[start] = nums[end];

start++;

end++;

}

}

if (end == nums.length) {

end--;

}

while (start <= end && end < nums.length) {

nums[start] = 0;

start++;

}

return start + 1;

}

}

https://discuss.leetcode.com/topic/25380/java-solution-of-move-zeroes/2

Discuss Solution: 思路一样,for 循环代替while

results matching ""

    No results matching ""