class Solution {
public boolean isPalindrome(String s) {
boolean flag = true;
// 对原字符串进行单个字符的转化
int len = s.length();
String startString = "";
for(int i = 0 ; i < len; i++){
char single = s.charAt(i);
if(single >= 48 && single <= 57){
startString += single;
}
else if(Character.isUpperCase(single)){
// 是大写字母
single = Character.toLowerCase(single);
startString += single;
}
else if(Character.isLowerCase(single)){
// 是小写字母
startString += single;
}
}
// // startString中存储着字符串的所有数字字母字符的小写
// String startString = sb.toString();
String reverseString = "";
for(int i = len - 1; i >= 0; i--){
char single = s.charAt(i);
if(single >= 48 && single <= 57){
reverseString += single;
}
else if(Character.isUpperCase(single)){
// 是大写字母
single = Character.toLowerCase(single);
reverseString += single;
}
else if(Character.isLowerCase(single)){
// 是小写字母
reverseString += single;
}
}
int reverseStringLength = reverseString.length();
int index = 0;
flag = startString.equals(reverseString);
return flag;
}
}
不熟悉的点:
ASCII: 数字是48~57 大写字母是65 ~ 90 小写字母是97 ~ 122
一些api:
Character.isUpperCase(char single)判断是否是大写字母
Character.toLowerCase(char single)转换为小写字母
String A.equals(String B) // 判断两个字符串是否先相等,返回布尔值
StringBuffer sb = new StringBuffer(); // 可以暂存字符串,通过append添加,toString转换成String型
StringBuffer sb = new StringBuffer(String s).reverse(); // 直接将该字符串转置
Character.isLetterOrDigit(char single); // 判断该char型的字符是否是字母或者数字