其他分享
首页 > 其他分享> > Leetcode 283 移动零 双指针

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++;
    }
};

 

标签:right,nums,int,void,rightPoint,283,left,Leetcode,指针
来源: https://www.cnblogs.com/niuyourou/p/15981063.html