leetcode-Remove Duplicates from Sorted Array
2015-11-06 20:43
190 查看
Difficulty:
Easy
Given a sorted array, remove the duplicates in place such that each element appear onlyonce 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
1and
2respectively. It doesn't matter what you leave beyond the new length.
class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.empty()) return 0; int size=nums.size(); int count=0; int slow=0; for(int i=0;i<size;++i){ while(i+1<size&&nums[i]==nums[i+1]) ++i; nums[slow++]=nums[i]; ++count; } size=size-count; while(size--) nums.pop_back(); return count; } };
相关文章推荐
- android studio 安装过程中intelhaxm-android不能安装的解决办法s
- 一个简易的指南针
- 【Codeforces Round 329 (Div 2) D】【LCA+并查集路径压缩】Happy Tree Party 除上两点间路径全部权值
- SpringMVC 学习系列 (8) 之 国际化
- Java程序员进化为架构师掌握的知识
- 纯SystemC设计吃进Questasim10.2c时遇到的问题
- 用ssh登陆远端主机
- Fedora (server) 23安装
- JavaScript函数
- 查询android资源R对应的空间
- Unicode转换成中文明文字符串,条码枪扫描结果解析
- React学习笔记---项目构建
- 猪八戒网站上的骗子为什么这么多
- Android Studio1.3/1.4如何升级Gradle到version 2.5
- 抽象工厂+反射+依赖注入 实现对数据访问层和业务逻辑层的优化
- 论文笔记《Fully Convolutional Networks for Semantic Segmentation》
- CodeForces 319B 栈
- hdu2033
- 快速排序的简单实现(Quick_Sort)
- 静态编联与动态编联总结