LeetCode-26. 删除排序数组中的重复项(Remove Duplicates from Sorted Array)

双指针

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

题目链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

发布了42 篇原创文章 · 获赞 2 · 访问量 1411

猜你喜欢

转载自blog.csdn.net/Listen_heart/article/details/103108907
今日推荐