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;
}
};

最新文章

  1. windows8.1下php环境搭建及基本配置(php+apache+mysql)
  2. IOS中NSUserDefaults的用法(轻量级本地数据存储)
  3. 奔小康赚大钱 hdu 2255( KM )
  4. Django 源码小剖: 响应数据 response 的返回
  5. 【ASP.NET 基础】ASP.NET内置对象
  6. 各Android版本的Linux内核表(待续)以及如何下载相应的android源码
  7. 06-Java 本地文件操作
  8. HTML5跨浏览器表单及HTML5表单的渐进增强
  9. Android 不能勾选 Project Build Target
  10. 黑马程序员-ReadInt
  11. Linux字符设备中的两个重要结构体(file、inode)
  12. Vericant维立克 | 氪加
  13. 【SSH三个框架】Hibernate第七基金会:不少下属业务
  14. delphi 中sql的语法规范
  15. ServiceStack.Redis 使用链接池方法
  16. pycharm远程linux开发和调试代码
  17. VS2017离线安装包[百度云盘](收藏了)
  18. 图像频谱图画图——matlab
  19. Linux账号管理
  20. sublime text 3中文乱码问题解决的方法

热门文章

  1. iOS Programming Touch Events and UIResponder
  2. 关于docker入门教程
  3. mysql 5.7安装过程中,初始化的问题
  4. 15年第六届蓝桥杯第七题_手链样式_(stl_string)
  5. flask的基本搭建
  6. CV方向:纽劢科技,复星,蚂蚁金服,哈喽出行,海康威视
  7. $(&quot;[lay-id=&#39;demo&#39;] tbody tr[data-index=0]&quot;) 查找某行layui table
  8. java_tcp_简单示例
  9. redis中基本命令
  10. sqllite相关总结