【LeetCode026】Remove Duplicates from Sorted Array

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int len = nums.size();
        if(len == 0)
            return 0;
        if(len == 1)
            return 1;
        int neww = 0;
        for(int old = 1; old < len; ++old){
            if(nums[neww] != nums[old]){
                ++neww;
                nums[neww] = nums[old];
            }
        }
        return ++neww;
    }
};

一定记得要判断len是不是0或者1

猜你喜欢

转载自blog.csdn.net/weixin_39458342/article/details/87105356