leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1041.cpp (636B)


0 class Solution { 1 public: 2 bool isRobotBounded(const string &instructions) const { 3 static const int offset_x[] = {0, -1, 0, 1}; 4 static const int offset_y[] = {1, 0, -1, 0}; 5 6 int x = 0, y = 0, dir = 0; 7 for (const char instruction : instructions) { 8 switch (instruction) { 9 case 'G': 10 x += offset_x[dir]; 11 y += offset_y[dir]; 12 break; 13 case 'L': dir = dir == 3 ? 0 : dir + 1; break; 14 case 'R': dir = dir == 0 ? 3 : dir - 1; break; 15 } 16 } 17 18 return (x == 0 && y == 0) || (dir != 0); 19 } 20 };