解法一:
class Solution {
public:
bool judgeCircle(string moves) {
int l=0,r=0,u=0,d=0;
for(int i=0;i<moves.size();++i)
{
if(moves[i]=='L')
++l;
else if(moves[i]=='R')
++r;
else if(moves[i]=='U')
++u;
else
++d;
}
if(l==r &&u==d)
return true;
else
return false;
}
};
解法二:
class Solution {
public:
bool judgeCircle(string moves) {
int l=count(moves.begin(),moves.end(),'L');
int r=count(moves.begin(),moves.end(),'R');
int u=count(moves.begin(),moves.end(),'U');
int d=count(moves.begin(),moves.end(),'D');
if(l==r &&u==d)
return true;
else
return false;
}
};