Remove Duplicates from Sorted Array II
来源:互联网 发布:mac系统最新版本10.13 编辑:程序博客网 时间:2024/06/07 13:12
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){ if (A.length <= 2){ return A.length; }int i = 0;int j = 0;int count = 0;int temp = A[0] - 1;while (i < A.length){if ((i + 1 < A.length) && (A[i] == A[i + 1])){i ++;}j = i + 1;while ((j < A.length) && (A[i] == A[j])){A[j] = temp; count ++;j ++;}i = j;}i = 0;if (count > 0){while (i < A.length){while (A[i] != temp){i ++;}j = i + 1;while ((j < A.length) && (A[j] == temp)){j ++;}if (j == A.length){break;}A[i] = A[j];A[j] = temp;i ++;}}return A.length - count;}}
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
- UGUI中Camera属性的简单介绍
- 杭电ACM 1002:A+B Problem II
- (译文)12个简单(但强大)的JavaScript技巧(一)
- Java Int unpacking(分解)packing
- 操作系统学习笔记:死锁
- Remove Duplicates from Sorted Array II
- 蓝桥杯——李白打酒(正式版)
- POJ 3233 - Matrix Power Series(等比矩阵求和)
- MySQL基础之学习笔记
- delete and rebuild 执行 link.exe 时出错的解决方案
- 论SOA架构的几种主要开发方式
- java多进程 -CD7-孙鑫-(5)
- 欢迎使用CSDN-markdown编辑器
- Mac下Wireshark的安装