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

要求:不准额外空间,且时间O(n)

思路:index很重要,是去重复后的新数组的长度,默认值1。对一个已排序的有重复的数组,重复数字肯定是连续的

如1,1,1,1,2,2,2,2,5,5,5,我们需要做的就是把连续的2序列的第一个放到前面去(第一个数字之后,更新A[index]),把连续的5序列的第一个放到前面去,那么条件判断就是看什么时候可以找到前一个值和当前值不相等,那么我我们就要做处理。

代码:

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

Remove Duplicates from Sorted Array(参考)的更多相关文章

  1. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  2. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  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] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  5. Remove Duplicates From Sorted Array

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

  6. 【leetcode】Remove Duplicates from Sorted Array II

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

  7. 26. Remove Duplicates from Sorted Array

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

  8. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

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

  10. 【26】Remove Duplicates from Sorted Array

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

随机推荐

  1. IntelliJ IDEA安装与破解

    1.软件下载 文中使用到的安装包下载 2.部署 安装一路下一步即可. 把下载的JetbrainsCrack-3.1-release-enc.jar放在安装目录的bin目录下 3.修改配置文件 在安装的 ...

  2. MySQL学习随笔--通过实例理解merge ,temptable算法的差异

    实例1 使用视图的两种算法merge和temptable分别统计 表tb_phone中market_price大于4000的手机,然后查询视图查找出小于6000的手机 简单总结最终获取的结果:查询出m ...

  3. Protostuff序列化和反序列化

    序列化和反序列化是在应对网络编程最常遇到的问题之一. 序列化就是将Java Object转成byte[]:反序列化就是将byte[]转成Java Object. 这里不介绍JDK serializab ...

  4. mysql 插入多条记录,重复值不插入

    只去除主键与唯一索引的字段,字段为null时 是可以重复插入的domo: insert ignore into table_name(email,phone,user_id) values('test ...

  5. codeforces_1075_C. The Tower is Going Home

    http://codeforces.com/contest/1075/problem/C 题意:一个长宽均为1e9的棋盘,n个垂直障碍在x列无限长,m个水平障碍在第y行从第x1列到x2列.可以水平和垂 ...

  6. 05CSS链接

    CSS链接 链接的四种状态: •  a:link - 普通的.未被访问的链接 •  a:visited - 用户已访问的链接 •  a:hover - 鼠标指针位于链接的上方 •  a:active ...

  7. 下载kaggle数据集,验证手机号

    https://blog.csdn.net/Tomxiaodai/article/details/80167765 kaggle上下载一下数据集必须手机验证,结果验证时一直提示错误输入的格式错误,试了 ...

  8. Failed to resolve filter报错原因

    问题 页面写过滤器,控制台报错,Failed to resolve filter 分析 语法错误?先检查 ``` {{ params | filterA }} filters: { filterA: ...

  9. ''tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[?]'' 错误分析

    这是tensorflow 一个经常性错误,错误的原因在于:显卡内存不够. 解决方法就是降低显卡的使用内存,途径有以下几种措施: 1 减少Batch 的大小 2 分析错误的位置,在哪一层出现显卡不够,比 ...

  10. 【牛客小白月赛6】 J 洋灰三角 - 快速幂&逆元&数学

    题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时 ...