Remove Duplicates from Sorted Array
2015-09-18 11:46
330 查看
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 =
Your function should return length =
It doesn't matter what you leave beyond the new length.
solution: 直接取出vector中重复的数据
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
1and
2respectively.
It doesn't matter what you leave beyond the new length.
solution: 直接取出vector中重复的数据
#include <limits.h> class Solution { public: int removeDuplicates(vector<int>& nums) { int count = nums.size(); int previous = INT_MIN; int start = -1; int end = -1; for(int i = 0; i < nums.size();) { if(nums[i] == previous){ start = i; end = i; int j = i+1; for(; j < nums.size(); j++) { if(nums[j] == previous){ end++; }else{ break; } } count = count - (end - start + 1); nums.erase(nums.begin() + i, nums.begin() + j); } else{ previous = nums[i]; i++; } } return count; } };
相关文章推荐
- 数据结构中的常用查找
- EOF是什么?
- 前端资料收集
- github常用命令
- udhcpc遇到问题
- Android中内存泄露监测
- C++标准库:bitset 用法整理 (来自网易 happyboy200032的博客)
- DevExpress源码编译总结 z
- 在Json解析过程中,我为什么用object1.optInt ,和 object1.optString
- CPS冥想 - 2 手撸控制流
- node
- 什么是log4j ?
- 收藏链接
- 适配 ios 9
- IOS9之BitCode
- Android代码截屏
- MongoDB学习记录
- oracle utf8字符集转gbk(转)
- Eclipse、Idea快捷键对比
- 关于xcode7的一些问题