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. 从Paxos到ZooKeeper-二、ZooKeeper和Paxos

    ZooKeeper为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如tong'yi统一命名服务.配置管理和分布式锁等分布式的基础服务.在解决分布式数据一致性方面,ZooKeeper并没有直接采用 ...

  2. (翻译)如何对python dict 类型按键(keys)或值(values)排序

    如何对dict类型按键(keys)排序(Python 2.4 或更高版本): mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':3} for key in ...

  3. python学习笔记-(十)面向对象基础

    面向对象相关知识简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...

  4. CentOS7搭建hadoop2.6.4+HBase1.1.6

    环境: CentOS7 hadoop2.6.4两个节点:master.slave1 HBase1.1.6 过程: hadoop安装目录:/usr/hadoop-2.6.4 master节点,hadoo ...

  5. C++ 以费波纳茨数列为权重的加权均值计算方法 wMA

    #pragma once #include <iostream> using namespace std; template <typename T> double *wMA( ...

  6. 使用 Object.create 创建对象,super 关键字,class 关键字

    ECMAScript 5 中引入了一个新方法:Object.create().可以调用这个方法来创建一个新对象.新对象的原型就是调用 create 方法时传入的第一个参数: var a = {a: 1 ...

  7. ecshop 订单-》订单状态

    /** * 取得状态列表 * @param string $type 类型:all | order | shipping | payment */ function get_status_list($ ...

  8. python 多线程学习

    多线程(multithreaded,MT),是指从软件或者硬件上实现多个线程并发执行的技术 什么是进程? 计算机程序只不过是磁盘中可执行的二进制(或其他类型)的数据.它们只有在被读取到内存中,被操作系 ...

  9. flask安装及第一个程序

    1.flask是一个轻量级的python web框架 ·1.Flask 依赖两个外部库: Jinja2 模板引擎和 Werkzeug WSGI 套件 ·2.安装: # easy_install fla ...

  10. Django笔记-数据库操作(多对多关系)

    1.项目结构 2.关键代码: data6.settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', ' ...