Description:
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.
public class Solution { public boolean isPalindrome(String s) { StringBuilder str = new StringBuilder(); for(int i=0; i<s.length(); i++) { if(s.charAt(i)>=‘a‘&&s.charAt(i)<=‘z‘ || s.charAt(i)>=‘A‘&&s.charAt(i)<=‘Z‘ || s.charAt(i)>=‘0‘&&s.charAt(i)<=‘9‘) { str.append(s.charAt(i)); } } str = new StringBuilder(str.toString().toLowerCase()); int flag = 1; for(int i=0; i<str.length()/2; i++) { if(str.charAt(i) != str.charAt(str.length()-i-1)) { flag = 0; break; } } if(flag == 1) return true; else return false; } }
时间: 2024-10-10 02:40:56