Leetcode-Palindrome Partitioning II
2014-12-12 04:15
411 查看
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
Analysis:
DP
Solution:
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.
Analysis:
DP
Solution:
public class Solution { public int minCut(String s) { if (s.length()==0) return 0; int[] minCut = new int[s.length()+1]; minCut[0] = -1; boolean[][] valid = new boolean[s.length()][s.length()]; for (int i=0;i<s.length();i++){ valid[i][i]=true; } for (int i=1;i<=s.length();i++){ minCut[i] = minCut[i-1]+1; for (int j=i-2;j>=0;j--) if (s.charAt(j)==s.charAt(i-1) && (j==i-2 || valid[j+1][i-2])){ valid[j][i-1]=true; minCut[i] = Math.min(minCut[i],minCut[j]+1); } } return minCut[s.length()]; } }
相关文章推荐
- leetcode Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- [LeetCode]132 Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- Leetcode: Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- leetcode 日经贴,Cpp code -Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Leetcode---Palindrome Partitioning II
- 每日AC-LeetCode-palindrome-partitioning-ii --动态规划
- LeetCode Palindrome Partitioning II
- [leetcode刷题系列]Palindrome Partitioning II
- Leetcode_Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- leetcode Palindrome Partitioning II DP 深搜
- [LeetCode] Palindrome Partitioning II