給定一個字符串,驗證它是否是迴文串,只考慮字母和數字字符,可以忽略字母的大小寫。
說明:本題中,我們將空字符串定義爲有效的迴文串。
示例 1:
輸入: "A man, a plan, a canal: Panama"
輸出: true
示例 2:
輸入: "race a car"
輸出: false
解析:
class Solution {
public boolean isPalindrome(String s) {
if(s==null||s.length()==0){
return true;
}
String regex ="[^0-9a-zA-Z]+";
String st = s.replaceAll(regex,"").toLowerCase();
for(int i=0;i<st.length()/2;i++){
if(st.charAt(i)!=st.charAt(st.length()-i-1))
return false;
}
// int i=0,j=st.length()-1;
// while(j>i){
// if(st.charAt(i)!=st.charAt(st.length()-i-1))
// return false;
// i++;
// j--;
// }
return true;
}
}