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].

题意:除去重复的元素,若有重复只保留一个,返回长度。

思路:遍历数组,用不同的,覆盖相同的。过程:保存第一个元素的下标lo。遇到相等不管,继续遍历,遇到不等的,则将该值赋给lo的下一个,反复这样。代码如下:

 class Solution
{
public:
int removeDuplicates(int A[],int n)
{
if(n<=) return ;
int lo=;
for(int i=;i<=n-;++i)
{
if(A[lo] !=A[i])
A[++lo]=A[i];
}
return lo+;
}
};

思路相同,另一种写法:

 class Solution {
public:
int removeDuplicates(int A[], int n)
{
if(n<) return n;
int count=;
int flag=;
for(int i=;i<n;++i)
{
if(A[flag] !=A[i])
{
A[++flag]=A[i];
count++;
}
}
return count;
}
};

[Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素的更多相关文章

  1. [Leetcode] Remove duplicates from sorted list 从已排序的链表中删除重复元素

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

  2. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

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

  3. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  4. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  5. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  7. LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)

    题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...

  8. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  9. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

随机推荐

  1. I/O流、序列化

    1)流序列化对象ObjectOutputStream调用writerObject写出序列化对象,ObjectInputStream调用readObject读取序列化对象,序列化的对象必须要实现Seri ...

  2. 我是一个MySQL小白

    我是一个MySQL小白 第一回早起装扮 “mysql,Oracle,SQL-SERVER你们三个 起床没?”,清晨七点多师父喊道. “师父,我(mysql)哪敢睡觉呀,我还在查询表呢,有客户的密码忘记 ...

  3. Hive初识(三)

    根据用户的需求创建视图.可以将任何结果集数据保存为一个视图.视图在Hive的用法和SQL视图用法相同.它是一个标准的RDBMS概念.我们可以在视图上执行所有DML操作. 创建一个试图 可以创建一个试图 ...

  4. logger模块的使用

    logging模块 下面是logger模块的配置文件,在写程序需要记录日志可以直接拿过来用,但是要经过相应配置的一些修改. 对于如何使用,在我上一篇随笔<ATM程序规范练习>中的记录日志的 ...

  5. VGA 时序标准

    VGA 显示器扫描方式从屏幕左上角一点开始,从左像右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT 对电子束进行消隐,每行结束时,用行同步信号进行同步:当扫描完所有的行 ...

  6. MVC中Model 的Key值不建议用非int型

    一次在开发中,key的值用了 byte型,结果插入第一条正常,第二条开始就出错,原因是用byte类型无法实现自动增加1,所以为了方便,建议使用 int型. public virtual byte bk ...

  7. IOException: win32 io returned 267. Path:

    unity3d在导出android项目时出现了这个错误,找了一圈也没找到原因,最后把项目名中空格去掉后OK了,坑啊!!!!

  8. 自学MVC开发基础

    由于现在面试需求,我必须有点了解MVC开发基础,MVC是一个开发框架或者是一个开发模式,MVC让软件开发的过程大致切割成三个单元,分别是:Model(模型).View(试图).Controller(控 ...

  9. 「暑期训练」「基础DP」 Common Subsequence (POJ-1458)

    题意与分析 很简单:求最长公共子序列. 注意子序列与子串的差别:一个不连续一个连续.一份比较好的参考资料见:https://segmentfault.com/a/1190000002641054 状态 ...

  10. [模板]BZOJ4756线段树合并

    题面 Solution: 板子不解释 #include <iostream> #include <algorithm> #include <cstdio> #inc ...