forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot Bounded In Circle.cpp
27 lines (27 loc) · 1019 Bytes
/
Robot Bounded In Circle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Runtime: 0 ms (Top 100.00%) | Memory: 6.2 MB (Top 51.28%)
class Solution {
public:
bool isRobotBounded(string instructions) {
char direction = 'N';
int x = 0, y = 0;
for (char &instruction: instructions) {
if (instruction == 'G') {
if (direction == 'N') y++;
else if (direction == 'S') y--;
else if (direction == 'W') x--;
else x++;
} else if (instruction == 'L') {
if (direction == 'N') direction = 'W';
else if (direction == 'S') direction = 'E';
else if (direction == 'W') direction = 'S';
else direction = 'N';
} else {
if (direction == 'N') direction = 'E';
else if (direction == 'S') direction = 'W';
else if (direction == 'W') direction = 'N';
else direction = 'S';
}
}
return (x == 0 && y == 0) || direction != 'N';
}
};