LeetCode - 字符串 - 验证回文串

  • 题目:验证回文串
  • 难度:简单
  • 题目描述:给定一个字符串,验证它是否是回文串,只考虑字母数字字符,可以忽略字母的大小写
  • 示例1:输入: “A man, a plan, a canal: Panama” 输出: true
  • 示例2:输入: "race a car"输出: false

C语言解法:一个头指针,一个尾指针,前后对比
在这里插入图片描述

bool isPalindrome(char * s){
	int len = strlen(s);
	if (len == 0 || len == 1){
		return true;
	}
	char *end = s;
	end += len - 1;
	while (end >= s){
		while (end >= s){
			if (*end >= 'a' && *end <= 'z' || *end >= 'A'&&*end <= 'Z' || *end >= '0' && *end <= '9'){
				break;
			}
			else{
				end--;
				continue;
			}
		}
		while (end >= s){
			if (*s >= 'a' && *s <= 'z' || *s >= 'A'&&*s <= 'Z' || *s >= '0' && *s <= '9'){
				break;
			}
			else{
				s++;
				continue;
			}
		}
		if (end < s) break;
		if (*end == *s || *end == toupper(*s) || *end == tolower(*s)){
			end--;
			s++;
			continue;
		}else{
			return false;
		}
	}
	return true;
}

猜你喜欢

转载自blog.csdn.net/weixin_45437022/article/details/106853897