[Verify] palindromic sequence LeetCode

[Problem] Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.
Description: In this problem, we define the empty string as a valid palindromic sequence.

Example 1 : 
Input: " A man, A Plan, A Canal: Panama " 
Output: to true 
Example 2 : 
Input: " Race A CAR " 
Output: to false

[Thinking]

Good think this idea, we use two-hand l and r, the comparison from the beginning of each character string ends, if the character is not the same, return false. If so, then l ++, r--, closer to the center.
But the key is the key, is to detect each character valid, and the capitalization. And the program has to solve a string of letters without a valid case!

Shortcut letter case conversion:

Unified National Cheng Kung University wrote: ch & 0b11011111 abbreviation: ch & 0xDF

Unified to lower case: ch | 0b00100000 abbreviation: ch | 0x20

【answer】

class Solution {
public:
    bool isvalid(char c){
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 
        || (c >= '0' && c <= '9')){
            return false;
        }
        return true;

    s) {
        stringisPalindrome (Bool
    }if(s == "") return true;
        int l = 0, r = s.length() - 1;
        while(l < r){
            while(isvalid(s[l]) && (l <r)) l++;
            while(isvalid(s[r]) && (l <r)) r--;
            if (l < r){
                if((s[l++] | 0x20) != (s[r--] | 0x20)){
                    return false;
                }
            } 
        }
        return true;
    }
};

Guess you like

Origin www.cnblogs.com/zhudingtop/p/11669742.html