1. 程式人生 > >Leetcode 125.驗證迴文串

Leetcode 125.驗證迴文串

驗證迴文串

給定一個字串,驗證它是否是迴文串,只考慮字母和數字字元,可以忽略字母的大小寫。

說明:本題中,我們將空字串定義為有效的迴文串。

示例 1:

輸入: "A man, a plan, a canal: Panama"

輸出: true

示例 2:

輸入: "race a car"

輸出: false

 

 1 class Solution {
 2     public boolean isPalindrome(String s){
 3         char[] cha=s.toCharArray();
 4         int
i=0,j=cha.length-1; 5 while(i<j){ 6 if(!Character.isLetterOrDigit(cha[i])) 7 i++; 8 else if(!Character.isLetterOrDigit(cha[j])) 9 j--; 10 else 11 if(Character.toLowerCase(cha[i])==Character.toLowerCase(cha[j])){
12 i++; 13 j--; 14 }else{ 15 return false; 16 } 17 } 18 return true; 19 } 20 }