题目来源于力扣–https://leetcode-cn.com/
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
1、首先将字符串的字母转为小写字母。
2、遇到非字母或者数字跳过
3、比较字母或者数字是否相同
main函数代码
#include<stdio.h>
bool isPalindrome(char * s);
int main(){
char s[] = "A man, a plan, a canal: Panama";
bool isTrue = isPalindrome(s);
printf("%d",isTrue);
return 0;
}
验证是否回文串代码
bool isPalindrome(char * s){
//判断字符串长度
int length = 0;
while(s[length]){
length++;
}
//如果字符串为空 返回true
if(length==0){
return true;
}
//首先将大写字母转为小写字母
for(int i=0;i<length;i++){
if(s[i]>='A'&&s[i]<='Z'){
s[i] = s[i]+32;
}
}
// 首指针
int start = 0;
// 尾指针
int end = length-1;
while(start<end){
// 不是字母或者数字跳过
while(!(s[start]>='a'&&s[start]<='z')&&!(s[start]>='0'&&s[start]<='9')&&start<end){
start++;
}
// 不是字母或者数字跳过
while(!(s[end]>='a'&&s[end]<='z')&&!(s[end]>='0'&&s[end]<='9')&&start<end){
end--;
}
// 不相等直接返回false
if(s[start]!=s[end]){
return false;
}
//首指针往后走
start++;
// 尾指针往前走
end--;
}
return true;
}
2189

被折叠的 条评论
为什么被折叠?



