题目描述:

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. SX学SX内容 笔记?

    某帖子笔记1 主要还是从三体吧某精品贴里看来的... 集合论 集合就是一堆东西...满足 1) 集合中的元素互异(即每种只有一个) 2) 集合中的元素无序(不是一个数组,集合中的元素没有显然的排序法则 ...

  2. Flatten 2D Vector

    Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  3. 基础知识(javaWeb工程目录结构)及各文件夹的作用

    在Eclipse中只要创建一个Dynamic Web Project,就可以根据创建向导创建出一个典型Java Web站点的目录结构.除非有特殊需要,在大多数情况下都没有必要修改这个目录结构,这也是W ...

  4. C# 读写十六进制bin 文件

    读一个十六进制的bin文件,在bin文件添加四行头,生成新的bin文件.bin文件可以用vs打开查看. using System; using System.Collections.Generic; ...

  5. ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)

    当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...

  6. MySQL SQL优化之in与range查询【转】

    本文来自:http://myrock.github.io/ 首先我们来说下in()这种方式的查询.在<高性能MySQL>里面提及用in这种方式可以有效的替代一定的range查询,提升查询效 ...

  7. vs版本转换工具

      [转]C#写的工程项目移植转换工具 – 支持VS2005/VS2010/VS2012/VS2013 经常用Visual Studio开发项目的是不是会经常遇到下面这种情况或者类似于这样的情况?用新 ...

  8. Django~static files (e.g. images, JavaScript, CSS)

    django.contrib.staticfiles settings.py django.contrib.staticfiles is included in your INSTALLED_APPS ...

  9. Android Volley入门到精通:定制自己的Request

    经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...

  10. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...