forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
38 lines (34 loc) · 1015 Bytes
/
RemoveDuplicates.java
File metadata and controls
38 lines (34 loc) · 1015 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
package normal; /**
* @program JavaBooks
* @description: 26.删除排序数组中的重复项
* @author: mf
* @create: 2019/11/03 17:10
*/
/**
* 题目:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
* 难度:easy
* 类型:数组
*/
import java.util.Arrays;
/**
* 题目:
* 给定数组 nums = [1,1,2],
* 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
* 你不需要考虑数组中超出新长度后面的元素。
*/
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
System.out.println(removeDuplicates(arr));
System.out.println(Arrays.toString(arr));
}
private static int removeDuplicates(int[] arr) {
int p = 0; // 符合条件指针
for (int i = 1; i < arr.length; i++) {
if (arr[p] != arr[i]) {
arr[++p] = arr[i];
}
}
return p + 1;
}
}