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 ...
随机推荐
- SEO优化技巧
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
- iTOP-4412开发板-can测试工具使用文档
本文档介绍如何使用 can 工具测试 can. 给用户提供了“can_libs.rar”以及“can_tools.zip”压缩包,分别是 can 工具需要的库 文件和 can 工具二进制文件. 注意开 ...
- BTree非递归
preorder void PreOrder(BTNode* b) { BTNode* p = b; SqStack* st; InitStack(st); if (b != NULL) { Push ...
- mysql 数据库保存\n 微信分享时不能换行
主要因为保存的是\n 但是查询出来是\\n 所以需要把\\n替换为\n即可(不转换的话不会换行并且显示\n)
- 《VSTO开发入门教程》配套资源下载
<VSTO开发入门教程> 刘永富 著 清华大学出版社 封面截图 购书网址 京东网 淘宝网 配套资源到如下页面寻找: https://www.cnblogs.com/ryueifu-VBA/ ...
- 34)PHP,PHP从数据库读取数据并在html中显示
首先是我的数据库截图: 然后展示我的php文件: b.php文件: <?php $link= mysqli_connect('localhost','root','root'); // mysq ...
- token和refresh token
https://www.cnblogs.com/minirice/p/9232355.html 在spring boot中结合OAuth2使用JWT时,刷新token时refresh token一直变 ...
- vue中v-on支持的事件总结
资源事件 事件名称 何时触发 error 资源加载失败时. abort 正在加载资源已经被中止时. load 资源及其相关资源已完成加载. beforeunload window,document 及 ...
- hdu1069 Monkey and Banana LIS
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...
- [LC] 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...