반응형
https://leetcode.com/problems/path-crossing/
이미 방문한 좌표를 기억해줍니다.
class Solution {
public:
bool isPathCrossing(string path) {
unordered_set<int> v;
int x = 0, y = 0;
v.insert(x * 10000 + y);
for(char p : path) {
if(p == 'N') {
x--;
} else if(p == 'S') {
x++;
} else if(p == 'E') {
y++;
} else {
y--;
}
if(v.find(x * 10000 + y) != v.end()) return true;
v.insert(x * 10000 + y);
}
return false;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 91. Decode Ways (0) | 2023.12.25 |
---|---|
LeetCode 1758. Minimum Changes To Make Alternating Binary String (0) | 2023.12.25 |
LeetCode 1422. Maximum Score After Splitting a String (1) | 2023.12.23 |
LeetCode 1637. Widest Vertical Area Between Two Points Containing No Points (0) | 2023.12.21 |
LeetCode 1913. Maximum Product Difference Between Two Pairs (0) | 2023.12.18 |