
官方给的题解
//7ms
public static boolean judgeCircle(String moves) {
int x = 0, y = 0;
for(char move:moves.toCharArray()) {
if (move == 'U') y--;
else if (move == 'D') y++;
else if (move == 'L') x--;
else if (move == 'R') x++;
}
return x ==0&&y ==0;
}
我根据官方的改的我的题解
//12ms
public static boolean judgeCircle1(String moves) {
int x = 0;
int y = 0;
for (int i = 0; i < moves.length(); i++) {
if (moves.charAt(i) == 'L')
x++;
else if (moves.charAt(i) == 'R')
y++;
else if (moves.charAt(i) == 'U')
x--;
else if (moves.charAt(i) == 'D')
y++;
}
return x == 0 && y == 0;
}
我自己写的
//18ms
public static boolean judgeCircle(String moves) {
int Lcount =0;
int Rcount =0;
int Ucount =0;
int Dcount =0;
for (int i = 0; i <moves.length() ; i++) {
if(moves.charAt(i)=='L')
Lcount++;
if(moves.charAt(i)=='R')
Rcount++;
if(moves.charAt(i)=='U')
Ucount++;
if(moves.charAt(i)=='D')
Dcount++;
}
if(Math.abs(Lcount - Rcount)!=0)
return false;
if (Math.abs(Ucount - Dcount)!=0)
return false;
return true;
}
来源:CSDN
作者:ldd儆儆
链接:https://blog.csdn.net/qq_38304320/article/details/103455452