[LeetCode]80. Remove Duplicates from Sorted Array II删除数组中的重复值

和第一题不同的地方是,容忍两次重复

虽然题目上说只需要长度,但是否检测的时候如果数组不跟着改变也是不行的

没说清楚题意

自己是用双指针做的,看了大神的答案更简单

public int removeDuplicates(int[] nums) {
        int i = 0;
        for (int n : nums)
            if (i < 2 || n > nums[i-2])
                nums[i++] = n;
        return i;
}

原文地址:https://www.cnblogs.com/stAr-1/p/8431373.html

时间: 2024-10-12 14:43:10

[LeetCode]80. Remove Duplicates from Sorted Array II删除数组中的重复值的相关文章

[LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 这道题是之前那道Remove Duplicates from Sorted Array 有序数组中

leetCode 80.Remove Duplicates from Sorted Array II (删除排序数组中的重复II) 解题思路和方法

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this

LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

leetcode 80 Remove Duplicates from Sorted Array II ----- java

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

[leetcode] 80 Remove Duplicates from Sorted Array II(数组下标操作)

因为这道题目的题意是要求我们在原数组上进行操作,所以操作变得稍微复杂了些,否则直接使用map最为简单. 基本思想是记录两个指针,一个是当前数组,另一个是目的数组,注意如果发现重复数超过2,那么目的数组的cur就要阻塞, 直到不同的出现后再赋值前进. class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0) return 0; int cur=1; //修改后数组的下标点 i

leetcode[80] Remove Duplicates from Sorted Array II

给定一个排好序的数组,要求里面数字重复的次数不超过2,并且记录在原数组的前头,返回剩余长度.例如给定: A = [1,1,1,2,2,3] 返回 5,并且A = [1,1,2,2,3] 思路: 用till记录满足条件的下一个位置,以便下一次填入 用repeat记录重复的次数,超过2则不理,否则往till里记录 如果n为0,则返回0,如果非空,那么第一个数肯定是符合的,所以till从1开始,repeat从1开始. class Solution { public: int removeDuplica