Remove Duplicates from Sorted Array II
来源:互联网 发布:多功能网络表 编辑:程序博客网 时间:2024/06/17 02:25
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 Solution { public int removeDuplicates(int[] A) { Integer prev = null; // last value int count = 0; // number of continuous occurrences of last value int index = 0; // points to index to place next qualified element for (int i = 0; i < A.length; i++) { int candidate = A[i]; if (prev != null && prev == candidate && count == 2) continue; A[index++] = A[i]; // reset count if a new value is encountered if (prev == null || candidate != prev) { prev = candidate; count = 1; } else count++; } return index;}}
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- NANDFlash结构图解
- [Shell Command] MySQL集群全局一览表【源码】
- Java注解@interface (入门)
- online_judge_1046
- 百度地图开发
- Remove Duplicates from Sorted Array II
- Android.mk简介
- android canva画人
- online_judge_1047
- Android UI开发第三十五篇——AppCompat实现Action Bar
- Office 365 – SharePoint 2013 Online 与Office相关的应用
- MySQL 没初始化
- Interpolate-Curve
- Linux终端高手必备神器