Leetcode Palindrome Partitioning II
2015-04-21 00:21
281 查看
题目地址:https://leetcode.com/problems/palindrome-partitioning-ii/
题目解析:此问题可以使用动态规划,用一个数组保存前i个字符需要的最少cut数,前i+1个字符串的最小cut数为前j个字符所需的cut数(j+1到i个字符为回文)+1;
题目解答:
题目解析:此问题可以使用动态规划,用一个数组保存前i个字符需要的最少cut数,前i+1个字符串的最小cut数为前j个字符所需的cut数(j+1到i个字符为回文)+1;
题目解答:
public class Solution { public int minCut(String s) { if(s == null || s.length() == 0){ return 0; } int[] cutNum = new int[s.length()+1]; boolean[][] palindromeMap = new boolean[s.length()][s.length()]; cutNum[0] = -1; for(int i=1;i<=s.length();i++){ cutNum[i] = i-1; for(int j=0;j<=i-1;j++){ palindromeMap[j][i-1] = false; if(s.charAt(j) == s.charAt(i-1) && (i-1-j<=2 || palindromeMap[j+1][i-2])){ palindromeMap[j][i-1] = true; cutNum[i] = Math.min(cutNum[i], cutNum[j]+1); } } } return cutNum[s.length()]; } }
相关文章推荐
- LeetCode 132 Palindrome Partitioning II--In C++
- leetcode 日经贴,Cpp code -Palindrome Partitioning II
- Palindrome Partitioning II Leetcode Java
- Leetcode: Palindrome Partitioning II
- leetcode132 Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning && 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——Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- LeetCode132 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