forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
34 lines (33 loc) · 974 Bytes
/
Solution2.java
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
class Solution {
public int minimumOperations(int[] nums, int start, int goal) {
Deque<Integer> q = new ArrayDeque<>();
q.offer(start);
boolean[] vis = new boolean[1001];
int ans = 0;
while (!q.isEmpty()) {
++ans;
for (int n = q.size(); n > 0; --n) {
int x = q.poll();
for (int y : next(nums, x)) {
if (y == goal) {
return ans;
}
if (y >= 0 && y <= 1000 && !vis[y]) {
vis[y] = true;
q.offer(y);
}
}
}
}
return -1;
}
private List<Integer> next(int[] nums, int x) {
List<Integer> res = new ArrayList<>();
for (int num : nums) {
res.add(x + num);
res.add(x - num);
res.add(x ^ num);
}
return res;
}
}