天题系列: 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.

说明摘抄自ref http://blog.csdn.net/ljphhj/article/details/22573983

解题思路:

我们可以把这个问题转换成动态规划dp的问题

首先我们先定义几个变量,并对这几个量做一定的说明!为了方便理解,下面这些为伪码!!!

len  = str.length();   // 字符串的长度

int[] cuts = new int[len + 1]; //cuts数组,cuts[i] 表示 以 i 开头到len结尾的子串 要达到题意需要的最少切割数(这样子最终 cuts[0]就是我们要的结果)【初始化 cuts[i] = len - i, 因为最坏情况以 i 开头到len结尾的子串要切割数就是每个字符都切一次】

int[][] matrix = new  int[len][len]; //设置出一个邻接矩阵数组,它所表示的意思:如matrix[i][j] = true, 表示 子串 sub(i, j) 是满足回文字符串条件的!

那么判断matrix[i][j] 是否满足回文字符串的条件是:

matrix[i+1][j-1] == true (表示sub(i+1,j-1)是满足回文字符串) && str[i] == str[j]

或者

j - i < 2 && str[i] == str[j] (即如果j - i == 1时,为两个字符相等,如果j - i == 0时,为同一个字符)

这两种情况,我们都将matrix[i][j]设置成true,方便下一次的DP,并且我们可以求出最小的切割次数

cuts[i] = min{cuts[i], cuts[j+1] + 1};  状态转移方程式

这样最后cuts[0] - 1便为 字符串str的最小的切割数!!!!

这里cut的初始化比较特别

public class Solution {
    public int minCut(String s) {
        if(s==null||s.length()<2) return 0;
        int len = s.length();
        int [] cut = new int[len+1];
        boolean[][] matrix = new boolean[len][len];
        for(int i=0; i<len;i++){
            cut[i] = len-i; // not len-i-1
        }
        for(int i=len-1;i>=0;i--){
            for(int j=i;j<len;j++){
                if((s.charAt(i)==s.charAt(j)) &&((j-i<2)||  matrix[i+1][j-1])){
                    matrix[i][j] = true;
                    cut[i] = Math.min(cut[i],cut[j+1]+1);
                }
            }
        }
        return cut[0]-1;
    }
}
时间: 2024-10-27 04:59:56

天题系列: Palindrome Partitioning II的相关文章

[LeetCode]132.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&qu

[LintCode] Palindrome Partitioning II

Palindrome Partitioning II Given a string s, cut s into some substrings such that every substring is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Example For example, given s = "aab", Return 1 since the palind

【LeetCode】Palindrome Partitioning II

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 p

[LeetCode] Palindrome Partitioning II [12]

题目 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&qu

动态规划——Palindrome Partitioning II

Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "aab"Output: 1Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut. 状态:这个题的状态也非常好理解,dp[i]表示将s[0..i]分割成回

LeetCode: Palindrome Partitioning II 解题报告

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 pa

19. Palindrome Partitioning &amp;&amp; Palindrome Partitioning II (回文分割)

Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a&q

[email&#160;protected] [131/132] Palindrome Partitioning &amp; Palindrome Partitioning II

https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa&qu

132 Palindrome Partitioning II

132 Palindrome Partitioning II 这道题就是标识出s[i:j+1]是否为palindrome, 然后dp找出最小分割 class Solution: # @param {string} s # @return {integer} def minCut(self, s): LS = len(s) dp = [[False] * LS for i in range(0, LS)] ans = [1<<31 for i in range(0 ,LS)] for ls in

leetcode题目:Palindrome Partitioning 和Palindrome Partitioning II

题目一: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",