## Saturday, November 15, 2014

### [LeetCode] Remove Duplicates from Sorted Array I, II

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]`.

Remove Duplicates from Sorted Array II
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]`.

(1) A[end]=A[i]，A[i]为重复数字，跳过。
(2) A[end]!=A[i]，将A[i]放到A[end+1]位置，end++

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14``` ```class Solution { public: int removeDuplicates(int A[], int n) { if(n<2) return n; int end=0; for(int i=1; i

 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```class Solution { public: int removeDuplicates(int A[], int n) { if(n<3) return n; int end = 1; for(int i=2; i

#### 1 comment:

1. Really someone could think of this method?