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 — Java/Others/Palindrome.java at master · feiteng/Java Expand file tree/ Copy pathPalindrome.javaMore file actions 50 lines (42 loc) · 1.38 KB / Copy pathPalindrome.javaFile metadata and controls50 lines (42 loc) · 1.38 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 45 46 47 48 49 50 package Others; class Palindrome { private String reverseString(String x) { // *helper method StringBuilder output = new StringBuilder(x); return output.reverse().toString(); } public boolean FirstWay(String x) { // *palindrome method, returns true if palindrome if (x == null || x.length() <= 1) return true; return x.equalsIgnoreCase(reverseString(x)); } public boolean SecondWay(String x) { if (x.length() == 0 || x.length() == 1) return true; if (x.charAt(0) != x.charAt(x.length() - 1)) return false; return SecondWay(x.substring(1, x.length() - 1)); } /** * This method ignores all non-alphanumeric characters and case runs in O(n) * where n is the length of s * * @param s String to check * @return true if s is palindrome else false */ public boolean isPalindrome(String s) { s = s.toLowerCase().trim(); StringBuilder sb = new StringBuilder(); for (char c : s.toCharArray()) { if (Character.isLetter(c) || Character.isDigit(c)) sb.append(c); } s = sb.toString(); int start = 0; int end = s.length() - 1; while (start <= end) { if (s.charAt(start++) != s.charAt(end--)) return false; } return true; } } |
| You are visitor 01402520 — 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. |