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 by modifying the input array in-place with O(1) extra memory.

Example:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.

思路:此题比较简单,大意是将数组中重复点删除,然后返回新数组的长度。数组中前n个数就是新的数组。唯一难点是不能用额外空间。

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() <= ){
return nums.size();
}
int len = ; // 记录当前的新的数组的长度
for (int i = ; i < nums.size(); i++){
if (nums[i] != nums[i - ]){
nums[len] = nums[i]; //将新的元素装到前len个
len++;
}
}
return len;
}
};
 

最新文章

  1. 自定义cell右侧 多按钮
  2. Excel中如何过滤复选框 How to filter checkbox column in Excel
  3. 【如何快速的开发一个完整的iOS直播app】(采集篇)
  4. DevExpress GridView对表格的部分说明
  5. 防止SVN冲突,Elipse资源同步介绍
  6. javacript 优化2
  7. EcShop二次开发系列教程–总纲
  8. [Everyday Mathematics]20150210
  9. C# 制作外挂常用的API
  10. 实践Scrum
  11. POJ 3673 Cow Multiplication
  12. [转帖]知乎专栏:正确使用 Docker 搭建 GitLab 只要半分钟
  13. node代理服务器
  14. BlueZone automation note1
  15. linux_开启mysql服务
  16. php分享十五:php的命令行操作
  17. Jsp之神笔记
  18. Jmeter-Critical Section Controller(临界区控制器)
  19. Eclipse Class Decompiler——Java反编译插件(转)
  20. 5、Spring Cloud-声明式调用 Feign(下)

热门文章

  1. MySQL 5.7忘记root密码如何修改?
  2. [20180926]查询相似索引.txt
  3. 前后端分离djangorestframework——序列化与反序列化数据
  4. mysql Client does not support authentication protocol requested by server; consider upgrading MySQL
  5. python3爬虫抓取智联招聘职位信息代码
  6. 《Java大学教程》—第15章 异常
  7. Caused by: java.io.FileNotFoundException: velocity.log (No such file or directory)
  8. sqlSugar的使用---入门
  9. 【js】把一个json对象转成想要的数组
  10. yaml的简单学习