LeetCode 125 Valid Palindrome
题目:https://leetcode.com/problems/valid-palindrome/description/
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama" Output: true
Example 2:
Input: "race a car" Output: false
Seen this question in a real interview before?
Yes
No
c++:
class Solution { public: bool isPalindrome(string s) { if(s.length()==0) return true; int l = s.length(); for(int i=0,j=l-1;i<=j;i++,j--) { if(judge(s[i])&&judge(s[j])) { if(change(s[i])!=change(s[j])) return false; } else if(!judge(s[i])&&judge(s[j])) { j++; } else if(judge(s[i])&&!judge(s[j])) { i--; } } return true; } int judge(char x) { if((x>='a'&&x<='z')||(x>='A'&&x<='Z')||(x>='0'&&x<='9')) return 1; else return 0; } char change(char x) { if(x>='A'&&x<='Z') return x+32; else return x; } };