26. Remove Duplicates from Sorted Array
2016-01-29 16:12
218 查看
题意: 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 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.
分析:去重
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array 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.
分析:去重
class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size() == 0) return 0; int ret = 1; for(vector<int>::iterator it = nums.begin(); it != nums.end();){ if(it != nums.begin() && *it != *(it - 1)){ ret ++; it ++; }else if(it != nums.begin() && *it == *(it - 1)){ nums.erase(it); }else{ it ++; } } return ret; } };
相关文章推荐
- 008实现一个算法从一个单链表中返回倒数第n个元素(keep it up)
- nyoj--491--幸运三角形(dfs)
- Android 获取以太网Mac地址和IP地址
- 如何实现手风琴折叠页面效果
- little SAS book
- NYOJ-24-素数距离问题
- Android:inputType参数类型说明
- Android Learning:数据存储方案归纳与总结
- opencv - haar人脸特征的训练
- linux网络编程实例
- 找到多个与名为“Login”的控制器匹配的类型
- Winform应用程序实现通用消息窗口
- Android-开发工具及测试工具
- 【MySQL】性能优化之 index merge (1)
- Spring bean的生命周期
- 音乐播放器APK之MediaPlayer使用指南
- Codeforces Round #330 (Div. 1) A. Warrior and Archer
- android的窗口机制分析------事件处理
- iOS微信第三方登录实现
- Mybatis传多个参数(三种解决方案)