leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1496.cpp (647B)
0 struct Hash { 1 size_t operator()(const tuple<int, int> &t) const { return (get<0>(t) << 16) + get<1>(t); } 2 }; 3 4 class Solution { 5 public: 6 bool isPathCrossing(const string &path) const { 7 static const pair<int, int> moves[128] = { 8 ['N'] = {-1, 0}, ['S'] = {1, 0}, ['E'] = {0, 1}, ['W'] = {0, -1}}; 9 unordered_set<tuple<int, int>, Hash> us; 10 int x = 0, y = 0; 11 us.insert({0, 0}); 12 for (const char c : path) { 13 x += moves[c].first; 14 y += moves[c].second; 15 if (us.count({x, y})) return true; 16 us.insert({x, y}); 17 } 18 return false; 19 } 20 };