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 ...
随机推荐
- 函数(Python)
函数是什么? 计算机的函数,是一个固定的一个程序段,或称其为一个子程序,它在可以实现固定运算功能的同时,还带有一个入口和一个出口,所谓的入口,就是函数所带的各个参数,我们可以通过这个入口,把函数的参数 ...
- Kubernetes系列:Kubernetes Dashboard
15.1.Dashboard 作为Kube认得Web用户界面,用户可以通过Dashboard在Kubernetes集群中部署容器化的应用,对应用进行问题处理和管理,并对集群本身进行管理.通过Dashb ...
- AtCoder Grand Contest 033
为什么ABC那么多?建议Atcoder多出些ARC/AGC,好不容易才轮到AGC…… A 签到.就是以黑点为源点做多元最短路,由于边长是1直接bfs就好了,求最长路径. #include<bit ...
- Matlab高级教程_第二篇:关于MATLAB转C#过程中MWArray到C#数组,C#数组到MWArray相互转换
Matlab传递数据时使用的方法,那么Matlab计算完成后在C#中应该怎么获取它的计算数据呢? 需要遵循两个基本步骤: 弄清楚Matlab函数传回的数据到底是什么格式?struct?cell?cha ...
- HDU 6126 Give out candies(网络流)
题目给出n,m,k 然后给出n*m的矩阵a[i][j]代表第i个人在获得j 颗糖果能得到的满足值, 然后k是k行每行输入三个整数x,y,z ,x,y,z表示一组限制表示第x个人分到的糖数减去第 ...
- win10//ubuntu安装tensorflow-gpu与kears,并用minist测试
WIn10 安装cuda 先安装VS,然后根据自己的版本安装CUDA. 安装完后,打开cmd命令行输入nvcc -V,检测是否安装成功 安装cuDDN 安装对应版本,解压后覆盖到CUDA的地址,默认为 ...
- C#函数的基础应用
C#函数的基础应用 函数之前的知识回顾 数据类型--变量常量--运算符表达式--语句(顺序,分支,循环)--数组--函数 程序里的函数:能完成一个相对独立功能的代码模块. 数学里的函数:高度抽象. 函 ...
- nodejs 模块变量 应用
exports.allcodeandname=(function(){ var fs = require('fs'); var data = fs.readFileSync(__dirname+'/a ...
- RxSwift学习笔记之Subject
本文为原创文章,转载请标明出处 AsyncSubject 一个AsyncSubject只在原始Observable完成后,发射来自原始Observable的最后一个值.它会把这最后一个值发射给任何后续 ...
- php结合Redis实现高并发下的秒杀抢购功能
实现思路 准备两个队列A和B,假设A队列的名称为stock,用于存放商品总库存信息,B队列的名称为users,用于存放抢购成功后的用户信息.每当有用户进行抢购操作时,先从A队列弹出一个元素,如果该元素 ...