541. 反转字符串 II

541. 反转字符串 II

class Solution 
{
public:
	bool isDigtalOrWord(char ch)
	{
		if (isalpha(ch)||isdigit(ch))
			return true;
		return false;
	}
	bool isPalindrome(string s)
	{
		if (s.empty())
			return true;

		for (int i = 0; i < s.size(); ++i)
			s[i] = tolower(s[i]);

		int left = 0;
		int right = s.size() - 1;
		while (left < right)
		{
			while (left < right && !isDigtalOrWord(s[left]))
				left++;
			while (left < right && !isDigtalOrWord(s[right]))
				right--;

			if (s[left] != s[right])
				return false;

			left++;
			right--;
		}
		return true;
	}
};

你可能感兴趣的:(C家家精品好题,c++)