【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
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)的更多相关文章
- 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【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 ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【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 ...
- 【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 ...
随机推荐
- LaTeX快速入门-蔡炎龙
蔡老师的这个文档只有26页,非常简短称得上是快速入门的文档了,TeX的基本入门这个文档还做不到,仅仅是一个简单的引子,让大家管中窥豹,先简单使用,然后才会更加深入进去. 文档早一个版本是用CJK排版的 ...
- python限制进程、子进程占用内存大小、CPU时间的方法:resource模块
内置模块:resource 在mac环境下功能会存在问题.linux下可以使用:但是for i in range(10000)的值必须是10000或者更大的数值才有用.没有搞清楚为什么 #/usr/b ...
- SQL Server AlwaysOn Setup Step-By-Step Guide
Step-By-Step: Creating a SQL Server 2012 AlwaysOn Availability Group http://blogs.technet.com/b/cani ...
- MD5 SHA1 哈希 签名 碰撞 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- scala 学习笔记十 一 伴生对象
1.介绍 a.所谓伴生对象就是和某个class同名的object, 并且object 必须和class在同一个scala源文件中. b.在scala中,没有像java中的静态类,静态方法和静态成员等, ...
- WPF Converter 使用复杂参数的方法
Step 1在WPF的C#代码文件中给定义复杂类型的变量,并给其赋值:Sample code: List<User>lsUser=....Setp 2在 C#代码对应的XAML 中将此复杂 ...
- APP 打包測试流程 从零開始
前言: 苹果应用打包測试一直是件令人头疼的事.尤其是第一次打包的时候,因为苹果官网是全英文性且缺少仔细的步骤指引.刚開始学习的人往往要花费非常多时间去干一件三分钟就能搞定的事. 今天我们来透彻的解说一 ...
- AngularJS是什么?
AngularJS扩展了HTML? 看了几天AngularJS的各种中文教程,一直没有理解AngularJS是做什么的. 直到今天了英文文档,才有了初步了解. HTML是静态语言. JavaScrip ...
- jsoup抓取豆瓣美女
package com.huowolf; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOu ...
- Simple example
This is a simple example showing a small window. Yet we can do a lot with this window. We can resize ...