move_zeros

  • 2022-12-14
  • 浏览 (491)

move_zeros.cpp 源码

#include "vector"
#include "iostream"
using namespace std;

class Solution {

public:
    // 移动零
    void moveZeroes1(vector<int>& nums) {
        int i = 0, j = 0;
        for (; j < nums.size(); j++)
        {
            if (nums[j] != 0)
            {
                int temp = nums[j];
                nums[j] = nums[i];
                nums[i] = temp;
                i++;
            }
        }
    }

    // 先将不为零的位置填充,剩下的就是0
    void moveZeroes2(vector<int>& nums) {
        int start = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] != 0) {
                nums[start++] = nums[i];
            }
        }

        for (; start < nums.size(); start++) {
            nums[start] = 0;
        }
    }
};

你可能感兴趣的文章

contains_duplicate

container_with_most_water

contains_duplicate_ii

0  赞