-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest05.java
More file actions
39 lines (37 loc) · 1012 Bytes
/
test05.java
File metadata and controls
39 lines (37 loc) · 1012 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 byteDance;
/**
* Created by lizeyang on 2020/5/12.
* 排序数组,平方后判断有多少不重复的数
*/
public class test05 {
//前后指针,O(n)
public static int test(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int l = 0;
int r = nums.length - 1;
int count = 0;
while (l < r) {
int tmp = Math.abs(nums[l]);
if (Math.abs(nums[l]) < Math.abs(nums[r])) {
if (tmp != nums[l]) {
count++;
tmp = nums[l];
}
l++;
} else {
if (tmp != Math.abs(nums[r])) {
count++;
tmp = nums[r];
}
r--;
}
}
return count;
}
public static void main(String[] args) {
int[] nums = {-10, -10, -5, 0, 1, 2, 5, 8, 10, 10, 10};
System.out.println(test(nums));
}
}