LeetCode: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.


解题思路:

    类似快排的写法,两头逼近。去掉不满足条件的字符,然后做个比較就可以.


解题代码:

class Solution {
public:
    bool isPalindrome(string s)
    {
        bool res = true;
        int i = 0, j = s.size() - 1;    
        while ( res && i < j)
        {
            while (i < j && !(isdigit(s[i]) || isalpha(s[i])))
                ++i;
            while (i < j && !(isdigit(s[j]) || isalpha(s[j])))
                --j;
            if (i < j)
            {
                res = s[i] == s[j] || abs(s[i] - s[j]) == 32;
                ++i, --j;
            }
        }
        return res;
    }
};







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5183857.html,如需转载请自行联系原作者

相关文章
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
55 0
LeetCode 409. Longest Palindrome
LeetCode 367. Valid Perfect Square
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
67 0
LeetCode 367. Valid Perfect Square
|
索引
LeetCode 336. Palindrome Pairs
给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。
96 0
LeetCode 336. Palindrome Pairs
LeetCode 242. Valid Anagram
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
54 0
LeetCode 242. Valid Anagram
|
算法 索引
LeetCode 214. Shortest Palindrome
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
60 0
LeetCode 214. Shortest Palindrome
|
canal
LeetCode 125. Valid Palindrome
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
67 0
LeetCode 125. Valid Palindrome
|
算法
LeetCode 65. Valid Number
验证给定字符串是否可以解释为十进制数。
68 0
LeetCode 65. Valid Number
Leetcode-Easy 234. Palindrome Linked List
Leetcode-Easy 234. Palindrome Linked List
45 0
Leetcode-Easy 234. Palindrome Linked List
Leetcode-Easy 20. Valid Parentheses
Leetcode-Easy 20. Valid Parentheses
90 0
Leetcode-Easy 20. Valid Parentheses