leetcode 5. Longest Palindromic Substring 最长回文子串的查找 + 按照length做DP
2017-08-30 11:50
513 查看
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.
Example:
Input: “cbbd”
Output: “bb”
题意很简单就不说了,就是寻找最长的回文子串。
最笨的方法就是找到所有的子串,然后判断回文与否,然后寻找最大回文子串;更加高明的方法就是使用DP来做,假设dp[i][j]表示字符串i到j的子串是否是会文字才,那么就可以得到推导式:
dp[i][j]=dp[i+1][j-1]
对于我需要注意的地方就是计算dp[i][j]的循环应该怎么写,像i:0-len,j:0-len,肯定不对,因为很多时候我遇到DP求解,都是半懂不懂。
针对这一道题 dp[i][j]=dp[i+1][j-1],可以理解为内层的字符子串决定着外层的字符子串,所以要有内向外计算,DP的思路就是有一个基本的小任务向外不断通过推导公式计算出大的任务,这个和递归的思想完全相反,递归是把大任务不断分解为小任务,然后计算并返回。假如换了一种思路的推导公式,就安装它的思路去写,比如最长公共子串
建议和这一道题一起学习leetcode 516. Longest Palindromic Subsequence 最长回文子序列 + DP动态规划 和 leetcode 647. Palindromic Substrings 回文子串的数量 和 leetcode 730. Count Different Palindromic Subsequences 动态规划DP
同时也建议leetcode 53. Maximum Subarray DP+最大子串和 一起学习
那么就没问题了,代码如下:
下面是C++的做法,就是DP,不过要学习的是,这道题的DP的递推公式是按照length来增加的。
代码如下:
Example:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example:
Input: “cbbd”
Output: “bb”
题意很简单就不说了,就是寻找最长的回文子串。
最笨的方法就是找到所有的子串,然后判断回文与否,然后寻找最大回文子串;更加高明的方法就是使用DP来做,假设dp[i][j]表示字符串i到j的子串是否是会文字才,那么就可以得到推导式:
dp[i][j]=dp[i+1][j-1]
对于我需要注意的地方就是计算dp[i][j]的循环应该怎么写,像i:0-len,j:0-len,肯定不对,因为很多时候我遇到DP求解,都是半懂不懂。
针对这一道题 dp[i][j]=dp[i+1][j-1],可以理解为内层的字符子串决定着外层的字符子串,所以要有内向外计算,DP的思路就是有一个基本的小任务向外不断通过推导公式计算出大的任务,这个和递归的思想完全相反,递归是把大任务不断分解为小任务,然后计算并返回。假如换了一种思路的推导公式,就安装它的思路去写,比如最长公共子串
建议和这一道题一起学习leetcode 516. Longest Palindromic Subsequence 最长回文子序列 + DP动态规划 和 leetcode 647. Palindromic Substrings 回文子串的数量 和 leetcode 730. Count Different Palindromic Subsequences 动态规划DP
同时也建议leetcode 53. Maximum Subarray DP+最大子串和 一起学习
那么就没问题了,代码如下:
public class Solution { public String longestPalindrome(String s) { if(s==null || s.length()<=1) return s; //dp[i][j]表示冲i到j的子串 boolean[][]dp=new boolean[s.length()][s.length()]; //i==j 表示一个字符的子串,是回文结构,当 i > j 认为是空串,也是回文 for(int i=0;i<s.length();i++) for(int j=0;j<s.length();j++) dp[i][j]= i>=j? true :false; int beg=0,end=0,maxLen=0; /* * 外层循环表示i和j的len,第二层循环是i的下标变化 * */ for(int len=1;len<=s.length();len++) { for(int i=0;i+len<s.length();i++) { int j=i+len; if(s.charAt(i)==s.charAt(j)) { dp[i][j]=dp[i+1][j-1]; if(dp[i][j] && maxLen< j-i+1) { maxLen=j-i+1; beg=i; end=j; } }else dp[i][j]=false; } } return s.substring(beg,end+1); } /* * * 最笨的方法实现 * 这个思路肯定超时 * * */ public String longestPalindromeStupid(String s) { if(s==null ||s.equals("")) return s; int maxLen=-1; String maxString=null; for(int i=0;i<s.length();i++) { for(int j=i;j<s.length();j++) { if(s.charAt(i)==s.charAt(j)) { String tar=s.substring(i, j+1); if(isPalindromic(tar) && tar.length() > maxLen) { maxLen=tar.length(); maxString=tar; } } } } return maxString; } private boolean isPalindromic(String tar) { for(int i=1;i<tar.length()/2;i++) { if(tar.charAt(i)!=tar.charAt(tar.length()-1-i)) return false; } return true; } public static void main(String[] args) { Solution solution=new Solution(); System.out.println(solution.longestPalindrome("babad"));; } }
下面是C++的做法,就是DP,不过要学习的是,这道题的DP的递推公式是按照length来增加的。
代码如下:
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> using namespace std; class Solution { public: string longestPalindrome(string s) { int n = s.length(); vector<vector<bool>> dp(n,vector<bool>(n, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dp[i][j] = i < j ? false : true; int maxLen = 1, beg = 0; for (int len = 1; len <= n; len++) { for (int i = 0; i + len < n; i++) { int j = i + len; if (s[j] == s[i]) { bcbb dp[i][j] = dp[i + 1][j - 1]; if (dp[i][j] && j - i + 1 > maxLen) { maxLen = j - i + 1; beg = i; } } else dp[i][j] = false; } } return s.substr(beg, maxLen); } };
相关文章推荐
- LeetCode 5 Longest Palindromic Substring(最长回文子串,暴力剪枝/DP/曼彻斯特算法)
- 最长回文子串 leetcode 05 DP解法
- LeetCode-5-Longest Palindromic Substring 最长回文子串DP
- leetcode 312. Burst Balloons 气球爆炸计算分数 + 一个按照length做动态规划DP的很棒的做法
- LeetCode:Longest Palindromic Substring 最长回文子串
- [LeetCode]33. Longest Palindromic Substring最长回文子串
- LeetCode Longest Palindromic Substring(最长回文子串)
- LeetCodeOJ_5_m_Longest Palindromic Substring(最长回文子串)
- LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium
- leetcode5 最长回文子串
- leetcode-5. Longest Palindromic Substring(寻找最长回文子串)
- LeetCode 5. Longest Palindromic Substring--最长回文子串
- 最长回文子串的查找
- Longest Palindromic Substring 在一个字符串里找最长回文子串@LeetCode
- LeetCode[5] 最长的回文子串
- LeetCode-最长回文子串
- 最长回文子串的查找
- 【LeetCode 214】最长回文子串
- Leetcode: 5 Longest Palindromic Substring 最长回文子串
- 2014再见 2015你好 【附带】[LeetCode]最长回文子串