`

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.

给定一个字符串s,用最少的次数将它拆分成多个回文子串。我们用动态规划来解决。首先我们用一个二维的布尔数组isPalin[][]记录当前子串是否是回文串,例如isPalin[i][j] = true,就代表了字符串中从字符i到字符j是回文子串。(当s.charAt(i) == s.charAt(j)并且i + 1 >= j - 1的时候,也就是对应了’aa‘和’a?a‘这两种情况,此时肯定为回文串)。用数组dp[]来记录每个阶段的最小值,如果检测到从第一个字符到当前字符为回文子串,那么dp[j] = 0, 因为不需要拆分所以为0; 如果从字符i开始到当前字符为回文子串并且i > 0,此时dp[j] = dp[i - 1] + 1。实现代码如下:
public class Solution {
    public int minCut(String s) {
        if(s == null) return 0;
        boolean[][] isPalin = new boolean[s.length()][s.length()];
        int[] dp = new int[s.length()];
        for(int i = 0; i < dp.length; i++) {
            int min = i;
            for(int j = 0; j <= i; j++) {
                if(s.charAt(j) == s.charAt(i) && (j + 1 >= i - 1 || isPalin[j + 1][i - 1])) {
                    isPalin[j][i] = true;
                    min = (j == 0) ? 0 : Math.min(min, dp[j - 1] + 1);
                }
            }
            dp[i] = min;
        }
        return dp[s.length() - 1];
    }
}
分享到:
评论

相关推荐

    python-leetcode题解之132-Palindrome-Partitioning-II

    在众多的LeetCode题库中,“Palindrome Partitioning II” 是一个有趣的字符串处理问题,属于动态规划和字符串分割的范畴。这个问题要求我们编写一个函数,接受一个字符串s作为输入,然后找到使字符串s的子串都是...

    js-leetcode题解之132-palindrome-partitioning-ii.js

    JavaScript解决方案专题——LeetCode题解之132题——分割回文串 II(palindrome-partitioning-ii.js) 在本篇文章中,我们将会探讨LeetCode中的132题——分割回文串 II。此题目要求我们对给定的字符串进行分割,...

    js-leetcode题解之131-palindrome-partitioning.js

    在JavaScript中,处理leetcode上的编程题目时,正确理解和解决"131.Palindrome Partitioning"这类字符串划分问题是一项重要的技能。本题要求编写一个函数,该函数将给定的字符串划分为尽可能多的回文子字符串,并...

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 ...Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar

    python-leetcode题解之131-Palindrome-Partitioning

    4. Palindrome Partitioning(回文划分):是本题的核心问题,即要求将给定的字符串划分为尽可能多的回文子串。回文是指正读和反读都相同的字符串。 5. 动态规划(Dynamic Programming):一种算法设计方法,它将...

    palindrome-partitioning:动态规划 | 第 17 组(回文分区)(http

    1. 项目源代码:可能有一个名为"PalindromePartitioning.java"的文件,其中包含了用Java实现的回文分区算法。 2. 测试用例:可能有测试文件用于验证算法的正确性,比如"TestPalindromePartitioning.java"。 3. 解释...

    數位之和leetcode-leetcode-cpp:我的LeetCodeC++答案

    partitioning - regex - sudoku solver: 37 排序 - merge sort - quick sort - insertion sort - selection sort - counting sort 位操作 - find the only element which exists once/twice... - count

    leetcode力扣是什么-leetcode:leetcodebytags按自己整理的一些类别

    leetcode力扣是什么 leetcode-按类别 看了一些leetcode刷题指南,总结一下两个重点,一是最好按照类别刷题,总结思路;...Partitioning (hard) 212 Word Search II (hard) DFS /二叉树 the difference between df

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    gas station leetcode 在牛客网上的在线编程...palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca

    10个最常见的Java算法.doc

    Maximum Subarray, 删除重复的排序数组, 删除重复的排序数组 2, 查找没有重复的最长子串, 包含两个独特字符的最长子串, Palindrome Partitioning 这些高级算法和经典问题是程序员在代码面试中经常遇到的问题,了解...

    Leetcode部分解答(126题)

    1. **Palindrome Partitioning (mine).cpp** - 这个文件是关于LeetCode的第131题“回文分割”。该问题要求将一个字符串分割成多个回文子串。解冑通常涉及深度优先搜索或动态规划,寻找所有可能的分割方案并检查是否...

    leetcode添加元素使和等于-leetcode:leetcode

    Palindrome_Partitioning DP,避免递归调用,利用数组储存已算出来的数值,由后向前逐渐增加字符串处理 Palindrome_Partitioning_2 同上 Sum_Root_to_Leaf_Numbers DFS Surrounded_Regions 由四周‘O’开始向内检索...

    Selective_Leetcode_solutions

    3. 题目131 - "Palindrome Partitioning" 这是一道回文子串分割问题,要求将一个字符串分割成多个回文子串。常见的解法是动态规划,创建一个二维数组来存储子问题的最优解。在Java中,可以使用StringBuilder和...

    leetcode338-LeetCode:力码

    分割回文串(Palindrome Partitioning)**:这是一个关于字符串处理的题目,要求将一个字符串分割成若干个回文子串。主要涉及的算法是深度优先搜索(DFS),通过递归的方式检查所有可能的分割方案,判断是否都是回文。 ...

    PythonAlgo:用Python解决Leetcode问题

    对于更高级的题目,可能会涉及图论或动态规划,比如`0131_palindrome_partitioning.py`可能涉及回文子串分割,这通常会用到动态规划来避免重复计算。 在PythonAlgo项目中,每个问题的解决方案都提供了清晰的思路和...

Global site tag (gtag.js) - Google Analytics