Longest Palindromic Substring

Posted 崔宏宇_chy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Longest Palindromic Substring

5. Longest Palindromic Substring

[Leetcode]Longest Palindromic Substring

LeetCode:Longest Palindromic Substring

leetcode--5. Longest Palindromic Substring

#Leetcode# 5. Longest Palindromic Substring