-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc1306
More file actions
31 lines (26 loc) · 853 Bytes
/
lc1306
File metadata and controls
31 lines (26 loc) · 853 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
class Solution {
public:
bool canReach(vector<int>& arr, int start) {
unordered_set<int> visited;
queue<int> q;
visited.insert(start);
q.push(start);
while(!q.empty()) {
int tempIndx = q.front();
q.pop();
if(arr[tempIndx] == 0)
return true;
int leftIndx = tempIndx - arr[tempIndx];
int rightIndx = tempIndx + arr[tempIndx];
if(leftIndx >= 0 && !visited.count(leftIndx)) {
q.push(leftIndx);
visited.insert(leftIndx);
}
if(rightIndx < arr.size() && !visited.count(rightIndx)) {
q.push(rightIndx);
visited.insert(rightIndx);
}
}
return false;
}
};