Leetcode 283 移动零 双指针
C:
void moveZeroes(int* nums, int numsSize){ int left = 0,right=0; while(right<numsSize){ if(nums[right]!=0){ int temp = nums[right]; nums[right] = nums[left]; nums[left] = temp; left++; } right++; } }
JAVA:
public final void moveZeroes(int[] nums) { int leftPoint = 0, rightPoint = 0, len = nums.length; while (rightPoint < len) { if (nums[rightPoint] != 0) { if (leftPoint != rightPoint) swap(nums, rightPoint, leftPoint); leftPoint++; } rightPoint++; } } private final void swap(int[] nums, int point0, int point1) { int temp = nums[point0]; nums[point0] = nums[point1]; nums[point1] = temp; }
JS:
/** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */ var moveZeroes = function (nums) { let left = 0, right = 0, len = nums.length; while (right < len) { if (nums[right]) { let temp = nums[right]; nums[right] = nums[left]; nums[left] = temp; left++ } right++; } };
当你看清人们的真相,于是你知道了,你可以忍受孤独