leetcode #26 in cpp

来源:互联网 发布:软件开发流程图软件 编辑:程序博客网 时间:2024/04/30 22:24

The question is to remove the duplicate within a sorted array and return the number of unique elements in the array.

Solution: keep a count of the unique numbers we have seen. The ith unique element should be saved in the (i-1)th entry in the array. 

Code:

class Solution {public:    int removeDuplicates(vector<int>& nums) {        if(nums.empty()) return 0;        int count = 1;        for(int i = 1; i < nums.size(); i ++){            if(nums[i-1] != nums[i]) {//determine if nums[i] is duplicate                nums[count] = nums[i];                count++;            }        }        return count;    }};


0 0
原创粉丝点击