[LintCode] Valid Palindrome 验证回文字符串

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

Notice

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.

Have you met this question in a real interview?

Example

"A man, a plan, a canal: Panama" is a palindrome.

"race a car" is not a palindrome.

Challenge

O(n) time without extra memory.

LeetCode上的原题,请参见我之前的博客Valid Palindrome

解法一:

class Solution {
public:
    /**
     * @param s A string
     * @return Whether the string is a valid palindrome
     */
    bool isPalindrome(string& s) {
        int left = 0, right = s.size() - 1;
        while (left < right) {
            if (!isAlNum(s[left])) ++left;
            else if (!isAlNum(s[right])) --right;
            else if ((s[left] + 32 - ‘a‘) % 32 != (s[right] + 32 - ‘a‘) % 32) return false;
            else {
                ++left; --right;
            }
        }
        return true;
    }
    bool isAlNum(char c) {
        if (c >= ‘a‘ && c <= ‘z‘) return true;
        if (c >= ‘A‘ && c <= ‘Z‘) return true;
        if (c >= ‘0‘ && c <= ‘9‘) return true;
        return false;
    }
};

解法二:

class Solution {
public:
    /**
     * @param s A string
     * @return Whether the string is a valid palindrome
     */
    bool isPalindrome(string& s) {
        int left = 0, right = s.size() - 1;
        while (left < right) {
            if (!isalnum(s[left])) ++left;
            else if (!isalnum(s[right])) --right;
            else if ((s[left] + 32 - ‘a‘) % 32 != (s[right] + 32 - ‘a‘) % 32) return false;
            else {
                ++left; --right;
            }
        }
        return true;
    }
};
时间: 2024-12-23 04:53:49

[LintCode] Valid Palindrome 验证回文字符串的相关文章

Validate Palindrome 验证回文字符串

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "race a car" is not a palindrome. Note:Have you consider that the string might be empty? This is a good question to ask du

Leetcode 680.验证回文字符串

验证回文字符串 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: True 解释: 你可以删除c字符. 注意: 字符串只包含从 a-z 的小写字母.字符串的最大长度是50000. 1 class Solution { 2 public boolean isPalindromeRange(String s, int i, int j) { 3 for (in

LeetCode Valid Palindrome 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回

[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

验证回文字符串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 输入: "A man, a plan, a canal: Panama" 输出: true 代码: 思路,这里涉及到了数据清洗,我只要字母和数字,并且字母必须是小写.使用 string,isalnum()可以滤出字母和数字,使用 string.lower()可以滤出小写字母.然后再转换成 list 反转对比即可. http://www.runoob.com/python/python-strings.ht

初级算法-16. 验证回文字符串

题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 一种解法是将字符串的有效字符存入数组中,再去比较 1 class Solution { 2 public boolean isPalindrome(Stri

[LeetCode] Palindrome Number 验证回文数字

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

LeetCode(125):验证回文串

Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 解题思路: 验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如"level"或者"n

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["