Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
给定一个字符串,求出其最长回文子串。
public class Solution { public String longestPalindrome(String s) { if (s.length() == 0) { return null; } if (s.length() == 1) { return s; } String longest = s.substring(0, 1); for (int i = 0; i < s.length(); i++) { String t1 = helper(i, i, s); if (longest.length() < t1.length()) { longest = t1; } String t2 = helper(i, i+1, s); if (longest.length() < t2.length()) { longest = t2; } } return longest; } private String helper(int beg, int end, String s) { while (beg>-1 && end<s.length() && s.charAt(beg)==s.charAt(end)) { beg--; end++; } return s.substring(beg+1, end); } }
相关推荐
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
数据结构问题:“最长回文子串问题”(Longest Palindromic Substring),详细解法和代码实现; 数据结构问题:“最长回文子串问题”(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. ...
Longest-Palindromic-Substring(最长回文子串) 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 Sample 1 输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案。 Sample 2 输入...
Given a string of length n, write a function to find the longest palindromic substring. Example: input = "babad" -> output = "bab" or "aba" Problem 2: Given a sorted array of integers, write a ...
c c语言_leetcode 0005_longest_palindromic_substring.zip
java入门 java_leetcode题解之005_Longest_Palindromic_Substring
c语言入门 c语言_leetcode题解05-longest-palindromic-substring.c
js js_leetcode题解之5-longest-palindromic-substring.js
* Solution of Longest Palindromic Substring in Java:该题目要求找到最长的回文子串,实现方法使用了动态规划的方法。 * Word Break、Word Break II:该题目要求将字符串分割成单词,实现方法使用了动态规划和...
longest palindromic substring是一个经典的字符串问题,要求找到字符串中最长的回文子串。可以使用动态规划或Manacher算法来解决该问题。 4. Solution Word Break 单词断词是一个自然语言处理问题,要求将字符串...
Palindromic Substring 6.ZigZag Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 ...
leetcode 分类leetcode ...Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:Longest Substring Without Repeating Characters 优先队列 #23:Merge k Sorted Lists
5. **Longest Palindromic Substring** (Medium) 该题要求找到给定字符串中最长的回文子串。可以使用动态规划或Manacher's Algorithm来解决。动态规划思路是构建一个二维数组,其中dp[i][j]表示从索引i到j的子串是否...
Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge...
Palindromic Substring 中等 回文 ZigZag Conversion 中等 矩阵 重要 Reverse Integer 简单 字串 String to Integer (atoi) 中等 字串 麻烦 Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 ...
Palindromic Substring 006. ZigZag Conversion 007. Reverse Integer 008. String to Integer 009. Palindrome Number 010. Regular Expression Matching 011. Container With Most Water 012. Integer to Roman ...
leetcode题库 ...Palindromic Substring 30.1% Medium 0006 ZigZag Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 String to Integer (atoi) 15.5% Medium 0009 Palindrome Number 49.4% Easy
Palindromic Substring 中等 6 ZigZag Conversion 中等 7 Reverse Integer 简单 8 String to Integer (atoi) 中等 9 Palindrome Number 简单 11 Container With Most Water 中等 12 Integer to Roman 中等 13 Roman ...
Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7.Reverse Integer 8.String to Integer (atoi) 9.Palindrome Number 10.Regular Expression Matching 11.Container With Most Water 12.Integer ...