125. Valid Palindrome

来源:互联网 发布:.儿童编程小游戏 编辑:程序博客网 时间:2024/05/22 16:45
  • Given a string, determine if it is a palindrome, considering only
    alphanumeric characters and ignoring cases.

    For example, “A man, a plan, a canal: Panama” is a palindrome. “race
    a car” is not a palindrome.

    Note: Have you consider that the string might be empty? This is a
    good question to ask during an interview.

    For the purpose of this problem, we define empty string as valid
    palindrome.

bool isAlphanumeric(char c) {    if( (c >= '0' && c <= '9')||        (c >= 'a' && c <= 'z')||        (c >= 'A' && c <= 'Z')){        return true;    }else{        return false;    } }bool isSame(char a,char b){      if(a >= '0' && a <= '9'){        if(a == b){            return true;        }else{            return false;        }    }    if((a >= 'a' && a <= 'z')||       (a >= 'A' && a <= 'Z')){        if(abs(a-b) == 0 || abs(a-b) == 32){            return true;        }else{            return false;        }    }    return false;}bool isPalindrome(char* s) {    char * start = NULL;    char * end = NULL;    if(s == NULL){        return true;    }    start = s;    end = s + strlen(s) - 1;    while(start < end){        while((!isAlphanumeric(*start))&&start < end){            start++;        }        while((!isAlphanumeric(*end))&&start < end){            end--;        }        if(start >= end ){            break;        }else{            //printf("start = %c,end = %c\n\r",*start,*end);            if(!isSame(*start,*end)){                               return false;            }            start++;            end--;        }    }    return true;}
原创粉丝点击