码迷,mamicode.com
首页 > 移动开发 > 详细

[LeetCode]数组——移动零

时间:2018-08-04 21:03:48      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:div   amp   for   相对   cto   col   个数   顺序   tco   

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

C++

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        for (int i = 0, j = 0; i < nums.size(); i++) {
            if (nums[i]) {
                swap(nums[i], nums[j++]);
            }
        }
    }
};

C

void moveZeroes(int* nums, int numsSize) {
    int t,i,j=0;
    for (i = 0; i < numsSize; i++) {
        if (nums[i]) {
            t = nums[j];
            nums[j++]=nums[i];
            nums[i]=t;
        }
    }
}

 

[LeetCode]数组——移动零

标签:div   amp   for   相对   cto   col   个数   顺序   tco   

原文地址:https://www.cnblogs.com/moonpie-sun/p/9419868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!