class Solution {
public:
bool judgeCircle(string moves) {
int x = 0, y = 0;
for (int i = 0; i < moves.length(); i++)
{
if (moves.[i]=="R")
x++;
if (moves.[i]=="L")
x--;
if (moves.[i]=="U")
y++;
if (moves.[i]=="D")
y--;
}
if (x+y==0)
return true;
else
return false;
}
};
显然取字符方式不对。“L”这样是字符串,‘L’才是字符
beat 80%多
class Solution {
public:
bool judgeCircle(string moves) {
int x = 0, y = 0;
for (int i = 0; i < moves.length(); i++)
{
if (moves[i]=='R')
x++;
if (moves[i]=='L')
x--;
if (moves[i]=='U')
y++;
if (moves[i]=='D')
y--;
}
if (x+y==0)
return true;
else
return false;
}
};