GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/code/lc125.java at master · mJackie/leetcode Expand file tree/ Copy pathlc125.javaMore file actions 44 lines (42 loc) · 1.51 KB / Copy pathlc125.javaFile metadata and controls44 lines (42 loc) · 1.51 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |