[LeetCode] Palindrome Partitioning II
2014-04-24 17:55
411 查看
Total Accepted: 7469
Total Submissions: 43163
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 =
Return
Total Submissions: 43163
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
1since the palindrome partitioning
["aa","b"]could be produced using 1 cut.
public class Solution { public int minCut(String s) { int len = s.length(); if (len == 0) return 0; int[][] dp1 = new int[len][len]; for (int i = 0; i < len; i++) dp1[i][i] = 1; for (int k = 1; k < len; k++) { for (int i = 0; i < len - k; i++) { int j = i + k; if (((j == i+1) || (dp1[i+1][j-1] == 1)) && s.charAt(i) == s.charAt(j)) dp1[i][j] = 1; } } // mincut from 0 to i int[] dp2 = new int[len]; for (int i = 0; i < len; i++) dp2[i] = Integer.MAX_VALUE; dp2[0] = 0; for (int i = 0; i < len; i++) { if (dp1[0][i] == 1) dp2[i] = 0; else { for (int j = 1; j <= i; j++) { if (dp1[j][i] == 1) dp2[i] = Math.min(dp2[i], 1 + dp2[j - 1]); } } } return dp2[len - 1]; } }
相关文章推荐
- LEETCODE: Palindrome Partitioning II
- Leetcode 132. Palindrome Partitioning II
- leetcode 日经贴,Cpp code -Palindrome Partitioning II
- Leetcode Palindrome Partitioning II
- Leetcode -- Palindrome Partitioning II
- [Leetcode] Palindrome Partitioning II (Java)
- LeetCode Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- [leetcode-132]Palindrome Partitioning II(java)
- Leetcode-Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- leetcode——Palindrome Partitioning II
- LeetCode 132. Palindrome Partitioning II
- [Leetcode] Palindrome Partitioning II
- [Leetcode] Palindrome Partitioning II
- LeetCode 132 Palindrome Partitioning II--In C++
- LeetCode:Palindrome Partitioning II
- Palindrome Partitioning II Leetcode Java