-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution2.cpp
33 lines (32 loc) · 900 Bytes
/
Solution2.cpp
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
class Solution {
public:
int minimumOperations(vector<int>& nums, int start, int goal) {
queue<int> q{{start}};
vector<bool> vis(1001);
int ans = 0;
while (!q.empty()) {
++ans;
for (int n = q.size(); n > 0; --n) {
int x = q.front();
q.pop();
for (int y : next(nums, x)) {
if (y == goal) return ans;
if (y >= 0 && y <= 1000 && !vis[y]) {
vis[y] = true;
q.push(y);
}
}
}
}
return -1;
}
vector<int> next(vector<int>& nums, int x) {
vector<int> res;
for (int num : nums) {
res.push_back(x + num);
res.push_back(x - num);
res.push_back(x ^ num);
}
return res;
}
};