leetcode 之Remove Duplicates from Sorted Array(2)
描述
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]
之前的想法是再加一个计数的变量就行了
int removeDeplicates1(int A[], int n)
{
int index = , count = ;
for (int i = ; i < n; i++)
{
if (A[index] != A[i])
{
A[index++] = A[i];
count = ;
}
else
{
if (count <= )
{
A[index] = A[i];
count++;
}
}
} return index + ;
}
后来看到了一个更简洁的做法,这个想法很巧妙啊:因为是排好序的,所以只需拿第三个和第一个比,不同的话保存即可。
int removeDeplicates1(int A[], int n)
{
int index = ;
for (int i = ; i < n; i++)
{
if (A[index-] != A[i])
{
A[index++] = A[i];
} } return index;
}
leetcode 之Remove Duplicates from Sorted Array(2)的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [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% ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 洛谷 P2659 美丽的序列 解题报告
P2659 美丽的序列 题目背景 GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列. 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的"美丽度"和" ...
- oAuth2.0理解
转自http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html 理解OAuth 2.0 作者: 阮一峰 日期: 2014年5月12日 OAuth是一个关 ...
- php安装gd库
安装gd需要以下库: gd-2.0.33.tar.gz http://www.boutell.com/gd/ jpegsrc.v6b.tar.gz http://www.ijg.org/ libpng ...
- bzoj3192: [JLOI2013]删除物品(树状数组)
既然要从一个堆的堆顶按顺序拿出来放到第二个堆的堆顶,那么我们就可以把两个堆顶怼在一起,这样从一个堆拿到另一个堆只需要移动指针就好了. 换句话说,把1~n倒着,n+1到n+m正着,用一个指针把两个序列分 ...
- net-speeder
有的同学反映自己的搬瓦工速度慢,丢包率高.这其实和你的网络服务提供商有关.据我所知一部分上海电信的同学就有这种问题.那么碰到了坑爹的网络服务商,我们应该怎么办呢? duangduang~~~~~~有请 ...
- extjs grid demo
Ext.onReady(function () { var store = Ext.create('Ext.data.Store', { fields: ['id', 'name', 'account ...
- jdbc:oracle:thin:@localhost:1521:orcl和jdbc:oracle:thin:@localhost:1521/orcl的区别
Oracle Thin JDBC Driver 驱动程序包名:ojdbc14.jar.ojdbc6.jar 驱动程序类名: oracle.jdbc.driver.OracleDriver JDBC ...
- 解决CodeBlocks无法自动补全的问题
在Deepin下安装的CB,输入printf.scanf的时候不会自动补全,这样就很难受. 解决办法是在Setting -> Editor -> Syntax highlighting - ...
- SSM框架使用-wrong
mybatis手册 1. mybatis 绑定错误 如果出现: org.apache.ibatis.binding.BindingException: Invalid bound statement ...
- 最短路+找规律 Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest L. Right Build
题目链接:http://codeforces.com/gym/101149/problem/L 题目大意:有n个点(其实是n+1个点,因为编号是0~n),m条有向边.起点是0,到a和b两个节点,所经过 ...