/**
* Created by qjb on 2016/1/30.
*
* 1、字符串为空的时候,也认为是回文数
* 2、考虑回文数要求的内容,字符、数字 直接用Character的子方法即可
* 3、这个题目麻烦在要同时考虑两个变量的变化,一个是font,一个是end,font负责从前向后遍历,end负责从后向前遍历,
* 遇到不满足情况的字符直接跳过;
* 4、遇到符合情况的字符,即Character.toLowerCase(s.charAt(front)) == Character.toLowerCase(s.charAt(end))要更新
* 遍历的条件;遇到不符合条件直接跳出循环
* 5、最关键的就是这个返回值了,因为(4)的break,所以最后可以通过end <= front的值来作为返回值
*/
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return true;
}
int front = 0;
int end = s.length() - 1;
while (front < end) {
while (front < s.length() && !isvalid(s.charAt(front))){ // nead to check range of a/b
front++;
}
if (front == s.length()) { // for emtpy string “.,,,”
return true;
}
while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
end--;
}
if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
break;
} else {
front++;
end--;
}
}
return end <= front;
}
private boolean isvalid (char c) {
return Character.isLetter(c) || Character.isDigit(c);
}
}
* Created by qjb on 2016/1/30.
*
* 1、字符串为空的时候,也认为是回文数
* 2、考虑回文数要求的内容,字符、数字 直接用Character的子方法即可
* 3、这个题目麻烦在要同时考虑两个变量的变化,一个是font,一个是end,font负责从前向后遍历,end负责从后向前遍历,
* 遇到不满足情况的字符直接跳过;
* 4、遇到符合情况的字符,即Character.toLowerCase(s.charAt(front)) == Character.toLowerCase(s.charAt(end))要更新
* 遍历的条件;遇到不符合条件直接跳出循环
* 5、最关键的就是这个返回值了,因为(4)的break,所以最后可以通过end <= front的值来作为返回值
*/
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return true;
}
int front = 0;
int end = s.length() - 1;
while (front < end) {
while (front < s.length() && !isvalid(s.charAt(front))){ // nead to check range of a/b
front++;
}
if (front == s.length()) { // for emtpy string “.,,,”
return true;
}
while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
end--;
}
if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
break;
} else {
front++;
end--;
}
}
return end <= front;
}
private boolean isvalid (char c) {
return Character.isLetter(c) || Character.isDigit(c);
}
}