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. Vue项目中使用vw实现移动端适配

    我们在vue移动端项目中的适配一般都采用rem,但是rem也不是能兼容所有的终端. 随着viewport单位越来越受到众多浏览器的支持,下面将简单介绍怎么实现vw的兼容问题,用vw代替rem 当我们采 ...

  2. JS 定时器,定时调用PHP

    $(function() { var voiceplay=function(){ var site = location.href.split('_cms')[0] + '_cms/'; $.ajax ...

  3. 学习新框架laravel 5.6 (第一天)

    学习新框架第一天. composer 基本命令: composer list 获取帮助信息 composer init 以交互方式填写composer.json文件信息 composer instal ...

  4. MySQL 主从服务器配置

    在主服务器Ubuntu上进行备份,执行命令: mysqldump -uroot -p --all-databases --lock-all-tables > ~/master_db.sql -u ...

  5. STL——list

    1.关键概述 list 是定义在 namespace::std 的模板,声明在 <list> ,存储结构是 双向链表, 提供的 正向和反向迭代器. 2.构造list对象 list<i ...

  6. R语言学习笔记(二): 类与泛型函数

    类 大多数R对象都是基于S3类(来源于第三代S语言),例如直方图函数hist()输出是一个包含多个组件的列表,它还有一个属性(attribute),用来指定列表的类,即histogram类. 泛型函数 ...

  7. python2.7入门---SMTP发送邮件

        SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib提 ...

  8. js分类多选全选

    效果如图: HTML代码: <div class="form-group quanxian-wrap"> <label>项目</label> & ...

  9. git的关于测试的相关的内容

    今天,我们来讲一下git的分支的一些内容,在以前的时候,我一直都以为,对于一个项目,这个时候,我们把这个项目叫做项目a项目,这个a项目有master,staging,以及我自己的分支xxx,当我想上测 ...

  10. 1 http协议

    1.四层模型 + 2.socket 3.http协议 4. HTTP请求 跟踪了新浪的首页,我们来总结一下HTTP请求的流程: 3.1.1 步骤1:浏览器首先向服务器发送HTTP请求,请求包括: 方法 ...