leetcode — remove-duplicates-from-sorted-array-ii
/**
* 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的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- Windows linux子系统 使用说明
1.安装 linux 子系统 2.应用商店安装ubuntu 3.为了方便可以配置成默认登陆root账户 Ubuntu config –default-user root 4. 安装完毕 5.安 ...
- [CF1138B]Circus
Description: 给你2个长度为n的01串 从中选出\(n/2\)个,使得选出的数中第一排1的个数等于未选出数中第二排1的个数 输出一种方案即可,没有输出-1 Hint: \(n \le 50 ...
- 简单的NIO使用实例
public class ThreadTest_2 { public static void main(String[] args) { Thread downloaderThread = null; ...
- Chapter 5 : Control Structures 2 : Repetition
import java.util.*; import java.io.*; public class Loop { static Scanner console = new Scanner(Syste ...
- iphone 屏蔽系统自动更新,消除设置上的小红点
苹果ios系统的更新频率大家应该都知道,一般来说1个月就会来次更新.这一点让很多人讨厌.主要原因还是iPhone会自动下载更新包,然后一直不停地提示你是否安装更新,问题是我们还找不到关闭提醒和关闭自动 ...
- appium定位
一.链接基本信息 二.在appium界面中 三,定位 三.通过ui automator viewer抓取手机页面元素,点击红框按钮会抓取当前手机界面app全部元素;路径在sdk>tools下面的 ...
- js将一个具有相同键值对的一维数组转换成二维数组
这两天,一个前端朋友在面试的笔试过程中遇到了一道类似于"用js实现将一个具有相同code值的一维数组转换成相同code值在一起的二维数组"的题目.他面试过后,把这个问题抛给了我,问 ...
- java课程之团队开发冲刺阶段1.3
一.总结昨天进度 1.完成任务指标,但是有些问题没有得到根本上解决,只是换方式解决了 2.使用时间:6小时 二.遇到的困难 1.在设置AlertDialog弹窗组件的时候,没有办法获取选中值,再实验多 ...
- Spring Cloud 组件 —— eureka
官方文档,Spring Cloud 对其封装,Spring Cloud eureka 文档
- f12 headers 变字典快捷方式