smart solution
class Solution { public void moveZeroes(int[] nums) { int zeroIndex = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != 0) { if (i > zeroIndex) { // so smart, ๅฆๆๆฒๆ, ๅฐๆผ็ญๆผ็้ฝๆ่ฎ0 nums[zeroIndex] = nums[i]; nums[i] = 0; } zeroIndex++; } } } }
normal solution
if (nums[i] != 0) { // ๅฆๆๆฏ้ๆจฃ็่ณๆ, Input: [1,0,0,3,12], swap ๅฎ, 1้ๆฏๅๅไฝ็ฝฎ swap(nums, zeroIndex, i); zeroIndex++; }
Last updated 4 years ago