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 ...
随机推荐
- BZOJ.4144.[AMPPZ2014]Petrol(Kruskal重构树)
BZOJ 看别人代码的时候发现哪一步都很眼熟,突然想起来,就在四个月前我好像看过还给别人讲过?mmp=v= 果然不写写就是容易忘.写了好歹忘了的时候还能复习呢(虽然和看别人的好像也没多少差别?). 首 ...
- django用户认证系统——拓展 User 模型
Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...
- 用python写一个非常简单的QQ轰炸机
闲的没事,就想写一个QQ轰炸机,按照我最初的想法,这程序要根据我输入的QQ号进行轰炸,网上搜了一下,发现网上的案列略复杂,就想着自己写一个算了.. 思路:所谓轰炸机,就是给某个人发很多信息,一直刷屏, ...
- Bad Hair Day [POJ3250] [单调栈 或 二分+RMQ]
题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80 ...
- MyBatis 缓存机制
Mybatis 有两级缓存: 一级缓存: 也称为本地缓存,SqlSession级别的缓存.一级缓存是一直开启的: 与数据库同一次会话期间查询到的数据会放在本地缓存中,以后如果需要获取相同的数据,直接从 ...
- spring-cloud-Zuul学习(四)【中级】--自定义zuul Filter详解【重新定义spring cloud实践】
实现自定义zuul Filter 方法很简单,只要继承ZuulFilter跟加入到spring IOC容器即可,zuulFilter是一个抽象类,里面包含以下方法需要我们实现: String fi ...
- 如何判断dt中所有行的状态并有选择的移除
DataRow drFocusedRow = dtCentralizerOptimalSelection.Rows[gvCentralizerOptimalSelection.FocusedRowHa ...
- 业务配置开发平台qMISPlat 2.0 产品介绍
qMISPlat是什么 qMISPlat(业务配置开发平台)是一套基于.net core 2.0.跨平台的,面向开发人员和具有一定技术水平的业务人员使用的业务配置开发平台.基于此平台您只需通过配置和少 ...
- 中文转拼音without CJK
Xamarin写Android程序时,通常要使用按中文首字母分组显示(如通讯录) . 于是需要被迫包含CJK,不过包含后包肯定是会变大的,于是....自己写了一个硬枚举的中文转拼音的类. 原理是这样的 ...
- 常见的java设计模式
单例模式 简单点说,就是一个应用程序中,某个类的实例对象只有一个,你没有办法去new,因为构造器是被private修饰的,一般通过getInstance()的方法来获取它们的实例. getInstan ...