标签:turn The lse str har rip 返回 bst answer
Description:
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd" Output: "bb"
1,暴力法
判断字符串s的所有子串a是否为回文字符串,如果是,则返回字符串a与其长度l。最终返回长度最长的回文子串。
class Solution { public String longestPalindrome(String s) { String s1; String st = null; int maxLenrth = 0; if (s.length() == 1 || s.length() == 0) { return s; }else { for (int i = 0; i < s.length(); i++) { for (int j = i; j < s.length(); j++) { s1 = s.substring(i, j+1); if (isHw(s1)>maxLenrth) { maxLenrth = isHw(s1); st = s1; } } } return st; } }
//判断是否为回文 是返回其长度 否返回0 public int isHw(String s){ for (int i = 0; i < s.length()/2; i++) { if (s.charAt(i) != s.charAt(s.length()-i-1)) { return 0; } } return s.length(); } }
2,...
Longest Palindromic Substring(最长回文子串)
标签:turn The lse str har rip 返回 bst answer
原文地址:https://www.cnblogs.com/wkcode/p/10360433.html