-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1346.java
More file actions
47 lines (42 loc) · 1.36 KB
/
LeetCode1346.java
File metadata and controls
47 lines (42 loc) · 1.36 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
import java.util.HashSet;
public class LeetCode1346 {
public static void main(String[] args) {
// 输入:arr = [10,2,5,3]
// 输出:true
System.out.println(new Solution1346().checkIfExist(new int[] { 10, 2, 5, 3 }));
// 输入:arr = [7,1,14,11]
// 输出:true
System.out.println(new Solution1346().checkIfExist(new int[] { 7, 1, 14, 11 }));
// 输入:arr = [3,1,7,11]
// 输出:false
System.out.println(new Solution1346().checkIfExist(new int[] { 3, 1, 7, 11 }));
// 输入:arr = [-10,12,-20,-8,15]
// 输出:true
System.out.println(new Solution1346().checkIfExist(new int[] { -10, 12, -20, -8, 15 }));
}
}
class Solution1346 {
public boolean checkIfExist(int[] arr) {
HashSet<Integer> set = new HashSet<Integer>();
int countZero = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
countZero++;
continue;
}
set.add(arr[i]);
if (set.contains(arr[i] * 2)) {
return true;
}
}
if (countZero >= 2) {
return true;
}
for (int i = 0; i < arr.length; i++) {
if (set.contains(arr[i] * 2)) {
return true;
}
}
return false;
}
}