leetCode 125. Valid Palindrome 字符串

125. Valid Palindrome

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.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

题目大意:

回文的检测。

思路:

1.清洗字符串,得到只有数字和字母的字符串。

2.通过比较首尾的字符来判断。

代码如下:

class Solution {
public:
    vector<string> stringSplit(string s, const char * split)
    {
    	vector<string> result;
    	const int sLen = s.length();
    	char *cs = new char[sLen + 1];
    	strcpy(cs, s.data());
    	char *p;
    
    	p = strtok(cs, split);
    	while (p)
    	{
    		printf("%s\n", p);
    		string tmp(p);
    		result.push_back(tmp);
    		p = strtok(NULL, split);
    	}
    	return result;
    }
    bool isPalindrome(string s) {
    	if (s.size() == 0 || s.size() == 1)
    		return true;
    	vector<string> vecStrs = stringSplit(s," [email protected]#$%^&*().,:;-?\"‘`");
    	s = "";
    	for (int i = 0; i < vecStrs.size(); i++)
    		s += vecStrs[i];
    	if (s.size() == 1 || s.size() == 0)
		    return true;
    	int i = 0;
    	for (; i < s.size() / 2; i++)
    	{
    		if (s[i] <= 57 ||  s[s.size() - i - 1] <= 57)
    		{
    			if (s[i] == s[s.size() - i - 1])
    			{
    				continue;
    			}
    			else
    			{
    				return false;
    			}
    		}
    		else if (s[i] == s[s.size() - i - 1] ||
    			s[i] - s[s.size() - i - 1] == 32 ||
    			s[s.size() - i - 1] - s[i] == 32)
    		{
    			continue;
    		}
    		else
    		{
    			return false;
    		}
    	}
    	return true;
    }
};

上面的做法效率低下,还有对API不熟悉。

下面是对上面的改进:

参考https://discuss.leetcode.com/topic/48376/12ms-c-clean-solution

代码如下:

class Solution {
public:
	bool isPalindrome(string s) {
		int i = 0, j = s.size() - 1;
		while (i < j)
		{
			while (!isalnum(s[i]) && i < j) i++;
			while (!isalnum(s[j]) && i < j) j--;
			if (tolower(s[i++]) != tolower(s[j--]))
				return false;
		}
		return true;
	}
};

这里使用了isalnum()函数来判断是否为文字数字。

通过使用tolower()来统一字符的大小写,都变为小写。

2016-08-11 13:26:25

时间: 2024-10-11 23:53:40

leetCode 125. Valid Palindrome 字符串的相关文章

[LeetCode] 125. Valid Palindrome 有效回文

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. Note:Have you consider that th

Java [Leetcode 125]Valid Palindrome

题目描述: 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. Note:Have you consider t

leetcode 125. Valid Palindrome ----- java

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. Note:Have you consider that th

leetcode 125. Valid Palindrome

题目描述 c语言中的tolowe(A)函数,将字符A转换为小写: isalpha(a) 判断a是不是一个字符 isdigital(a)判断a是不是一个数字,isalnum(a)判断a是不是一个数字或者字符 只判断字符串中字符是不是回文 空字符串是一个回文字符串 class Solution { public: bool isPalindrome(string s) { int i = 0; int j = s.size() -1; while(i < j){ while(!isalnum(s[i

Java for LeetCode 125 Valid Palindrome

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. 解题思路: 注意题目,忽略大小写,忽略非字母或数字,JA

125. Valid Palindrome(js)

125. Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome 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. Note:

125. Valid Palindrome【easy】

125. Valid Palindrome[easy] 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. No

【LeetCode】125 - Valid Palindrome

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. Note:Have you consider that th