forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest06.java
More file actions
35 lines (33 loc) · 911 Bytes
/
test06.java
File metadata and controls
35 lines (33 loc) · 911 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
package byteDance;
/**
* Created by lizeyang on 2020/5/12.
* 一个数据先递增再递减,找出数组不重复的个数
*/
public class test06 {
//双指针法,每次取最小元素移动,不使用额外空间,O(n)
public static int test(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int l = 0, r = nums.length - 1;
int count = 0;
while (l <= r) {
if (nums[l] < nums[r]) {
count++;
l++;
} else if (nums[l] > nums[r]) {
count++;
r--;
} else {
l++;
r--;
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] nums = {1, 3, 5, 6, 7, 8, 9, 8, 6, 4, 3, 1,0};
System.out.println(test(nums));
}
}