What is the longest palindromic substring problem on LeetCode?

更新于
2026-10-12 11:44:22
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计218个文字,预计阅读时间需要1分钟。

What is the longest palindromic substring problem on LeetCode?

javapublic class LongestPalindromicSubstring { public String longestPalindrome(String s) { if (s==null || s.length() <1) return ; int start=0, end=0; for (int i=0; i end - start) { start=i - (len - 1) / 2; end=i + len / 2; } } return s.substring(start, end + 1); }

private int expandAroundCenter(String s, int left, int right) { while (left >=0 && right LongestPalindromicSubstring.java

/** * 5. Longest Palindromic Substring * Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. * Example: * Input: "babad" * Output: "bab" * Note: "aba" is also a valid answer. * Input: "cbbd" * Output: "bb" * @param s * @return */ public static String longestPalindrome(String s) { if (s.length() == 1) { return s; } if (s.length() == 2) { return s.charAt(0) == s.charAt(1) ? s : ""; } String resultString = ""; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j < s.length(); j++) { String tmp = s.substring(i, j+1); if(tmp.equals(reverse(tmp)) && tmp.length()>= resultString.length()){ resultString = tmp; } } } return resultString; } private static String reverse(String s){ StringBuffer strBuffer = new StringBuffer(); for(int i=s.length()-1;i>=0;i--){ strBuffer.append(s.charAt(i)); } return strBuffer.toString(); }

What is the longest palindromic substring problem on LeetCode?

本文共计218个文字,预计阅读时间需要1分钟。

What is the longest palindromic substring problem on LeetCode?

javapublic class LongestPalindromicSubstring { public String longestPalindrome(String s) { if (s==null || s.length() <1) return ; int start=0, end=0; for (int i=0; i end - start) { start=i - (len - 1) / 2; end=i + len / 2; } } return s.substring(start, end + 1); }

private int expandAroundCenter(String s, int left, int right) { while (left >=0 && right LongestPalindromicSubstring.java

/** * 5. Longest Palindromic Substring * Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. * Example: * Input: "babad" * Output: "bab" * Note: "aba" is also a valid answer. * Input: "cbbd" * Output: "bb" * @param s * @return */ public static String longestPalindrome(String s) { if (s.length() == 1) { return s; } if (s.length() == 2) { return s.charAt(0) == s.charAt(1) ? s : ""; } String resultString = ""; for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j < s.length(); j++) { String tmp = s.substring(i, j+1); if(tmp.equals(reverse(tmp)) && tmp.length()>= resultString.length()){ resultString = tmp; } } } return resultString; } private static String reverse(String s){ StringBuffer strBuffer = new StringBuffer(); for(int i=s.length()-1;i>=0;i--){ strBuffer.append(s.charAt(i)); } return strBuffer.toString(); }

What is the longest palindromic substring problem on LeetCode?