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,将s分割成一些子串,使每个子串都是回文。

比如,给出字符串s = "aab",

返回 1, 因为进行一次分割可以将字符串s分割成["aa","b"]这样两个回文子串

使用动态规划解决这道问题,我们需要初始化两个数组,
一个int数组cuts[i]纪录前i个字符最小的切割次数,
一个boolean数组dp[i][j]用于纪录从第i个位置到第j个位置是否是回文。
 
怎样用动态规划的思想判断从i到j之间的字符是否是回文呢,大致分为三种情况。
(1)如果i == j,也就是i和j是同一个位置的字符,一个字符肯定是回文。
(2)如果i和j是相邻的两个位置并且字符相等,i= j + 1,s.charAt(i) == s.charAt(j),  则是回文。
(3)如果i和j不想等也不相邻,如果dp[j + 1][i - 1]是回文,并且s.charAt(i) == s.charAt(j),  则是回文。
 
cuts[i]= i 设置最大的切割次数,前i个字符如果切割,最多切i次。
如果i到j之间是回文串,如果j大于0,说明从起始点到i之间右被切割出来一块i~j的回文,
有可能i和j是相邻的,也有可能是相隔了几个字符的,
前i个字符被切割的最小次数为 cuts[i]  和 cuts[j -1] +1的较小值,
这里的1指得是前i个字符中,从j到i这一块最少切割出来一次。
如果j =0,说明前i个字符串本身就是回文串,不需要在次切割,最小的切割次数为0
public class Solution {
public int minCut(String s) {
int n = s.length();
int[] cuts = new int[n];
boolean[][] dp = new boolean[n][n]; for (int i = 0; i < n; i++) {
cuts[i] = i;
for (int j = 0; j <= i; j++) {
if (s.charAt(i) == s.charAt(j) && (i - j <= 1 || dp[j + 1][i - 1])) {
dp[j][i] = true; if (j > 0) {
cuts[i] = Math.min(cuts[i], cuts[j - 1] + 1);
} else {
cuts[i] = 0;
}
} }
}
return cuts[n -1];
}
}

Palindrome Partitioning II Leetcode的更多相关文章

  1. Palindrome Partitioning II Leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  2. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  3. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  4. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  6. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  7. LeetCode: Palindrome Partitioning II 解题报告

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  8. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

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

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

随机推荐

  1. MYSQL 常用函数(数学、字符串、日期时间、系统信息、加密)

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...

  2. 如何修改Linux主机名

    Linux 下什么都比较麻烦,就连修改主机名也不例外.我们就下文说一下具体方法. Linux 安装好后,其默认的主机名是 localhost.修改 Linux 主机名需要3步. 使用 hostname ...

  3. mysql-python

    sudo pip install MySQL-python centos安装 python-dev包提示No package python-dev available: 出现此问题的原因是python ...

  4. AWK命令的用法

    1.awk命令简介: awk是一种可以处理数据.产生格式化报表的语言,功能十分强大. awk的工作方式是读取数据,将每一行数据视为一条记录(record)每笔记录以字段分隔符分成若干字段,然后输出各个 ...

  5. python版本升级及pip部署方法

    Python版本升级 CentOS 6.3自带的Python版本为2.6,首先需要升级到2.7版本.由于旧版本的Python已被深度依赖,所以不能卸载原有的Python,只能全新安装. 1.下载Pyt ...

  6. windows下php连接sqlserver2008

    如果你需要和sql server通信需要到http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx自行下载微软提供的The SQL Server ...

  7. Selenium 代码收集

    [转载]使用Selenium2测试含有iframe的Ajax网页  原文地址:http://www.cnblogs.com/hexin0614/archive/2012/03/24/2415670.h ...

  8. ubuntu使用root用户登录/切换root权限

    ubuntu系统默认root用户是不能登录的,密码也是空的. 如果要使用root用户登录,必须先为root用户设置密码 打开终端,输入:sudo passwd root 然后按回车 此时会提示你输入密 ...

  9. 获取Executor提交的并发执行的任务返回结果的两种方式/ExecutorCompletionService使用

    当我们通过Executor提交一组并发执行的任务,并且希望在每一个任务完成后能立即得到结果,有两种方式可以采取: 方式一: 通过一个list来保存一组future,然后在循环中轮训这组future,直 ...

  10. JQuery Easy Ui dataGrid 数据表格

    数据表格 - DataGrid 英文文档:http://www.jeasyui.com/documentation/index.php# 继承$.fn.panel.defaults,使用$.fn.da ...