题目描述:

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的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  2. 【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 ...

  3. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)

    这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  4. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  5. 【Leetcode】【Medium】Remove Duplicates from Sorted Array II

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

  6. 【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 ...

  7. 【数组】Remove Duplicates from Sorted Array II

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

  8. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  9. 【leetcode】 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

随机推荐

  1. mach 和 array 方法

  2. 【Unity3D】计算二维向量夹角(-180到180)

    在Unity3D中,有时候我们需要计算二维向量的夹角.二维向量夹角一般在0~180度之前,可以直接调用Vector2.Angle(Vector2 from, Vector2 to)来计算. 但是在有些 ...

  3. 开始学习C++

    这里突然想起来当初学习java和C# 总是会有个demo :  hello  world. 这里我记得我曾经看过一个笑话.说有个程序员,想学习书法,买了笔墨,都准备好了,但是不知道写什么好.最后,他大 ...

  4. android SDK manager 无法获取更新版本列表

    打开SDK Manager---Tools---Options,填入如下代理和端口,勾选选项也如下. 网址:mirrors.neusoft.edu.cn 端口:80 99%是成功的 参考:http:/ ...

  5. ios NSURLSession completeHandler默认调用quque

    注意 , [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSU ...

  6. How to call getClass() from a static method in Java?

    刚才在学习Java 使用properties类,遇到这样的错误: Cannot make a static reference to the non-static method getClass() ...

  7. SAP系统更改小数点显示问题

    在SAP系统中会出现小数点显示的问题,比如123.12,正常情况下是这样显示,但SAP系统是德国的出的系统,德国的书写数字的习惯是将小数点“.”写成“,”逗号,显示为:123,12 这个问题可以使用事 ...

  8. Effective C++ -----条款51:编写new 和delete 时需固守常规

    operator new 应该内含一个无穷循环,并在其中尝试分配内存,如果它无法满足内存需求,就该调用new-handler.它也应该有能力处理0 bytes 申请.Class专属版本则还应该处理“比 ...

  9. RSA 加解密转换

    由于项目的原因,原来的项目使用.net 进行开发,现在需要转成java, 所以原来的加解密就成了一个棘手的问题.由于数据使用RSA签名加密,又因为.net 和 Java 加解密算法上的差异,并不能使用 ...

  10. 【linux】vim的一些快捷键

    ctrl+y  :重复上一行内容 v+移动光标  :选择内容 y  :复制选中的内容 p  :在光标处粘贴复制的内容 ctrl+v :进入列模式,可以选择多列数据 dd :剪切一行,也可做删除一行使用