leetcode26:valid-palindrome

题目描述

判断题目给出的字符串是不是回文,仅考虑字符串中的字母字符和数字字符,并且忽略大小写
例如:"A man, a plan, a canal: Panama"是回文
"race a car"不是回文
注意:
你有没有考虑过字符串可能为空?这是面试时应该提出的一个好问题。
针对这个问题,我们定义空字符串是回文


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.

示例1

输入

复制
"A man, a plan, a canal: Panama"

输出

复制
true
示例2

输入

复制
"race a car"

输出

class Solution {
public:
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */

bool isPalindrome(string s) {
        int i,j;
        for(i=0,j=s.length()-1;i<j;++i,--j){
            while(i<j && !isalnum(s[i])) ++i;
            while(i<j && !isalnum(s[j])) --j;
            if (i<j && tolower(s[i])!=tolower(s[j])) return false;
        }
        return true;
    }

};

 

posted on 2020-08-01 22:48  滚雪球效应  阅读(109)  评论(0编辑  收藏  举报