--- title: 'LeetCode 125. Valid Palindrome' disqus: hackmd --- # LeetCode 125. Valid Palindrome ## Description A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. ## Example Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. ## Constraints 1 <= s.length <= 2 * 10^5^ s consists only of printable ASCII characters. ## Answer 此題先抓頭尾,將非英文及數字的符號跳過。其中記得考慮全非字號的情況,若L可以一路跳到尾即為全非字號。跳完要判斷L<R看是否仍需比較,若L跟R是相同的英文 / 數字,則*L != *R和*L != *R^32(大小寫不同情況)必有一項不成立,故直接return false。剩下即為全檢查完return true。 ```Cin= //2022_03_12 bool isPalindrome(char * s){ char *L = s, *R = s + strlen(s) - 1; while(L < R){ while(L<R && (*L<'0') || (*L>'9' && *L<'A') || (*L>'Z' && *L<'a') || (*L>'z') ){L++;} while(L<R && (*R<'0') || (*R>'9' && *R<'A') || (*R>'Z' && *R<'a') || (*R>'z') ){R--;} if(L<R && *L != *R && *L != (*R^32)){ return false; } L++; R--; } return true; } ``` ## Link https://leetcode.com/problems/valid-palindrome/ ###### tags: `Leetcode`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up