Validation palindrome string of LeetCode

Keywords: Programming ascii REST

Given a string, verify whether it is a palindrome string, only consider alphabetic and numeric characters, and ignore the case of letters.

In this paper, we define the empty string as a valid palindrome string.

Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:
Type: "race a car"
Output: false

C code ①: modify the original string (uppercase letter to lowercase letter)

bool isPalindrome(char* s) {
    int length = strlen(s);
    int left=0, right=length-1;
    if( length == 0 )
        return true;
    while( left < right )
    {
        if( s[left]>='A' && s[left]<='Z' ) //Convert upper case letters to lower case letters by ASCII value
            s[left] += 32;
        if( (s[left]<'a'||s[left]>'z') && (s[left]<'0'||s[left]>'9') ) //Skip characters other than letters and numbers
        {
            left++;
            continue;
        }
        if( s[right]>='A' && s[right]<='Z' )
            s[right] += 32;
        if( (s[right]<'a'||s[right]>'z') && (s[right]<'0'||s[right]>'9') )
        {
            right--;
            continue;
        }
        
        if( s[left] != s[right] ) //Compare characters in corresponding positions
            return false;
        
        left++, right--;
    }
    return true; //Jump out of the while loop, then left > = right, indicating that it is a palindrome string
}

C code ②: do not modify the original string (match the case of the same letter in the comparison algorithm)

bool isPalindrome(char* s) {
    int length = strlen(s);
    int left=0, right=length-1;
    if( length == 0 )
        return true;
    while(1)
    {   
        //Skip characters other than letters and numbers
        while( !( (s[left]>='A'&&s[left]<='Z') || (s[left]>='a'&&s[left]<='z') || (s[left]>='0'&&s[left]<='9') ) ) 
        {
            left++;
            if( left >= right )
                return true;
        }
        while( !( (s[right]>='A'&&s[right]<='Z') || (s[right]>='a'&&s[right]<='z') || (s[right]>='0'&&s[right]<='9') ) ) 
        {
            right--;
            if( left >= right )
                return true;
        }
        
        if( s[left]>='A' && s[right]>='A') //Letter discrimination
        {
            if( !(s[left]==s[right] || s[left]-s[right]==32 || s[right]-s[left]==32) ) //Match the case of a letter
                return false;
        }
        else if( s[left]<='9' && s[right]<='9' ) //Digital discrimination
        {
            if( s[left] != s[right] )
                return false;
        } 
        else //For non return strings containing numbers and letters, such as "0P"
            return false;  
        
        left++, right--; //left,right move symmetrically
    }
}

 

Note: the above palindrome string judgment algorithm is to match the number and letter of palindrome by skipping the rest of the characters when there are other irrelevant characters.

If there are no irrelevant characters, it is easier to match the continuous pure letter string. Please refer to the following code

C Code:

bool palindrome(char *s){  
	char *a = s;		//Define a new pointer to s
	int i = 0,j=0,k=0;
	while (*a != '\0'){     	//Count string digits
		a++;
		i++;
	}
	a--;                     //a is the address of the last bit '\ 0' of the string, so the last bit of the input string will be subtracted
	while (*s != '\0'){       //Make two comparisons
		if (*s == *a){
			k++;
		}
		else{ 
			return false;//If there are corresponding unequal items during traversal, false will be returned directly
		}
		s++;//Pointer s forward traversal compared with a
		a--;//Pointer a reverse traversal compared with s
	}
	if (k == i){           //If k and i are equal, it means that the forward and reverse correspondences of strings are equal during traversal, and return true
		return true;
	}
}

For the judgment of palindrome number, please refer to the palindrome number of LeetCode, link: https://blog.csdn.net/qq_39564672/article/details/87988067

Posted by mistercash60000 on Sat, 30 Nov 2019 01:19:08 -0800