題目如下:
也是很簡單的題,只要先判斷是否是偶數,然後再判斷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;
}
}
}
};