[leet code] Remove Duplicates from Sorted Array I & II
来源:互联网 发布:如何复制淘宝商品链接 编辑:程序博客网 时间:2024/05/25 20:01
Remove Duplicates from Sorted Array I :
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
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]
.
===========
Analysis:
Key points of of these 2 problems: use 2 pointers to overwrite the original array according to the problem requirement. One pointer is for the original array, +1 every time. Another pointer is for the overwritten array, +1 when current number not duplicated or not duplicated 2 times (problem II).
Remove Duplicates from Sorted Array I :
public class Solution { public int removeDuplicates(int[] A) { // exception case if (A.length <= 1) return A.length; int oPointer=1; // pointer for original array int nPointer=0; // pointer for overwritten array while (oPointer<A.length){ if(A[oPointer] == A[nPointer]) oPointer++; // duplicate case else{ nPointer++; A[nPointer] = A[oPointer]; oPointer++; } } return nPointer+1; }}
Remove Duplicates from Sorted Array II:
public class Solution {public int removeDuplicates(int[] A) { if (A.length<3) return A.length; int nPointer = 1; // pointer for overwritten array int oPointer = 2; // pointer for original array while(oPointer<A.length){ if(A[oPointer]==A[nPointer] && A[oPointer]==A[nPointer-1]) oPointer++; else{ nPointer++; A[nPointer] = A[oPointer]; oPointer++; } } return nPointer+1; }}
- [leet code] Remove Duplicates from Sorted Array I & II
- 【Leet Code】80. Remove Duplicates from Sorted Array II---Medium
- [leet code] Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array I, II
- 【Leet Code】26. Remove Duplicates from Sorted Array---Easy
- 【LEET-CODE】26. Remove Duplicates from Sorted Array
- [leet code] Remove Duplicates from Sorted List II
- Leet -- Remove Duplicates from Sorted Array
- [leet code] Remove Duplicates from Sorted List
- CODE 53: Remove Duplicates from Sorted Array II
- leetcode 日经贴,Cpp code -Remove Duplicates from Sorted Array II
- leet code 第25-26题Remove Duplicates from Sorted Array & Remove Element
- LeetCode:Remove Duplicates from Sorted Array I&&II
- LeetCode题解:Remove Duplicates from Sorted Array I and II
- 【LeetCode】Remove Duplicates from Sorted Array I && II
- LeetCode Solutions : Remove Duplicates from Sorted Array I & II
- Leetcode: Remove Duplicates from Sorted Array I and II
- L2:08 - Remove Duplicates from Sorted Array I, II
- HDU 1257(贪心;动态规划(最长上升子序列))
- WEBLOGIC部署 详细文档--贴图
- type - 显示命令的类型
- Sereja and Stairs
- 快速把web项目部署到weblogic上
- [leet code] Remove Duplicates from Sorted Array I & II
- Object类中的方法
- 字符串分割函数---strtok和strsep函数
- 交际的soften法则
- Android应用程序签名详解
- (vimchrome)以 vim 的方式来使用 chrome 浏览器(利用 vimium 插件)
- android开机自动登录
- linux下终端查看时间和修改时间
- POJ 1129