llllmz

导航

125. 验证回文串c

回文串 置逆,栈,双指针。

bool judge(char c){
    if(c>='a'&&c<='z') return true;
    if(c>='A'&&c<='Z') return true;
    if(c>='0'&&c<='9') return true;
    return false;
}

bool isPalindrome(char* s) {
    int n=strlen(s);
    if(n==1) return true;
    char* stack=(char*)malloc(sizeof(char)*n);
    int top=0;
    for(int i=0;i<n;i++){
        if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]-'A'+'a';
        if(judge(s[i])) stack[top++]=s[i];
    }
    if(top==0) return true;
    int head=0,tail=top-1;
    while(head<=tail){
        if(stack[head]!=stack[tail]) return false;
        head++;
        tail--;
    }
    return true;
}

结果:

posted on 2024-03-15 22:24  神奇的萝卜丝  阅读(25)  评论(0)    收藏  举报