-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
33 lines (29 loc) · 918 Bytes
/
Solution.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
package ds.pointer.leetcode26;
import java.util.Arrays;
/**
* 删除排序数组中的重复项
* LeetCode 26 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
*
* @author yangyi 2020年12月16日15:35:00
*/
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int slow = 0, fast = 0;
while (fast < nums.length) {
if (nums[slow] != nums[fast]) {
slow++;
nums[slow] = nums[fast];
}
fast++;
}
return slow + 1;
}
public static void main(String[] args) {
int[] a = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
Solution removeDuplicates = new Solution();
System.out.println(Arrays.toString(a) + "去掉重复元素的长度为:" + removeDuplicates.removeDuplicates(a));
}
}