【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 ...
随机推荐
- HTK学习2:工具使用
选自:http://www.cnblogs.com/mingzhao810/archive/2012/08/03/2617674.html 这个是重点,呵呵,本部分会讨论到如下内容: 1. 建立语音材 ...
- struts2 校验数据的有效性 2种方式
Struts2的数据校验: 数据的校验分为客户端校验和服务器端两种: 客户端校验:JS完成的校验.(为了提升用户体验.减少用户的输入错误) 服务器端校验:在后台的校验.(必须的.) 手动编码进行校验: ...
- svn上传报Authorization failed错误解决办法
svn上传文件时没有弹出用户登录界面,而是直接报Authorization failed错误.出现该问题基本都是三个配置文件的问题,下面把这个文件列出来 svnserve.conf配置文件中的 [ge ...
- 6. javacript高级程序设计-面向对象设计
1. 面向对象设计 1.1 理解对象 1.1.1 属性类型 (1). 数据属性:相当于对象的字段,包含一个数据值的位置,在这个位置可以读取和写入值.数据属性中有4个描述其行为的特性: l [[Conf ...
- Innodb 表空间卸载、迁移、装载
从MySQL的Innodb特性中我们知道,Inndob的表空间有共享和独享的特点,如果是共享的.则默认会把表空间存放在一个文件中(ibdata1),当开启独享表空间参数Innodb_file_per_ ...
- Java for LeetCode 221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- C#文件夹和文件操作
File.Exist(string path)//文件读写FileStream fs=new FileStream(filename, FileMode.Create);BinaryWriter bw ...
- July 9th, Week 28th Saturday, 2016
Every cloud has a silver lining. 山穷水尽疑无路,柳暗花明又一村. Every cloud has a silver lining, that just because ...
- Redis内存管理(二)
上一遍详细的写明了Redis为内存管理所做的初始化工作,这篇文章写具体的函数实现. 1.zmalloc_size,返回内存池大小函数,因为库不同,所以这个函数在内部有很多的宏定义,通过具体使用的库来确 ...
- python基础——实例属性和类属性
python基础——实例属性和类属性 由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(objec ...