题目如下:
也是很简单的题,只要先判断是否是偶数,然后再判断UD,LR数目是否相等就可以了。
代码如下:
#include <string>
int num(string s, char c) //统计字符串中含有几个c字符
{
int count = 0;
for (int i = 0; i < s.length(); ++i)
{
if (c == s[i])
{
count++;
}
}
return count;
}
class Solution {
public:
bool judgeCircle(string moves) {
int numofu, numofd, numofl, numofr;
numofu = num(moves, 'U');
numofd = num(moves, 'D');
numofl = num(moves, 'L');
numofr = num(moves, 'R');
if ((moves.length() % 2) != 0)
{
return 0;
}
else
{
if ((numofu != numofd) || (numofl != numofr))
{
return 0;
}
else
{
return 1;
}
}
}
};