Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool isRobotBounded(string ins) {
- // north=0, east=1, south=2, west=3
- int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
- int x=0, y=0;
- int idx=0;
- for(char ch: ins){
- if(ch == 'L'){
- idx = (idx+3)%4;
- }
- else if( ch == 'R'){
- idx = (idx+1)%4;
- }
- else{
- x += dir[idx][0];
- y += dir[idx][1];
- }
- }
- return (x==0 && y==0) || (idx != 0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement