leetcode---657. Judge Route Circle

題目如下:

 

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章