leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1138.cpp (758B)
0 class Solution { 1 using array_t = array<pair<int, int>, 26>; 2 static constexpr const array_t lookup = []() constexpr -> array_t { 3 array_t res; 4 5 for (int i = 0; i < 26; i++) 6 res[i] = {i / 5, i % 5}; 7 8 return res; 9 }(); 10 11 public: 12 string alphabetBoardPath(const string &target) const { 13 int x = 0, y = 0; 14 string res; 15 16 for (const char c : target) { 17 const auto [i, j] = lookup[c - 'a']; 18 19 if (j < y) res += string(y - j, 'L'); 20 if (i < x) res += string(x - i, 'U'); 21 if (i > x) res += string(i - x, 'D'); 22 if (j > y) res += string(j - y, 'R'); 23 24 res += '!'; 25 x = i, y = j; 26 } 27 28 return res; 29 } 30 };