博客
关于我
80. Remove Duplicates from Sorted Array II
阅读量:414 次
发布时间:2019-03-06

本文共 1592 字,大约阅读时间需要 5 分钟。

Given a sorted array nums, remove the duplicates  such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array  with O(1) extra memory.

Example 1:

Given 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 respectively.It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,1,2,3,3],Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)int len = removeDuplicates(nums);// any modification to nums in your function would be known by the caller.// using the length returned by your function, it prints the first len elements.for (int i = 0; i < len; i++) {    print(nums[i]);}
 

AC code:

class Solution {public:    int removeDuplicates(vector
& nums) { int i = 0; for (auto n : nums) { if (i < 2 || n > nums[i-2]) nums[i++] = n; } return i; }};
Runtime: 
8 ms, faster than 100.00% of C++ online submissions for Remove Duplicates from Sorted Array II.

 

 

转载地址:http://uzxuz.baihongyu.com/

你可能感兴趣的文章
开源项目在闲鱼、b 站上被倒卖?这是什么骚操作?
查看>>
Vue3发布半年我不学,摸鱼爽歪歪,哎~就是玩儿
查看>>
《实战java高并发程序设计》源码整理及读书笔记
查看>>
Java开源博客My-Blog(SpringBoot+Docker)系列文章
查看>>
程序员视角:鹿晗公布恋情是如何把微博搞炸的?
查看>>
Spring+SpringMVC+MyBatis+easyUI整合进阶篇(七)一次线上Mysql数据库崩溃事故的记录
查看>>
【JavaScript】动态原型模式创建对象 ||为何不能用字面量创建原型对象?
查看>>
ClickHouse源码笔记4:FilterBlockInputStream, 探寻where,having的实现
查看>>
Linux应用-线程操作
查看>>
多态体验,和探索爷爷类指针的多态性
查看>>
系统编程-进程间通信-无名管道
查看>>
记2020年初对SimpleGUI源码的阅读成果
查看>>
C语言实现面向对象方法学的GLib、GObject-初体验
查看>>
系统编程-进程-ps命令、进程调度、优先级翻转、进程状态
查看>>
为什么我觉得需要熟悉vim使用,难道仅仅是为了耍酷?
查看>>
一个支持高网络吞吐量、基于机器性能评分的TCP负载均衡器gobalan
查看>>
HDOJ2017_字符串统计
查看>>
高等软工第二次作业《需求分析阶段总结》
查看>>
404 Note Found 团队会议纪要
查看>>
CentOS安装Docker-ce并配置国内镜像
查看>>