CODE 53: Remove Duplicates from Sorted Array II
来源:互联网 发布:构件柱模板的算法 编辑:程序博客网 时间:2024/06/06 14:06
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 int removeDuplicates(int[] A) {// IMPORTANT: Please reset any member data you declared, as// the same Solution instance will be reused for each test case.if (A.length <= 0) {return 0;}int length = A.length;int cur = 0;int nxt = 1;boolean morethan2 = false;for (; nxt < A.length; nxt++) {if (A[nxt] != A[nxt - 1]) {A[cur] = A[nxt - 1];cur++;morethan2 = false;} else if (!morethan2) {A[cur] = A[nxt - 1];cur++;morethan2 = true;} else {length--;}}A[cur] = A[nxt - 1];return length;}
- CODE 53: Remove Duplicates from Sorted Array II
- [leet code] Remove Duplicates from Sorted Array I & II
- leetcode 日经贴,Cpp code -Remove Duplicates from Sorted Array II
- 【Leet Code】80. Remove Duplicates from Sorted Array II---Medium
- CODE 106: Remove Duplicates from Sorted Array
- [leet code] Remove Duplicates from Sorted Array
- 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
- Implementing Runtime Object Messaging
- UVA 10201 Adventures in Moving - Part IV 车站加油 dp
- Linux系统操作技巧集萃
- CMD的常用命令
- 用busybox作简单的文件系统(一)
- CODE 53: Remove Duplicates from Sorted Array II
- 2013 EMC新产品网络发布大会[演示文档下载]
- 函数与程序结构&数组与指针(C编程语言)
- RAM R/W TEST FAILED PRESS F1 to resume
- 搜索与优化2优化方法总结
- 第12周项目4--任务1--猜数字游戏
- Document root element "beans", must match DOCTYPE root "null"
- 数据结构之哈希表
- vi学习笔记