思路:这题思路很清晰,分别计数x轴和y轴上移动步数即可。L:x--;R:x++;U:y++;D:y--;我犯错的点是,判断等于的时候,==“U” 应该为’u‘。其次这个写为switch使得代码可读性更好。
class Solution {
public:
bool judgeCircle(string moves) {
int posX=0;
int posY=0;
bool flag=true;
for(int i=0;i<moves.length();i++){
if(moves[i]=='U'){
posY++;
}else if(moves[i]=='D'){
posY--;
}else if(moves[i]=='L'){
posX--;
}else if(moves[i]=='R'){
posX++;
}
}
if(posX==0&&posY==0){
flag= true;
}else{
flag=false;
}
return flag;
}
};