【leetcode】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 memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
这个题目挺简单的,就是删除数组中的重复元素就行,而且数组还已经排好序了。
接下来我们要做的就是考虑细节问题了,我的做法是首先考虑特殊输入(也可以说是非法输入什么的),比如:输入如果是0、1怎么办,然后考虑复杂度一定很容易能想到O(n)的算法(千万不要O(n^2)),想到这些题目就很简单了。
我的做法是这样的:用一个值记录前面有几个重复的了,后面的直接转移到它应在的位置,一步到位。
代码如下:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n==)
{
return ;
}
if(n==)
{
return ;
}
int pre=A[];
int flg=;
for(int i=;i<n;i++)
{
if(pre==A[i])
{
pre=A[i];
flg++;
}
else
{
pre=A[i];
A[i-(flg)]=A[i];
}
}
return n-flg;
}
};
虽然题目很简单,但是还是有些值得我们学习得,通过leetcode的练习我觉得收获得不仅仅是算法方面的知识,更多的应该是考虑问题的全面性,直接一点就是每一道题都可以假设是你在面试中遇到,在实现之前你要怎么考虑。keep going!
【leetcode】Remove Duplicates from Sorted Array的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【Leetcode】【Easy】Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【数组】Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Remove Duplicates from Sorted List
题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- 【leetcode】 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
随机推荐
- EXTJS 6 必填项加星号*
/**重写ext filed组件, 实现表单必填项加红色*星号**/ Ext.override(Ext.form.field.Base,{ initComponent:function(){ if(t ...
- linux命令——mutt的安装和使用【转】
linux命令--mutt的安装和使用[转] 首先介绍一下mutt这个软件,它是一款基于文字界面的邮件客户端,非常小巧,但功能强大,可以用它来读写,回复保存和删除你的邮件,能在linux命令行模式下收 ...
- Access an instance through a console
VNC or SPICE is used to view the console output of an instance, regardless of whether or not the con ...
- C#中使用ADOMD.NET查询多维数据集
ADOMD.NET 是用于与 Microsoft SQL Server Analysis Services 进行通信的 Microsoft .NET Framework 数据访问接口. ADOMD.N ...
- POJ 1258
http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...
- INNODB自增主键的一些问题
背景: 自增长是一个很常见的数据属性,在MySQL中大家都很愿意让自增长属性的字段当一个主键.特别是InnoDB,因为InnoDB的聚集索引的特性,使用自增长属性的字段当主键性能更好,这里要说明下自增 ...
- manage account
#!/bin/bash # #Delete_user - Automates the steps to remove an account # ############################ ...
- HTML 基础
1.HTML 超文本标记语言 2.网页分类: 动态网页 静态网页 ①静态网页与动态网页区别: 主要:动态网页与数据库链接,静态网页不与数据库连接: ②静态网页 修改展示图片 必须修改源代码 : ...
- 【OpenCV】内存溢出
今天在写读大量图片时,发现在读到第721张时,内存溢出了,无法继续读.出错语句为pframe2 = cvLoadImage(pname2); 后来加上了ReleaseImage(&pname2 ...
- $.each 和$(selector).each()的区别
$.each() 对数组或对对象内容进行循环处理 jQuery.each( collection, callback(indexInArray, valueOfElement) ) collectio ...