-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_Path_crossing.cpp
More file actions
56 lines (34 loc) · 1.27 KB
/
08_Path_crossing.cpp
File metadata and controls
56 lines (34 loc) · 1.27 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
48
49
50
51
52
53
54
55
56
// Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.
// Return true if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false otherwise.
// Example 1:
// Input: path = "NES"
// Output: false
// Explanation: Notice that the path doesn't cross any point more than once.
// Example 2:
// Input: path = "NESWW"
// Output: true
// Explanation: Notice that the path visits the origin twice.
// Optimal Solution
class Solution {
public:
bool isPathCrossing(string path) {
unordered_map<char, pair<int,int>> moves = {
{'N', {0, 1}},
{'S', {0, -1}},
{'E', {1, 0}},
{'W', {-1, 0}}
};
int x = 0, y = 0;
set<pair<int,int>> visited;
visited.insert({0, 0});
for (auto &ch : path) {
x += moves[ch].first;
y += moves[ch].second;
if (visited.count({x, y})) {
return true;
}
visited.insert({x, y});
}
return false;
}
};