80. Remove Duplicates from Sorted Array II
2016-03-03 20:06
274 查看
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
Subscribe to see which companies asked this question
脑子好像转的有点慢了,discuss区里发现的超棒的方法,先贴上来,感受一下
class Solution {
public:
int removeDuplicates(vector& nums) {
if(nums.size()<=2)
{
return nums.size();
}
int index=2;
for(int i=2;i
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
Subscribe to see which companies asked this question
脑子好像转的有点慢了,discuss区里发现的超棒的方法,先贴上来,感受一下
class Solution { public: int removeDuplicates(vector<int>& nums) { int count=0; int len=nums.size(); for(int i=2;i<len;i++){ if(nums[i]==nums[i-count-2]){ count++; } else{ nums[i-count]=nums[i]; } } return len-count; } }; ********************* 几天之后回顾,更新另一种方法:
class Solution {
public:
int removeDuplicates(vector& nums) {
if(nums.size()<=2)
{
return nums.size();
}
int index=2;
for(int i=2;i
相关文章推荐
- OpenCV3.1-vc2008编译
- ubuntu 开机启动 Dr.com Matlab等应用,程序
- 数据库连接池、静态连接,普通连接的比较2(多线程)
- BZOJ 2088/POI 2010 Teleportation
- JavaScript事件绑定与冒泡捕获
- Linux软件包管理之yum
- 面试笔试杂项积累-Unity
- hadoop基础----hadoop理论(三)-----hadoop分布式文件系统HDFS详解
- hadoop基础----hadoop理论(三)-----hadoop分布式文件系统HDFS详解
- w3c盒模型和ie盒子模型
- 创业笔记-Node.js入门之阻塞与非阻塞
- 使子控件超出父控件的限制
- 一个优秀的提交应该包含什么?
- 常见的Android优化方案
- java设计模式——创建型之工厂三兄弟(1)
- java web目录层次
- Leet Code OJ 206. Reverse Linked List [Difficulty: Easy]
- 采用nginx让多个tomcat实现负载均衡
- JS练习(个人练习有许多问题,解决不了)
- mac 安装lua命令