|

分类

leetcode 

|

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

by StefanPochmann

思路是将当前遍历元素与新数组的倒数第二个元素比较,只有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的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. [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 ...

  3. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. [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% ...

  5. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  6. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  7. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

  8. [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 ...

  9. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. 计算文本长度-boundingRectWithSize

    - (void)viewDidLoad {    [super viewDidLoad]; //新建lable控件 UILabel *lable=[[UILabel alloc]init]; labl ...

  2. 如何将本地未提交的更改合并到另一个Git分支?

    如何在Git中执行以下操作? 我当前的分支是branch1,我做了一些本地更改. 但是,我现在意识到我实际上是要将这些更改应用于branch2. 有没有办法应用/合并这些更改,以便它们成为branch ...

  3. 用hash存数组|得地址|取地址

    #!/usr/bin/perl -w use strict; my %hash = %{&collect};my $arr_ad=$hash{'a'};print "$arr_ad\ ...

  4. 编译原理_P1004

    龙书相关知识点总结 //*************************引论***********************************// 1. 编译器(compiler):从一中语言( ...

  5. 吴裕雄--天生自然python机器学习:使用朴素贝叶斯过滤垃圾邮件

    使用朴素贝叶斯解决一些现实生活中 的问题时,需要先从文本内容得到字符串列表,然后生成词向量. 准备数据:切分文本 测试算法:使用朴素贝叶斯进行交叉验证 文件解析及完整的垃圾邮件测试函数 def cre ...

  6. 算法笔记4.3递归 问题 B: 数列

    题目描述 编写一个求斐波那契数列的递归函数,输入n 值,使用该递归函数,输出如下图形(参见样例). 输入 输入第一行为样例数m,接下来有m行每行一个整数n,n不超过10. 输出 对应每个样例输出要求的 ...

  7. 对数据集进行最优分箱和WOE转换

    对数据集分箱的方式三种,等宽等频最优,下面介绍对数据集进行最优分箱,分箱的其他介绍可以查看其他的博文,具体在这就不细说了: 大体步骤: 加载数据: 遍历所有的feature, 分别处理离散和连续特征: ...

  8. Java集合源码剖析——ArrayList源码剖析

    ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下,多线 ...

  9. ddt-python测试数据驱动工具(转载)

    背景 python 的unittest 没有自带数据驱动功能. 所以如果使用unittest,同时又想使用数据驱动,那么就可以使用DDT来完成. DDT是 “Data-Driven Tests”的缩写 ...

  10. 《C 程序设计语言》练习1-3

    #include<stdio.h> /*当fahr=0,20,...,300时,打印华氏温度与摄氏温度对照表; 浮点数版本*/ main () { float fahr,celsius; ...