LeetCode:125。回文を確認します

文字列を指定して、それが回文であるかどうかを確認し、文字と数字のみを考慮し、文字の大文字と小文字を無視します。

説明:この質問では、空の文字列を有効な回文文字列として定義します。

例1:

入力:「男、計画、運河:パナマ」
出力: true

例2:

入力:「車をレースする」
出力: false
class Solution {
    bool isNumOrChar(char c){
        if(tolower(c) >= 'a' && tolower(c) <= 'z' || tolower(c) >= '0' && tolower(c) <= '9')
            return true;
        return false;
    }
public:
    bool isPalindrome(string s) {
        int n = s.size();
        if(n == 0) return true;
        int low = 0, high = n - 1;
        while(low < high){
            if(!isNumOrChar(s[low])) low ++;
            if(!isNumOrChar(s[high])) high --;
            if(isNumOrChar(s[low]) && isNumOrChar(s[high])){
                if(tolower(s[low]) == tolower(s[high])){
                    low ++; high --;
                }else return false;
            }
        }
        return true;
    }
};

 

おすすめ

転載: blog.csdn.net/qq_40513792/article/details/106222855
おすすめ