1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc125.java 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-07-01 11:04 . 20190701
package code;
/*
* 125. Valid Palindrome
* 题意:判断是否为回文
* 难度:Easy
* 分类:Two Pointers, String
* 思路:两个指针。另一种是正则表达式替换数字,字母为空格,再反转,判断是否相等。
* Tips:记下另一种方法。第一种方法Bingo!
* lc5, lc9, lc125, lc131, lc234, lc647
*/
public class lc125 {
public static void main(String[] args) {
System.out.println(isPalindrome("A man, a plan, a canal: Panama"));
}
public static boolean isPalindrome(String s) {
int begin = 0;
int end = s.length()-1;
s = s.toLowerCase(); //转为小写
while(begin<end && !(helper(s.charAt(begin)))) begin++;
while(begin<end && !(helper(s.charAt(end)))) end--;
while(begin<end){
if(s.charAt(begin)==s.charAt(end)){
begin++;
end--;
while(begin<end && !(helper(s.charAt(begin)))) begin++;
while(begin<end && !(helper(s.charAt(end)))) end--;
}else{
return false;
}
}
return true;
}
public static boolean helper(Character ch){ //过滤非字符数字
if(Character.isAlphabetic(ch)||Character.isDigit(ch)) return true;
return false;
}
public boolean isPalindrome2(String s) {
String actual = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
String rev = new StringBuffer(actual).reverse().toString();
return actual.equals(rev);
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891