-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
39 lines (32 loc) · 870 Bytes
/
Copy pathRemoveDuplicates.java
File metadata and controls
39 lines (32 loc) · 870 Bytes
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
package com.lga.algorithm.tag.homework.Week_01;
import org.junit.Assert;
import org.junit.Test;
/**
* 26. 删除排序数组中的重复项
*
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
*/
public class RemoveDuplicates {
/**
* 时间复杂度O(n)
* 空间复杂度O(1)
* @param nums
* @return
*/
public int removeDuplicates(int[] nums) {
if(nums == null) return 0;
int left = 0;
int right = 1;
while (right < nums.length) {
if (nums[left] !=nums[right])
nums[++left] = nums[right];
right++;
}
return left + 1;
}
@Test
public void test1() {
Assert.assertTrue(2 == removeDuplicates(new int[]{1,1,2}));
Assert.assertTrue(5 == removeDuplicates(new int[]{0,0,1,1,1,2,2,3,3,4}));
}
}