首页 > 其他分享 >LeetCode 283 Move Zeroes

LeetCode 283 Move Zeroes

时间:2022-11-03 16:34:09浏览次数:52  
标签:Zeroes nums Move Solution LeetCode 283 array

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Solution

点击查看代码
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int i=0,j=1;
        while(j<nums.size()){
            if(nums[i]==0 && nums[j]!=0){
                swap(nums[i], nums[j]);
                i++; j++;
            }
            else if(nums[i]==0 && nums[j]==0)j++;
            else{
                i++;j++;
            }
        }
    }
};

标签:Zeroes,nums,Move,Solution,LeetCode,283,array
From: https://www.cnblogs.com/xinyu04/p/16854916.html

相关文章