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. Asp.Net Core 使用Docker进行容器化部署(一)

    前几篇文章介绍了Liunx下的环境搭建,今天来分享一下.Net Core在Liunx上的部署. 我采用的方案是使用Dokcer作为运行虚拟机,Nginx作为Http服务器来进行反向代理,你可以理解为D ...

  2. python3 练习题100例 (二十五)打印一个n层金字塔

    题目内容: 打印一个n层(1<n<20)金字塔,金字塔由“+”构成,塔尖是1个“+”,下一层是3个“+”,居中排列,以此类推. 注意:每一行的+号之后均无空格,最后一行没有空格. 输入格式 ...

  3. Django之频率组件

    一.频率简介 为了控制用户对某个url的请求 的频率,比如 ,一分钟以内,只能访问三次 二.自定义频率类,自定义频率规则 自定义的逻辑 (1)取出访问者的ip (2)判断当前ip不在访问字典里,添加进 ...

  4. Java学习笔记十四:如何定义Java中的类以及使用对象的属性

    如何定义Java中的类以及使用对象的属性 一:类的重要性: 所有Java程序都以类class为组织单元: 二:什么是类: 类是模子,确定对象将会拥有的特征(属性)和行为(方法): 三:类的组成: 属性 ...

  5. JAVA学习一 对象数组

    对象数组 今天在写一个代码,才发现自己对于对象数组的理解是不够的,那么就讲讲自己现在的理解. 对于数组中的每一个元素都是一个针对对象的引用 他会指向你的具体的一个堆上的对象,它本身知识一个地址值,与其 ...

  6. [Cracking the Coding Interview] 4.6 Successor 后继节点

    Write an algorithm to find the 'next' node(i.e. in-order successor) of a given node in a binary sear ...

  7. OVERLAY(文字の上書き)

    OVERLAY 命令により.文字列が別の文字列によって上書きされます. OVERLAY c1 WITH c2 [ONLY str]. この命令により.項目 c1 のすべての位置のうち.str の中に出 ...

  8. HBase配置和使用

    参考官方文档 整体实现框架 图1 以下几个为组成部件 21892 HMaster 22028 HRegionServer 21553 QuorumPeerMain 2366 NameNode 2539 ...

  9. 2,PyAudio 实现录音 自动化交互实现问答

    Python 很强大其原因就是因为它庞大的三方库 , 资源是非常的丰富 , 当然也不会缺少关于音频的库 关于音频, PyAudio 这个库, 可以实现开启麦克风录音, 可以播放音频文件等等,此刻我们不 ...

  10. XStream轻松转换xml和java对象

    首先引入所需的jar: xstream-1.4.9.xpp3_min-1.1.4c.dom4j-1.6.1, 或用maven管理jar包时在pom.xml中添加: <!-- https://mv ...