LeetCode: Palindrome Partitioning II 解题报告
2014-12-06 22:28
204 查看
Palindrome Partitioning II
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.
View Code
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/MinCut_1206.java
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.
public class Solution { public int minCut(String s) { if (s == null || s.length() == 0) { return 0; } int len = s.length(); // D[i]: 前i 个字符切为回文需要的切割数 int[] D = new int[len + 1]; D[0] = -1; // P[i][j]: S.sub(i-j) is a palindrome. boolean[][] P = new boolean[len][len]; for (int i = 1; i <= len; i++) { // The worst case is cut character one by one. D[i] = i - 1; for (int j = 0; j <= i - 1; j++) { P[j][i - 1] = false; if (s.charAt(j) == s.charAt(i - 1) && (i - 1 - j <= 2 || P[j + 1][i - 2])) { P[j][i - 1] = true; D[i] = Math.min(D[i], D[j] + 1); } } } return D[len]; } }
View Code
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/MinCut_1206.java
相关文章推荐
- [leetcode] 285. Inorder Successor in BST 解题报告
- [Leetcode] 108. Convert Sorted Array to Binary Search Tree 解题报告
- [Leetcode] 82. Remove Duplicates from Sorted List II 解题报告
- [leetcode] 352. Data Stream as Disjoint Intervals 解题报告
- [Leetcode] 191. Number of 1 Bits 解题报告
- [leetcode]76. Minimum Window Substring@Java解题报告
- 【LeetCode】156.Binary Tree Upside Down(Medium)(加锁题)解题报告
- LeetCode: Next Permutation 解题报告
- leetCode解题报告之Copy List with Random Pointer
- 【LeetCode】744. Find Smallest Letter Greater Than Target 解题报告
- LeetCode 216. Combination Sum III 解题报告
- [Leetcode] 313. Super Ugly Number 解题报告
- [Leetcode] 319. Bulb Switcher 解题报告
- [leetcode] 163. Missing Ranges 解题报告
- [Leetcode] 119. Pascal's Triangle II 解题报告
- 【LeetCode】 Add Digits 解题报告
- leetcode 40. Combination Sum II 解题报告
- LeetCode解题报告--Search Insert Position
- Leetcode: LRU Cache 解题报告
- [LeetCode]Nth Highest Salary,解题报告