LeetCode 题解(90): Palindrome Partitioning II
来源:互联网 发布:出租屋网络不稳定 编辑:程序博客网 时间:2024/05/28 11:30
题目:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
动态规划是一定的。先记录最坏的cut情况,即到第i个字母为止需要切 i 刀。比如s = "abcdefg",则i = 0时需要切0刀,i=1时需要切1刀,以此类推。存在mincut[]中,待以后更新。
然后通过判断是否为Palindrome来更新mincut。首先初始化一个二维矩阵palindrome[][],其中每个元素均为false。此矩阵作为判断palindrome的动态规划查表。
递推公式为:for i = [1, s.length()-1], j = [0, i]
if j - 1 >= 0:
mincut[i] = min(mincut[i], mincut[j-1] + 1)
else:
mincut[i] = 0
其中并不是每个j都需要更新mincut[],而只有当s[j:i]为Palindrome的时候才需计算,由此省去大量计算时间。判断s[j:i]为Palindrome的条件为:
if s[j] == s[i] and (i - j < 2 or palindrome[j+1][i-1] == true) (此处使用了动态规划查表palindrome,又省去了大量时间)。
C++版:
class Solution {public: int minCut(string s) { if(s.length() <= 1) return 0; vector<int> mincut(s.length()); for(int i = 0; i < s.length(); i++) { mincut[i] = i; } vector<vector<bool>> palindrome; for(int i = 0; i < s.length(); i++) { vector<bool> row(s.length()); palindrome.push_back(row); } for(int i = 1; i < s.length(); i++) { for(int j = i; j >= 0; j--) { if(s[j] == s[i] && (i - j < 2 || palindrome[j+1][i-1])) { palindrome[j][i] = true; if(j - 1 >= 0) mincut[i] = min(mincut[i], mincut[j-1]+1); else mincut[i] = 0; } } } return mincut[s.length()-1]; }};
Java版:
public class Solution { public int minCut(String s) { if(s.length() == 0) return 0; int[] mincut = new int[s.length()]; for(int i = 0; i < s.length(); i++) mincut[i] = i; boolean[][] palindrome = new boolean[s.length()][s.length()]; for(int i = 1; i < s.length(); i++) { for(int j = i; j >= 0; j--) { if(s.charAt(i) == s.charAt(j) && (i - j < 2 || palindrome[j+1][i-1])) { palindrome[j][i] = true; if(j - 1 >= 0) mincut[i] = Math.min(mincut[i], mincut[j-1] + 1); else mincut[i] = 0; } } } return mincut[s.length()-1]; }}
Python版:
class Solution: # @param {string} s # @return {integer} def minCut(self, s): if len(s) == 0: return 0 mincut = [i for i in range(len(s))] palindrome = [] for i in range(len(s)): palindrome.append([False] * len(s)) for i in range(1, len(s)): for j in range(i,-1,-1): if s[i] == s[j] and (i - j < 2 or palindrome[j+1][i-1]): palindrome[j][i] = True if j - 1 >= 0: mincut[i] = min(mincut[i], mincut[j-1] + 1) else: mincut[i] = 0 return mincut[len(s)-1]
- LeetCode 题解(90): Palindrome Partitioning II
- Palindrome Partitioning II|leetcode题解
- LeetCode - Palindrome Partitioning II 题解
- LeetCode题解:Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- LeetCode - Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- JOBDU-OJ 1008 最短路径问题
- newFCK函数,FCKEditor编辑器在轻开平台中的使用例子
- 判断是否点击在listctrl的checkbox上
- inline-block元素间的间隙问题
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- LeetCode 题解(90): Palindrome Partitioning II
- 1018. 锤子剪刀布 (20):三者按字母序求最值
- 分享一些目前可用的谷歌IP
- ListView嵌套GridView使用详解及注意事项
- Spring3学习笔记之(spring core之DI循环依赖)
- JAVA菜鸟入门(17) 有排序的TreeSet和无排序的HashSet, LinkedHashSet
- jspsmartupload如何使用?
- Spring3学习笔记之(Spring core 之DI 更多知识点(延迟加载和自动装配))
- 方案四成功了奥