首页 > 代码库 > Longest Palindromic Substring
Longest Palindromic Substring
1 public class Solution { 2 /** 3 * @param s input string 4 * @return the longest palindromic substring 5 */ 6 public static String longestPalindrome(String s) { 7 if (s == null || s.isEmpty()){ 8 return null; 9 } 10 int left = 0; 11 int right = 0; 12 int len = s.length(); 13 int longest = 0; 14 for (int i = 0; i < len; i++){ 15 for (int j = i + 1; j <= len; j++){ 16 String cub = s.substring(i, j); 17 if (isPalindroma(cub) && cub.length() > longest){ 18 longest = cub.length(); 19 left = i; 20 right = j; 21 } 22 } 23 } 24 String result = s.substring(left, right); 25 return result; 26 27 } 28 public static boolean isPalindroma(String s){ 29 if (s == null || s.isEmpty()){ 30 return false; 31 } 32 int len = s.length() - 1; 33 for (int i = 0; i <= len; i++){ 34 if (s.charAt(i) != s.charAt(len - i)){ 35 return false; 36 } 37 } 38 return true; 39 } 40 }
Longest Palindromic Substring
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。