80 remove duplicates from sorted array 2
|
分类
|
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.
1
idx 是新数组的指针,i是nums数组的遍历指针。使用flag表示当前重复的元素个数。
如果nums[i] != nums[idx],更新idx,将nums[i]存到nums[idx],同时置flag为0。
如果nums[i] == nums[idx]且flag < 1,说明当前重复元素为0个,可以更新idx,将nums[i]存到
nums[idx],同时flag加1。
如果题目变为最多允许出现k次,将flag < 1改为flag < k-1即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int n = nums.length;
int idx = 0;
int flag = 0;
int tol
for(int i = 1; i < n; i++){
if(nums[i] != nums[idx]){
nums[++idx] = nums[i];
flag = 0;
}else if大专栏 80 remove duplicates from sorted array 2>(nums[i] == nums[idx] && flag < 1){
nums[++idx] = nums[i];
flag++;
}
}
return idx+1;
}
2 most votes
思路是将当前遍历元素与新数组的倒数第二个元素比较,只有n大于此元素才添加新元素。
太精炼了。也可以扩展到最多允许出现k次的情况。
1
2
3
4
5
6
7
public int removeDuplicates(int[] nums) {
int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i-2])
nums[i++] = n;
return i;
}
80 remove duplicates from sorted array 2的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II
1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- C和C++的区别,有你不知道的
c和c++可以说现在都是比较流行的,但是两者到底有什么联系和区别吗,这是学习c和c++最需要注意的,不要把两者搞混了,我们先开始就来看一下c和c++有什么联系,这两者可以这样说:C++是C的超集,兼容 ...
- 4)PHP命名规则,传值方式
(1)命名规则: 包括变量名,类名,接口名函数名等等 ①基本规则: 只能使用小写字母,下划线或者数字 数字不能开头 不能跟环境和系统关键字重复(比如,if,else,function) ② 驼峰式 ...
- day06-初识Vuetify框架UI框架和使用域名访问本地项目
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...
- Python语言学习前提:条件语句
一.条件语句 1.条件语句:通过一条或多条语句的执行结果(True或False)来决定执行额代码块.python程序语言指定任何非0或非空(null)的值为true,0或null为false. 2. ...
- 吴裕雄--天生自然C语言开发:共同体
union [union tag] { member definition; member definition; ... member definition; } [one or more unio ...
- jQuery性能优化与技巧
1.使用最新版本的jQuery类库 jQuery的每一个新的版本都会较上一版进行Bug修复和一些优化,同时也会包含一些创新,所以建议使用最新版本的jQuery来提高性能,需要注意的是在更换版本之后,要 ...
- Reveal详细安装教程
Reveal的详细安装使用 标签: Reveal 工具 调试 iOS 一.终端的操作 首先最重要的一点,要先把Reveal软件放到Application中,否则路径是错的,后面的设置也就没有作用了 打 ...
- redHat更新yum源
1. 网易镜像仓库查找相关rpm 包并下载 :http://mirrors.163.com/centos/6/os/x86_64/Packages/ wget http://mirrors.163.c ...
- ROS中的日志(log)消息
学会使用日志(log)系统,做ROS大型项目的主治医生 通过显示进程的运行状态是好的习惯,但需要确定这样做不会影响到软件的运行效率和输出的清晰度.ROS 日志 (log) 系统的功能就是让进程生成一些 ...
- [LC] 156. Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...