026 Remove Duplicates from Sorted Array
2016-04-06 21:46
253 查看
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.
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.
#include <iostream> #include <cstdio> #include <string> #include <vector> using namespace std; class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size() == 0) return nums.size(); int cnt = 1; for(int i = 1; i < nums.size(); i++) { while(i < nums.size() && nums[i-1] == nums[i]) i++; if(i < nums.size()) { cnt++; nums[cnt-1] = nums[i]; } } return cnt; } };
相关文章推荐
- Uva699 839二叉树的遍历
- matlab矩阵基础
- URAL 1297 最长回文子串(后缀数组)
- 时域认识采样定理的一个小例子
- 关于JAVA多态的一些笔记
- CNN model系列文章
- iOS js oc相互调用(JavaScriptCore)(二)
- Windows Server 2008 R2 Server Core常用命令及其安装
- 中央政府各职能部门简介
- [Java] 组合与继承的区别
- pixhawk双机通信测试例子
- SQL Server 2012 Express LocalDB
- 屏幕适配 和等比缩放
- 20135323符运锦----第七周:Linux内核如何装载和启动一个可执行程序
- [置顶] 深入理解JVM之三:类文件结构
- MySQL Cluster集群探索与实践
- SPOJ - DISUBSTR 多少个不同的子串
- SPOJ - DISUBSTR 多少个不同的子串
- Markdown语法及简要规则
- Windows Server 2008 R2 Hyper-v 基本使用方法