25. Valid Palindrome

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.

思想:简单的从两端来逐个读字符,判断是否相等。(36ms)

inline char getLowerChar(string &s, int id) {
    if((s[id] >= ‘a‘ && s[id] <= ‘z‘) || (s[id] >= ‘0‘ && s[id] <= ‘9‘)) return s[id];
    else if(s[id] >= ‘A‘ && s[id] <= ‘Z‘)  return s[id] + 32;
    else return 0;
}
inline char getFirstChar(string &s, int& l) {
    while(l < s.size()) {
        char ch = getLowerChar(s, l);
        if(ch) return ch;
        ++l;
    }
    return 0;
}
inline char getLastChar(string &s, int& h) {
    while(h >= 0) {
        char ch = getLowerChar(s, h);
        if(ch) return ch;
        --h;
    }
    return 0;
}
class Solution {
public:
    bool isPalindrome(string s) {
        int l = 0, h = s.size()-1;
        while(l <= h) {
            if(getFirstChar(s, l) != getLastChar(s, h))
                return false;
                ++l, --h;
        }
        return true;

    }
};
时间: 2024-10-11 07:14:34

25. Valid Palindrome的相关文章

Valid Palindrome(LintCode)

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Example "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note Have you co

Valid Palindrome 解答

Question 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 conside

LeetCode: Valid Palindrome 解题报告

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

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 tha

LeetCode(125)题解--Valid Palindrome

https://leetcode.com/problems/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"

680. Valid Palindrome II【easy】

680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You co

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 co

[LeetCode][JavaScript]Valid Palindrome

https://leetcode.com/problems/valid-palindrome/ 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."rac

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:Hav