LeetCode 125. 驗證迴文串 (首尾雙指針)

驗證迴文串

如何將string對象進行大小寫字母轉換。

template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op );
class Solution {
public:
    bool isPalindrome(string s) {
        transform(s.begin(),s.end(),s.begin(),::tolower);
        int l = 0,r = s.size()-1; 
        while(l<=r){
            while(!check(s[l]) && l<=r){
                l++;
            }
            while(!check(s[r]) && l<=r){
                r--;
            }
            if(l>r){
                break;
            }
            if(s[l]!=s[r]){
                return false;
            }
            l++;
            r--;
        }
        return true;
    }  
    bool check(char c){
        return (c>='0'&& c<='9') || (c>='a' && c<='z');
    }

};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章