/**
* Source : https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
*
*
* Follow up for "Remove Duplicates":
* What if duplicates are allowed at most twice?
*
* For example,
* Given sorted array A = [1,1,1,2,2,3],
*
* Your function should return length = 5, and A is now [1,1,2,2,3].
*
*/
public class RemoveDuplicates2 { /**
* 只需要找到不重复的元素个数,不重复的定义是:小于等于2个
* 遍历数组
*
* @param arr
* @return
*/
public int remove (int[] arr) {
if (arr.length <= 2) {
return arr.length;
}
int count = 1;
int pos = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i-1]) {
count ++;
} else {
if (count > 2) {
pos += 2;
} else {
pos += count;
}
count = 1;
}
}
if (count > 2) {
pos += 2;
} else {
pos += count;
}
return pos;
} /**
* 出现3次及以上才算重复
* 第一次出现记一次数。第二次出现记一次数
*
* @param arr
* @return
*/
public int remove1 (int[] arr) {
if (arr.length <= 2) {
return arr.length;
}
int count = 1;
int pos = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i-1]) {
count ++;
if (count == 2) {
pos ++;
}
} else {
count = 1;
pos ++;
}
}
return pos +1;
} public static void main(String[] args) {
RemoveDuplicates2 removeDuplicates = new RemoveDuplicates2();
int[] arr0 = new int[]{1,1,1};
int[] arr = new int[]{1,1,2};
int[] arr1 = new int[]{1,1,1,2};
int[] arr2 = new int[]{1,1,22,22,22,33};
int[] arr3 = new int[]{1,1,1,1,1,1};
System.out.println(removeDuplicates.remove(arr0) + "----" + removeDuplicates.remove1(arr0));
System.out.println(removeDuplicates.remove(arr) + "----" + removeDuplicates.remove1(arr));
System.out.println(removeDuplicates.remove(arr1) + "----" + removeDuplicates.remove1(arr1));
System.out.println(removeDuplicates.remove(arr2) + "----" + removeDuplicates.remove1(arr2));
System.out.println(removeDuplicates.remove(arr3) + "----" + removeDuplicates.remove1(arr3));
}
}

leetcode — remove-duplicates-from-sorted-array-ii的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  2. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  3. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  4. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  5. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  6. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

  7. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

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

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

  9. 【leetcode】Remove Duplicates from Sorted Array II

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

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

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

随机推荐

  1. MongoDB 学习使用

    博客教程: https://jingyan.baidu.com/article/dca1fa6f0428a4f1a440522e.html

  2. C#三目运算符

    在编写项目的时候,会经常用到 if else 判断语句,但有些简单的判断或赋值,可以通过三目运算符来完成! 例如: int sex=0; string sexText=""; if ...

  3. java中的异常类

    Java中的异常: 1. Throwable是所有异常的根,java.lang.Throwable Throwable包含了错误(Error)和异常(Exception),Exception又包含了运 ...

  4. 【尺取法】Jurisdiction Disenchantment

    [尺取法]Jurisdiction Disenchantment PROBLEM 时间限制: 1 Sec 内存限制: 128 MB 题目描述 The Super League of Paragons ...

  5. Prometheus 企业微信报警/inhibit抑制 /静默(二)

    创建企业微信应用 注册企业微信:访问https://work.weixin.qq.com/,注册企业,随便填,不需要认证 创建应用 创建告警配置 vim /usr/local/prometheus-2 ...

  6. django 标签的使用

    首先重建一个common的app 然后创建__init__使common成为一个包   注意templatetags 名字使固定的 并在下面创建一个名字为fitter的过滤器 注册过滤器app htm ...

  7. dx.jar文件问题,有没有同学知道怎么解决呀,这一步没法解决,后面就没办法跟着做了

     Java Code  123456789101112 dx.jar文件问题,有没有同学知道怎么解决呀,这一步没法解决 - test] Unknown error: Unable to build:  ...

  8. MySQL 分区建索引

    200 ? "200px" : this.width)!important;} --> 介绍 mysql分区后每个分区成了独立的文件,虽然从逻辑上还是一张表其实已经分成了多张 ...

  9. 1.8 Double-Opening and Virtual Machine

    Since plug-in will be replaced by RN as following years, what is the future of plug-in? the answer i ...

  10. 基于Java的HashMap和HashSet实现

    一.Map接口类: import java.util.Iterator; public interface IMap<K, V> { /* 清除所有键值对 */ void clear(); ...