Valid Palindrome

    xiaoxiao2021-03-25  167

    https://leetcode.com/problems/valid-palindrome/?tab=Description

    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.

    #define isAlphanumeric(c) ((((c)>='A')&&((c)<='Z'))||(((c)>='a')&&((c)<='z'))||(((c)>='0')&&((c)<='9'))) bool isPairs(char c1, char c2) { if(c1 == c2) return true; if((c1>='A')&&(c1<='Z') && ((c2>='a')&&(c2<='z'))) return ((c1+32) == c2); if((c2>='A')&&(c2<='Z') && ((c1>='a')&&(c1<='z'))) return (c1 == (c2+32)); return false; } bool isPalindrome(char* s) { if(!s || *s == '\0') return true; int len = strlen(s); int head = 0; int tail = len - 1; while(head < tail) { while(head < tail && !isAlphanumeric(s[head])) head++; while(head < tail && !isAlphanumeric(s[tail])) tail--; if(!isPairs(s[head], s[tail])) return false; head++; tail--; } return true; }

    转载请注明原文地址: https://ju.6miu.com/read-9656.html

    最新回复(0)