<leetCode回文题的多种解法>转战github https://github.com/WinXi27

代码保证都AC

9. 回文数

9. 回文数

双指针 解法

cpp 复制代码
//思路: 
//先转换为字符串类型
//左右指针逼近
class Solution {
public:
    bool isPalindrome(int x) {
        string s=to_string(x);
        int l=0;
        int r=s.size()-1;
        while(l<r&&s[l]==s[r])l++,r--;
        return l>=r;
    }
};

125. 验证回文串

125. 验证回文串

笨 解法

cpp 复制代码
//思路: 
//先提取有用的字符(注意转为大写字母)
//再判断是不是回文串
class Solution {
public:
    bool isPalindrome(string s) {
        string temp;
        for(char c:s){
            if(isdigit(c)||isalpha(c) )temp.push_back(tolower(c) );
        }
        int l=0;
        int r=temp.size()-1;
        while(l<r&&temp[l]==temp[r])l++,r--;

        return l>=r;
    }
};

双指针 解法

cpp 复制代码
class Solution {
public:
    bool isValid(char c){
        return isdigit(c)||isalpha(c);
    }
    bool isPalindrome(string s) {
        int l=0;//左指针
        int r=s.size()-1;//右指针
        while(l<r){//左右指针逼近
            while(l<s.size()&&!isValid(s[l]) )l++;//左指针跳过不考虑的字符
            while(r>=0&&!isValid(s[r]) )r--;//右指针跳过不考虑的字符
            if(l<r&&tolower(s[l])!=tolower(s[r]) )return false;//不是回文,立即返回false
            l++,r--;
        }
        return true;
    }
};

234. 回文链表

234. 回文链表

反转链表+双指针 解法

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
//思路: 
//先找到中间节点位置(快慢指针法)
//反转后半部分的节点
//左右指针判断是否为回文
class Solution {
public:
    ListNode*reverse(ListNode*head){
        ListNode*Next=nullptr;
        ListNode*cur=head;
        ListNode*pre=nullptr;
        while(cur){
            Next=cur->next;
            cur->next=pre;
            pre=cur;
            cur=Next;
        }
        return pre;
    }
    bool isPalindrome(ListNode* head) {
        ListNode*fast=head;
        ListNode*slow=head;
        //1      1
        //2n+1  n+1
        while(fast&&fast->next){
            fast=fast->next->next;
            slow=slow->next;
        }
        ListNode*l=head;
        ListNode*r=reverse(slow);
        while(l&&r){
            if(l->val!=r->val)return false;
            l=l->next;
            r=r->next;
        }
        return true;
    }
};

266. 回文排列

266. 回文排列

哈希 解法

cpp 复制代码
class Solution {
public:
    bool canPermutePalindrome(string s) {
        unordered_map<char,int>mp;
        for(char c:s)mp[c]++;
        int cnt=0;
        for(auto it:mp){
            if(it.second%2==1)cnt++;
            if(cnt>=2)return false;
        }
        return true;
    }
};

680. 验证回文串 II

680. 验证回文串 II

双指针 解法

cpp 复制代码
//思路: 
//最多只有一个要删除的字符
//左右指针l,r
//遇到s[l]!=s[r]位置,要么删除l位置字符,要么删除r位置字符
class Solution {
public:
    bool valid(string s,int l,int r){
        while(l<r&&s[l]==s[r])l++,r--;
        return l>=r;
    }
    bool validPalindrome(string s) {
        if(s.empty() )return true;
        int l=0;
        int r=s.size()-1;
        while(l<r){
            if(s[l]==s[r])l++,r--;
            else{
                return valid(s,l+1,r)||valid(s,l,r-1);
            }
        }
        return true;
    }
};