Remove Duplicates from Sorted Array II
来源:互联网 发布:怎么有效瘦脸 知乎 编辑:程序博客网 时间:2024/06/08 18:42
Q:
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]
.
Solution:
j is the tail of the sub array.
public class Solution { public int removeDuplicates(int[] A) { if (A.length < 3) return A.length; int j = 1; int dup = 1; for (int i = 1; i < A.length; i++) { if (A[i-1] == A[i]) { if (dup < 2) { dup++; A[j] = A[i]; j++; } else continue; } else { if (dup >= 2) { A[j] = A[i]; dup = 1; j++; } else { A[j] = A[i]; j++; } } } if (j < A.length) { A[j] = '\0'; } return j; }}
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
- 字符设备驱动程序
- Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]" 博客分类: Exception 内存溢出av
- 我的第一个程序
- Win32汇编编译程序总结
- 疑问
- Remove Duplicates from Sorted Array II
- Microsoft .NET Framework 4 安装 1603错误的解决
- MySQL之快速入门
- eclipse编辑器调节背景颜色保护视力
- @SuppressLint("NewApi") 或者 @TargeApi(9)区别和注意事项
- 关于MFC Listcontrol控件分类显示的问题
- 第七周项目一电阻串联
- AE中通过UID使用getLayer
- 计算机中的幂等性