forked from lilong-dream/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path026RemoveDuplicatesfromSortedArray.java
More file actions
43 lines (37 loc) · 1.3 KB
/
026RemoveDuplicatesfromSortedArray.java
File metadata and controls
43 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Author: Li Long, [email protected]
// Date: Apr 17, 2014
// Source: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
// Analysis: http://blog.csdn.net/lilong_dream/article/details/19757047
// 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].
public class RemoveDuplicatesfromSortedArray {
public int removeDuplicates(int[] A) {
if(A.length == 0) {
return 0;
}
if(A.length == 1) {
return 1;
}
int index = 0;
for(int i = 1; i < A.length; ++i) {
if(A[i] != A[index]) {
++index;
A[index] = A[i];
}
}
return index + 1;
}
public static void main(String[] args) {
RemoveDuplicatesfromSortedArray slt = new RemoveDuplicatesfromSortedArray();
int[] A = {1, 1, 2, 3, 4, 4, 5};
int len = slt.removeDuplicates(A);
System.out.println(len);
for(int i = 0; i < len; ++i) {
System.out.print(A[i] + " ");
}
}
}