[Leetcode]-Valid Palindrome, leetcode-valid
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.
Question: Determine whether the string is a return string
Train of Thought: The two pointers go from the header and tail of the string to the middle. Only when the character is met Will the comparison be equal. When any pointer encounters other symbols, it will continue to go, until it is a character or has reached the character boundary.
Note:
1. Empty characters are also input characters
2. Be sure to make boundary judgments. Otherwise, when there are only special characters and no letters in the string, it will cross the border. In VS2012, the program does not crash, nor does it exist in LINUX.
3. Case Insensitive
4. When strlen (s) <2 must be a reply
#include <stdlib.h>#include <stdio.h>#include <math.h>#include <string.h>#include <stdbool.h>#define isstr(a) ((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9'))bool isPalindrome(char* s) { if(NULL == s) return true; if('\0' == s) return true; if(strlen(s) < 2) return true; char* pa = s; char* pb = s; char* l = s;//border of pa point while(*pb != '\0') pb++; pb--; //make pb point the last character,nor '\0'!! char* n = pb;//border of pb point while(pa < pb) { while(!isstr(*pa) && pa<=n) pa++; while(!isstr(*pb) && pb>=l) pb--; if(((*pa != *pb) && (abs(*pa-*pb) != 'a'-'A')) && (isstr(*pa)) && (isstr(*pb))) return false; else { pa++; pb--; } } return true;}int main(){ char* s = "A man, a plan, a canal: panama"; bool r = isPalindrome(s); printf("s is isPalindrome? : %d \n",r); char *s1 = ""; bool r1 = isPalindrome(s1); printf("s1 is isPalindrome? : %d \n",r1); char *s2 = "*."; bool r2 = isPalindrome(s2); printf("s2 is isPalindrome? : %d \n",r2); char *s3 = "Sore was I ere I saw Eros."; bool r3 = isPalindrome(s3); printf("s3 is isPalindrome? : %d \n",r3);}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.