- 浏览: 183543 次
- 性别:
- 来自: 济南
文章分类
最新评论
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。实现代码如下:
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]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
C#,回文分割问题(Palindrome Partitioning Problem)算法与源代码 1 回文串 “回文串”是一个正读和反读都一样的字符串,初始化标志flag=true,比如“level”或者“noon”等等就是回文串。 2 回文分割问题 给定...
python python_leetcode题解之132_Palindrome_Partitioning_II
javascript js_leetcode题解之132-palindrome-partitioning-ii.js
python python_leetcode题解之131_Palindrome_Partitioning
javascript js_leetcode题解之131-palindrome-partitioning.js
leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 ...Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
1. 项目源代码:可能有一个名为"PalindromePartitioning.java"的文件,其中包含了用Java实现的回文分区算法。 2. 测试用例:可能有测试文件用于验证算法的正确性,比如"TestPalindromePartitioning.java"。 3. 解释...
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-按类别 看了一些leetcode刷题指南,总结一下两个重点,一是最好按照类别刷题,总结思路;...Partitioning (hard) 212 Word Search II (hard) DFS /二叉树 the difference between df
gas station leetcode 在牛客网上的在线编程...palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
Maximum Subarray, 删除重复的排序数组, 删除重复的排序数组 2, 查找没有重复的最长子串, 包含两个独特字符的最长子串, Palindrome Partitioning 这些高级算法和经典问题是程序员在代码面试中经常遇到的问题,了解...
1. **Palindrome Partitioning (mine).cpp** - 这个文件是关于LeetCode的第131题“回文分割”。该问题要求将一个字符串分割成多个回文子串。解冑通常涉及深度优先搜索或动态规划,寻找所有可能的分割方案并检查是否...
Palindrome_Partitioning DP,避免递归调用,利用数组储存已算出来的数值,由后向前逐渐增加字符串处理 Palindrome_Partitioning_2 同上 Sum_Root_to_Leaf_Numbers DFS Surrounded_Regions 由四周‘O’开始向内检索...
3. 题目131 - "Palindrome Partitioning" 这是一道回文子串分割问题,要求将一个字符串分割成多个回文子串。常见的解法是动态规划,创建一个二维数组来存储子问题的最优解。在Java中,可以使用StringBuilder和...
分割回文串(Palindrome Partitioning)**:这是一个关于字符串处理的题目,要求将一个字符串分割成若干个回文子串。主要涉及的算法是深度优先搜索(DFS),通过递归的方式检查所有可能的分割方案,判断是否都是回文。 ...
对于更高级的题目,可能会涉及图论或动态规划,比如`0131_palindrome_partitioning.py`可能涉及回文子串分割,这通常会用到动态规划来避免重复计算。 在PythonAlgo项目中,每个问题的解决方案都提供了清晰的思路和...