[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”,”b”] could be produced using 1 cut.

思路

此题可以用动态规划求解。isPal[j][i]表示字符串s的子串s[j…i]是否为回文串,cut[i]表示子串s[0…i]所需要的最小分割数

代码

    /**------------------------------------
    *   日期:2015-03-02
    *   作者:SJF0115
    *   题目: 132.Palindrome Partitioning II
    *   网址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        int minCut(string s) {
            int size = s.size();
            if(size == 0){
                return 0;
            }//if
            // isPal[i][j]表示字符串s的子串s[i,j]是否为回文串
            bool isPal[size][size];
            memset(isPal,0,sizeof(isPal));
            // cut[j]表示子串s[0,j]所需要的最小分割数
            int cut[size];
            // cut[0,i]
            for(int i = 0;i < size;++i){
                // [0,i]最多分割i次
                cut[i] = i;
                // 判断s[j,i]是否是回文串
                for(int j = 0;j <= i;++j){
                    // s[j,i]是回文串
                    if(s[j] == s[i] && (i - j <= 1 || isPal[j+1][i-1])){
                        isPal[j][i] = true;
                        // s[0,i]是回文串
                        if(j == 0){
                            cut[i] = 0;
                        }//if
                        else{
                            cut[i] = min(cut[i],cut[j-1]+1);
                        }//else
                    }//if
                }//for
            }//for
            return cut[size-1];
        }
    };

    int main(){
        Solution s;
        string str("cabababcbc");
        cout<<s.minCut(str)<<endl;
        return 0;
    }

运行时间

时间: 2024-12-13 19:55:52

[LeetCode]132.Palindrome Partitioning II的相关文章

[LeetCode]131.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","a",&q

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",

[LeetCode] Valid Palindrome II 验证回文字符串之二

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. N

[LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output:

[LeetCode] Largest Palindrome Product 最大回文串乘积

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example: Input: 2 Output: 987 Explanation: 99 x 91 = 9009, 9009 % 1337 = 987 Note: The

LeetCode 9 Palindrome Number (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

[LeetCode]234.Palindrome Linked List

题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 思路 利用双指针法找到链表中点位置,链表中点以后的的元素(不包括中点元素)翻转,再跟链表中点位置以前的元素一一匹配. 代码 /*--------------------------------------- * 日期:2015-07-18 * 作者:SJF01

[LeetCode]40.Combination Sum II

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

[LeetCode]9.Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of usin