题意
机器人开始在(0,0)点,有一段指令,包括上下左右,判断它通过一段指令后是否回到了原点。
解题思路
模拟即可
参考代码
//java
class Solution {
public boolean judgeCircle(String moves) {
int x=0,y=0;
for (int i=0;i<moves.length();i++){
if (moves.charAt(i)=='R') x++;
if (moves.charAt(i)=='L') x--;
if (moves.charAt(i)=='U') y--;
if (moves.charAt(i)=='D') y++;
}
if (x==0&&y==0)
return true;
return false;
}
}