https://leetcode.com/problems/valid-palindrome/#/description
问题描述:指定含有标点符号空格的字符串,判断是否为回文串。
思路解析:
[^A-Za-z0-9]正则表达式,过滤掉非数字和大小写字母的字符。
用到了StringBuffer的reverse()与正则表达式过滤的串比较相同则是回文串。
代码如下:
public class Solution {
public boolean isPalindrome(String s) {
String temp=s.replaceAll("[^A-Za-z0-9]","").toUpperCase();
String str=new StringBuffer(temp).reverse().toString();
return temp.equals(str);
}
}