LeetCode OJ 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 1122 and 3. It doesn‘t matter what you leave beyond the new length.



【题目分析】

在前一个题目的基础上,这个题目稍微做了变化。上一个题目中每一个数字只能出现一次,在这个题目中每一个数字允许出现两次。



【思路】

回忆一下,在Remove Duplicates from Sorted Array这个题目中,一个巧妙的方法就是设置了两个下标 i 和 j,i 用来遍历数组,j 用来指示数组中不重复元素最后一个元素的位置。那么同样在这个题目中我们也设置这样两个变量,指示此时的 j 用来指示数组中重复次数不超过2的元素最后的位置。我们怎么知道一个元素重复出现的次数呢?难道要设置一个变量来记录当前元素重复出现了多少次吗?一个很巧妙的办法就是比较当前元素nums[i] 是否和前前一个元素 nums[j-1] 相同,如果不相同则nums[++j] = nums[i],否则的话当前元素出项次数肯定是大于2次了,继续向后遍历数组即可。

这个过程如下:

  • j = 1; i = 2;

  • num[i] 等于 nums[j -1]; i++;

  • nums[i] 不等于 nums[j -1]; nums[++j] = nums[i]; i++;

  • num[i] 不等于 num[j -1]; nums[++j] = nums[i]; i++;

  • num[i] 等于 num[j -1] ; i++;

  • num[i] 不等于 num[j -1]; nums[++j] = nums[i];



【java代码】

 1 public class Solution {
 2     public int removeDuplicates(int[] nums) {
 3         if (nums.length <= 2)
 4             return nums.length;
 5         int j = 1;
 6         for(int i=2; i<nums.length; i++) {
 7         if (nums[j-1] != nums[i])
 8             nums[++j] = nums[i];
 9         }
10         return j+1;
11     }
12 }
时间: 2024-10-16 03:44:01

LeetCode OJ Remove Duplicates from Sorted Array II的相关文章

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][JavaScript]Remove Duplicates from Sorted Array II

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 n

[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

[C++]LeetCode: 72 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 【 Remove Duplicates from Sorted Array II 】python 实现

题目: 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]. 代码:oj测试通过 Runtime: 120 ms 1 class Solution: 2

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

Java for LeetCode 080 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'

leetcode No80. Remove Duplicates from Sorted Array II

Question: 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.