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.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
只考虑数字和字母,字母大小写都一样,判断是否是回文。重点是忽略非非字母和数字。比较简单。
1 public class Solution { 2 public boolean isPalindrome(String s) { 3 4 int low=0; 5 int high=s.length()-1; 6 String s1=s.toLowerCase(); 7 while(low<high) 8 { 9 10 while(low<high) 11 { 12 char c=s1.charAt(low); 13 if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘)) 14 { 15 break; 16 } 17 else low++; 18 19 } 20 21 while(low<high) 22 { 23 char c=s1.charAt(high); 24 if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘)) 25 { 26 break; 27 }else high--; 28 29 } 30 31 32 33 if(s1.charAt(low)==s1.charAt(high)) 34 { 35 low++; 36 high--; 37 } 38 else 39 { 40 return false; 41 } 42 43 44 45 } 46 47 return true; 48 49 50 } 51 }
leecode 回文字符串加强版
时间: 2024-10-10 10:19:19