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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章