题目链接:
https://leetcode.cn/problems/robot-return-to-origin/?envType=study-plan-v2&envId=programming-skills
思路:
循环遍历,模拟即可
代码:
class Solution {
public boolean judgeCircle(String moves) {
int n = moves.length();
if(n%2==1) return false;
int x=0,y = 0;
for(int i = 0;i<n;i++) {
char c = moves.charAt(i);
// if(c=='U') {
// y++;
// } else if(c=='D') {
// y--;
// } else if(c=='L') {
// x--;
// } else if(c=='R') {
// x++;
// }
switch(c) {
case'U':
y++;
break;
case'D':
y--;
break;
case'L':
x--;
break;
case'R':
x++;
break;
}
}
return x==0 && y==0;
}
}