forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveDuplicatesFromSortedArray.java
38 lines (31 loc) · 1.18 KB
/
RemoveDuplicatesFromSortedArray.java
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
// Source : https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
// Author : Tianming Cao
// Date : 2018-02-02
/**********************************************************************************
* Implement the following operations of a stack using queues.
* 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 by modifying the input array in-place with O(1) extra memory.
* Example:
* Given nums = [1,1,2],
* Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
* It doesn't matter what you leave beyond the new length.
*
**********************************************************************************/
package removeDuplicatesFromSortedArray;
public class RemoveDuplicatesFromSortedArray {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int n = nums.length;
int len = 0;
for (int i = 1; i < n; i++) {
if (nums[i] != nums[len]) {
nums[len + 1] = nums[i];
len++;
}
}
return len + 1;
}
}