/**
* 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. [AGC017D]Game on Tree

    [AGC017D]Game on Tree 题目大意: 一棵\(n(n\le10^5)\)个结点的树.A和B轮流进行游戏,A先手.每次删掉一棵子树,根结点不能删.最先不能操作的人输,问最后谁赢. 思路 ...

  2. Linux进阶命令用法

    1.tr命令 可以对来自标准输入的字符进行替换.压缩和删除.它可以将一组字符变成另一组字符 选项 -c或——complerment:取代所有不属于第一字符集的字符: -d或——delete:删除所有属 ...

  3. CSS-单位em 和 rem

    1,em单位(css3新增单位) em:就是一种长度单位,它是参照当前元素的字号,如果没有设置,就参照父容器,一直到浏览器的默认字号大小 em 是相对长度单位(参照父元素),其参照当前元素字号大小,如 ...

  4. dedecms 文章根据 权重排序

    原文链接 一: dede:list   标签 找到 /include/arc.listview.class.php if($orderby=="senddate" || $orde ...

  5. 接口自动化项目搭建(Java+testng+maven+git+springboot)

    自动化测试: https://www.bilibili.com/video/av31078661?from=search&seid=16551153777362561361 一工具准备 二 环 ...

  6. phpstorm 断点调试 傻瓜教程

    前言: 简单介绍下为什么要用断点调试,很多人说我在代码调试的部位用var_dump 或者 exit 或者print_r来进行断点,但是当项目足够大的时候这样的做法就比较费时费力,因为你断点后需要删除原 ...

  7. CSS面试细节整理(二)

    5.css盒模型: CSS 框模型 (Box Model) 规定了元素框处理元素内容.内边距.边框 和 外边距 的方式

  8. php-cgi占用太多cpu资源而导致服务器响应过慢

    服务器环境:redhat linux 5.5 , nginx ,  phpfastcgi 在此环境下,一般php-cgi运行是非常稳定的,但也遇到过php-cgi占用太多cpu资源而导致服务器响应过慢 ...

  9. [Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  10. [Swift]LeetCode455. 分发饼干 | Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...