0
点赞
收藏
分享

微信扫一扫

[leetcode] 125. Valid Palindrome


Description

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

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

Example 1:

Input:

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

Output:

true

Example 2:

Input:

"race a car"

Output:

false

分析

题目的意思是:判断一个字符串是否是回文子串。

  • 本题是判断回文子串,用两个索引,分别指向字符串的开头和结尾,其中注意大小写的相等比较,还有要跳过空格和标点不好。
  • 这道题在牛课网上通过了但在leetcode上没有通过,".“和” "没有判断出来,我家了if(i>j)的操作,然后accept了,看来leetcode更难一点。

代码

class Solution {
public:
bool isPalindrome(string s) {
int i=0;
int j=s.length()-1;
while(i<=j){
while(i<=j&&!isalnum(s[i])){
i++;
}
while(i<=j&&!isalnum(s[j])){
j--;
}
if(i>j){
return true;
}
if(tolower(s[i])==tolower(s[j])){
i++;
j--;
}else{
return false;
}
}
return true;
}
};

参考文献

​​[编程题]valid-palindrome​​


举报

相关推荐

0 条评论