remove_duplicates

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

remove_duplicates.cpp 源码

// 删除排序数组中的重复项

#include "iostream"
#include "vector"

using namespace std;

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.size() == 0) {
            return 0;
        }
        int i = 0, j = 0;
        while (j < nums.size()) {
            if (nums[i] != nums[j]) {
                nums[i + 1] = nums[j];
                i++;
            }
            j++;
        }

        return i + 1;
    }
};

你可能感兴趣的文章

contains_duplicate

container_with_most_water

contains_duplicate_ii

0  赞