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)


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