-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDup.java
More file actions
64 lines (59 loc) · 1.82 KB
/
RemoveDup.java
File metadata and controls
64 lines (59 loc) · 1.82 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package LeetCodeOJ;
/* 26.Remove Duplicates from Sorted Array from leetCode
* [1,1,2]
* Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
*/
//ʱ�临�Ӷ�o(n)
public class RemoveDup {
public int func(int[] nums) {
int tmp = nums[0];
int len = nums.length;
int count = 0;
int j = 0;
for (int i = 1; i < nums.length; i++) {
while (i < nums.length && nums[i++] == tmp) {
len--;
}
if (--i < nums.length) {
nums[++j] = nums[i];
tmp = nums[i];
}
}
return len;
}
/*
* �������������Ȼ�ȵĵ�һ������Ҫ��ࣺʱ�临�Ӷ�o(n)
*/
public int len(int[] nums) {
int index = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[index] != nums[i]) {
// 只保留与index不相等的元素,并且将++index的值为当前nums[i]
// 很简洁
nums[++index] = nums[i];
}
}
// ��ӡ�������ʾ����ʱ������
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i]);
}
System.out.println();
return index + 1;
}
public static int fun() {
int count = 0;
int num1 = 12345;
while (num1 != 0) {
count++;
num1 &= (num1 - 1);
}
return count;
}
public static void main(String[] args) {
int[] n = { -999, -999, -998, -998, -997, -997, -996, -996, -995, -995,
-994, -994 };
// int len = new RemoveDup().func(n);
int len = new RemoveDup().len(n);
System.out.println(len);
}
}