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].

解法一:

我是Pick One!的,所以一开始没关心"Remove Duplicates"中的要求,不知道是排好序的,直接开始做了。

代码比较简单,建立map,存放每个元素与相应的出现次数。

class Solution {
public:
int removeDuplicates(int A[], int n) {
int sum = ;
map<int,int> m;
vector<int> v;
for(int i = ; i < n; i ++)
{
map<int,int>::iterator it = m.find(A[i]);
if(it == m.end())
{
m.insert(map<int,int>::value_type(A[i], ));
v.push_back(A[i]);
sum ++;
}
else if(it->second == )
{
it->second ++;
v.push_back(A[i]);
sum ++;
}
else
{
it->second ++;
}
}
for(vector<int>::size_type st = ; st < v.size(); st ++)
A[st] = v[st];
return sum;
}
};

解法二:

后来发现是排好序的,那么使用in-place做法就可以了。

使用index记录新的A数组下一个的位置,使用i扫描原始A数组。

核心思想就是:

如果i扫到的当前元素在index之前已经存在两个(注意,由于A是排好序的,因此只需要判断前两个就行),

那么i继续前进。否则将i指向的元素加入index,index与i一起前进。

class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n < )
return n;
int index = ;
for(int i = ; i < n; i ++)
{
if(A[i] != A[index-])
A[index ++] = A[i];
}
return index;
}
};

【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)的更多相关文章

  1. 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  3. 【LeetCode】080. Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  4. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  5. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

  6. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  7. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  8. 【LeetCode】83 - Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  9. 【LeetCode】26. Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. error: 'release' is unavailable: not available in automatic reference counting,该怎么解决

    编译出现错误: 'release' is unavailable: not available in automatic reference counting mode.. 解决办法: You nee ...

  2. <mvc:annotation-driven />做了什么

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...

  3. C语言:通过函数指针来完成两个数的加减乘除(函数指针当做参数使用)

    // //  main.c //  Function_pointer // //  Created by mac on 15/8/2. //  Copyright (c) 2015年. All rig ...

  4. OpenCV学习(40) 人脸识别(4)

    在人脸识别模式类中,还实现了一种基于LBP直方图的人脸识别方法.LBP图的原理参照:http://www.cnblogs.com/mikewolf2002/p/3438698.html       在 ...

  5. go语言之进阶篇非结构体匿名字段

    1.非结构体匿名字段 示例 : package main import "fmt" type mystr string //自定义类型,给一个类型改名 type Person st ...

  6. 什么是'脑分裂(split brain)'?

    这个词明显有点恐怖.设想一下,如果某时刻连接两个控制器之间的通路出现了问题,而不是其中某个控制器死机,此时两个控制器其实都是工作正常的,但是两者都检测不到对方的存在,所以两者都尝试接管所有总线,这时候 ...

  7. OpenGL使用libPng读取png图片

    #include<stdarg.h> #include<png.h> #include<glut.h> #include<math.h> #includ ...

  8. Splunk的安装与使用

    一.简单介绍         Splunk 是机器数据的引擎.使用 Splunk 可收集.索引和利用全部应用程序.server和设备(物理.虚拟和云中)生成的高速移动型计算机数据 .从一个位置搜索并分 ...

  9. [HTML5] Using the focus event to improve navigation accessibility (nextElementSibling)

    For a menu item, when we tab onto it, we want this element get 'focus' event, so that the submenu wi ...

  10. (LeetCode 86)Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...