/**
* 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. 2018-4-5-MEMS

    微机电系统,研究生在学习纳米操作方面的知识的时候了解过一些,有时间的话写点东西温故知新.

  2. Android 动画 (1) 基础

    背景 坑, 最近打算在recyclerview item上加一个带动画的button,结果button无法连续点击,还以为是动画是同步的,必须要结束之后才能开始另一个动画,后来去掉recylervie ...

  3. Git 实用指南

    传送门: https://mp.weixin.qq.com/s?__biz=MzI3MzgxNDY2MQ==&mid=2247484159&idx=1&sn=2d28513ef ...

  4. Linux结束进程到底有多少种方法?

    我们经常在Linux里使用kill命令来结束某后台进程.但kill命令实际上是向进程发送信号,并且有多种信号.终止运行一个程序只是其中一个信号而已.kill是根据进程号发送信号的,而另一个工具kill ...

  5. 用idea 创建一个spring小demo,基于xml文件配置

    1.首先,File->new->project ,进入新增项目页面 或者在 2.勾选spring,然后点击下一步 3.修改项目名称和项目位置 进入页面后 5.创建一个spring配置文件 ...

  6. 用kattle将数据从SQLserver中导入到vertica中

    今天简单的学习了一下ETL工具kattle了,只是简单的上手,不过这也已经够我去做POC了. 首先大体介绍一下kattle,Kettle是一款国外开源的ETL工具,纯java编写,可以在Window. ...

  7. javaWeb+servlet+mysql实现简单的企业员工管理系统

    企业员工信息管理系统 一.源码描述       本程序为企业员工信息管理系统.是javaEE一个系统,主要实现登录功能和两个模块信息的增删改查.可以作为JAVAweb学习,也可在原有基础上进行深一步的 ...

  8. js根据顺序加载,有依赖关系

    function loadScript(url, callback) { var script = document.createElement("script"); script ...

  9. [Swift]LeetCode700. 二叉搜索树中的搜索 | Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  10. [Swift]LeetCode983. 最低票价 | Minimum Cost For Tickets

    In a country popular for train travel, you have planned some train travelling one year in advance.  ...