No.80 Remove Duplicates from Sorted Array ||

Remove Duplicates from Sorted Array||

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.

Tags: Array Two Pointers

移除有序数组中出现次数超过两次的数字
要求:原地移除,不能使用额外空间;返回新数组的长度

 1 #include "stdafx.h"
 2 #include <map>
 3 #include <vector>
 4 #include <iostream>
 5 using namespace std;
 6
 7 class Solution
 8 {
 9 public:
10     int removeDuplicates(vector<int> &nums)
11     {//移除有序数组中出现次数超过两次的数字
12      //要求:原地移除,不能使用额外空间;返回新数组的长度
13
14         int size = nums.size();
15         if(size <= 2)
16             return size;
17         int index = 0;//已确定的最后索引
18         bool isTwice = false;//是否一出现两次
19         for(int i=1; i<size; i++)
20         {
21             if(nums[i] != nums[index])
22             {
23                 nums[++index] = nums[i];
24                 isTwice = false;
25             }
26             else
27             {
28                 if(!isTwice)//重复,但此时为第二次出现
29                 {
30                     nums[++index] = nums[i];
31                     isTwice = true;
32                 }
33             }
34         }
35         nums.erase(nums.begin()+index+1, nums.end());
36         return index+1;
37     }
38 };
39
40 int main()
41 {
42     Solution sol;
43     int data[] = {1,1,1,2,2,3,3,3,3,3,4,5};
44     vector<int> test(data,data+sizeof(data)/sizeof(int));
45     for(const auto &i : test)
46         cout << i << " ";
47     cout << endl;
48     cout<< boolalpha << sol.removeDuplicates(test)<<endl;
49         for(const auto &i : test)
50         cout << i << " ";
51     cout << endl;
52 }

  其实,还有一种更简单的做法,不用isTwice标识,直接比较nums[index-1]即可[因为是排序过的]!!

 1 class Solution
 2 {
 3 public:
 4     int removeDuplicates(vector<int> &nums)
 5     {//移除有序数组中出现次数超过两次的数字
 6      //要求:原地移除,不能使用额外空间;返回新数组的长度
 7
 8         int size = nums.size();
 9         if(size <= 2)
10             return size;
11         int index = 1;//已确定的最后索引
12
13         for(int i=2; i<size; i++)//i从2开始
14         {
15             if(nums[i] != nums[index-1])
16                 nums[++index] = nums[i];
17         }
18         nums.erase(nums.begin()+index+1, nums.end());
19         return index+1;
20     }
21 };
时间: 2024-09-29 15:57:11

No.80 Remove Duplicates from Sorted Array ||的相关文章

80. Remove Duplicates from Sorted Array II

/* * 80. Remove Duplicates from Sorted Array II * 2016-5-13 by Mingyang * 这里用了通式,就是k的值可以根据需要随时改变,注意这里++j的用法,最后return ++j * 注意的是题目不光要返回int,还要把array给换了 */ public int removeDuplicates2(int[] A,int k){ int len=A.length; if(len<k) return len; int j=0; int

26. Remove Duplicates from Sorted Array &amp;&amp; 80. Remove Duplicates from Sorted Array II

26. Remove Duplicates from Sorted Array Given a sorted array, 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 in place with constant mem

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) {

80. Remove Duplicates from Sorted Array II(js)

80. Remove Duplicates from Sorted Array II Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying t

leetcode 26. Remove Duplicates from Sorted Array 、80. Remove Duplicates from Sorted Array II

两个题类似,第一个题是不允许有重复的数字,第二个题是允许每个数字最多重复两个,两个题目都要求在原数组上进行操作,并返回生成数组的长度,即空间复杂度为O(1). 两个题都是使用双指针,第一个指针指向生成新的数组的最后一个位置,第二个指针指向当前进行判断的位置. 唯一不同的是, 第二个题需要设置一个变量来控制重复的个数是否超过2 26. Remove Duplicates from Sorted Array class Solution { public: int removeDuplicates(

LeetCode26/27/80 Remove Duplicates from Sorted Array I and II/Remove Element**

一:Remove Duplicates from Sorted Array 题目: Given a sorted array, 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 in place with constant m

[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 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