1041. Robot Bounded In Circle
这个问题需要发现规律,就是将命令序列执行一遍,两遍,四遍,必然存在一次回到原点,如果没有就不可能回到起点。
class Solution {
public boolean isRobotBounded(String ins) {
int[][] dirs={{0,1},{1,0},{0,-1},{-1,0}};
int dir=0;
int startX=0,startY=0;
for(int i=0;i<ins.length();i++){
if(ins.charAt(i)=='L'){
dir=(dir+3)%4;
}
else if(ins.charAt(i)=='R'){
dir=(dir+1)%4;
}
else
{
startX+=dirs[dir][0];
startY+=dirs[dir][1];
}
}
return (startX==0 && startY==0) || dir!=0;
}
}