Beats 97.21% of C++ solutions


  • 0
    I
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            if (nums.empty()) {
                return 0;
            }
            int current = 0;
            int offset = 0;
            while (offset < nums.size()) {
                while (offset + 1 < nums.size() and nums.at(offset + 1) == nums.at(offset)) {
                    offset ++;
                }
                offset ++;
                current ++;
                if (offset < nums.size()) {
                    nums.at(current) = nums.at(offset);
                }
            }
            return current;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.