leetcode Valid Palindrome

题目连接

https://leetcode.com/problems/valid-palindrome/ 

Valid Palindrome

Description

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example, 
“A man, a plan, a canal: Panama” is a palindrome. 
“race a car” is not a palindrome.

class Solution {
public:
	bool isPalindrome(string s) {
		ret = "";
		if (s.empty()) return true;
		int n = (int)s.length();
		for (int i = 0; i < n; i++) {
			if (isalpha(s[i]) || isdigit(s[i])) ret += tolower(s[i]);
		}
		return isOk();
	}
private:
	string ret;
	bool isOk() const {
		int i = 0, j = (int)ret.length() - 1;
		while (i < j) {
			if (ret[i] != ret[j]) return false;
			i++, j--;
		}
		return true;
	}
};
posted @ 2015-12-09 20:20  GadyPu  阅读(161)  评论(0编辑  收藏  举报