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/L005_Longest_Palindromic_Substring.java at master · likevson/LeetCode Expand file tree/ Copy pathL005_Longest_Palindromic_Substring.javaMore file actions 41 lines (28 loc) · 692 Bytes / Copy pathL005_Longest_Palindromic_Substring.javaFile metadata and controls41 lines (28 loc) · 692 Bytes 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 package LeetCode; public class L005_Longest_Palindromic_Substring { public String longestPalindrome(String s) { if (s == null || s.length() == 0) { return ""; } int start = 0; int end = 0; for (int i = 0; i < s.length() - 1; i++) { int len1 = longest(s, i, i); int len2 = longest(s, i, i + 1); int len = Math.max(len1, len2); if (end - start < len) { start = i - (len - 1) / 2; end = i + len / 2; } } return s.substring(start, end + 1); } private int longest(String s, int left, int right) { while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { left--; right++; } return right - left - 1; } } |
| You are visitor 01402792 — 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. |