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.

这种最小啊,最大啊,最长啊,etc肯定是用动态规划无疑了。

我之前已经知道如何判断一个字符串s中,s[i...j]是否是一个回文。

isPalindrome[i][j] = true iff s[i] == s[j] && isPalindrome[i + 1][j - 1] 或者 j - i <= 1

当我们得到这个matrix以后,我们可以认为这是一个邻接矩阵,也就是一个图。那么这个问题可以转换成一个源最短路径问题。那么最简单的就是用bfs了。

public int minCut(String s) {
boolean[][] isPalindrome = new boolean[s.length() + 1][s.length() + 1];
// Map<Integer, Set<Integer>> adjecent = new HashMap<>();
for (int i = 0; i <= s.length(); i++) {
isPalindrome[i][i] = true;
// adjecent.put(i, new HashSet<Integer>());
}
for (int i = s.length() - 1; i >= 0; i--) {
for (int j = i + 1; j <= s.length(); j++) {
isPalindrome[i][j] = s.charAt(i) == s.charAt(j - 1)
&& (j - i == 1 || isPalindrome[i + 1][j - 1]);
if (isPalindrome[i][j]) {
// adjecent.get(i).add(j);
}
}
} LinkedList<Integer> queue = new LinkedList<Integer>();
boolean[] visited = new boolean[s.length() + 1];
queue.add(0);
int head = 0;
int depth = 0;
while (!queue.isEmpty()) {
int w = queue.poll();
if (head == w) {
depth++;
head = -1;
}
for (int i = 0; i < isPalindrome[w].length; i++) {
if (isPalindrome[w][i] && i == s.length()) {
return depth - 1;
}
if (isPalindrome[w][i] && !visited[i]) {
visited[i] = true;
queue.add(i);
if (head == -1) {
head = i;
}
}
}
}
return -1;
}

结果这个弄的很郁闷,因为之前我一直用的时hash map来表示邻接矩阵,结果老超时,于是干脆来粗暴的,直接使用得到的二维数组来做,竟然过了。hash map应该给我O(1)的时间才对啊,为啥还不如数组呢?

于是我google了一下,结果发现求最小的cut居然本身也可以用动态规划来做。算了,哭晕在厕所里。

先看代码。

public int minCut(String s) {
boolean[][] isPalindrome = new boolean[s.length()][s.length()];
int[] cut = new int[s.length()]; for (int j = 0; j < s.length(); j++) {
cut[j] = j;
for (int i = 0; i <= j; i++) {
if (s.charAt(i) == s.charAt(j)
&& (j - i <= 1 || isPalindrome[i + 1][j - 1])) {
isPalindrome[i][j] = true;
if (i > 0) {
cut[j] = Math.min(cut[j], cut[i - 1] + 1);
} else {
cut[j] = 0;
}
}
}
}
return cut[s.length() - 1];
}

这区区几行就完了。可以观察到在求回文矩阵的部分是一样的。关键是那个cut数组。

cut[j]表示s[0...j]最小cut数。

在i移动的过程中,把i作为分割点。

那么 cut[j] = min(cut[i - 1] + 1) iif s[i...j] 是回文 for i = 0 ~ j

所以在最里面的一个循环(i 从 0 到j)中,既能求得isPalindrome[0...j][j],同时也能求得cut[j]了。

该算法还有一点,就是循环方向的选择很合理。

LeetCode 笔记24 Palindrome Partitioning II (智商碾压)的更多相关文章

  1. 【LeetCode】132. Palindrome Partitioning II

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

  2. 【leetcode刷题笔记】Palindrome Partitioning II

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

  3. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  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,Palindrome Partitioning II

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

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

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

  7. 【leetcode】Palindrome Partitioning II

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

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

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

  9. LeetCode: Palindrome Partitioning II 解题报告

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

随机推荐

  1. Xcode 文件删除拷贝 出现的问题

    当删除一个组的时候,不管是下面的两个选择,是彻底删除还是不彻底: 然后又要往工程里拷贝进去  同名  文件组,最好是选择Creat groups (因为创建groups就不会有import的时候,还需 ...

  2. 二维码扫描利用ZBar实现

    上次是根据系统的属性自己封装的一个二维码扫描,这次给大家介绍一下基于ZBar集成的类似于QQ二维码扫描界面的二维码扫描的效果.                                     ...

  3. Linux 基础知识----shell

    1.file title: #!/bin/bash 2.input: echo $1 echo $2 3.if # ifif [ "$1" = "N" ]the ...

  4. SAM4E单片机之旅——23、在AS6(GCC)中使用FPU

    浮点单元(Floating Point Unit,FPU),是用于处理浮点数运算的单元. 为使用FPU,除了需要启用FPU外,还需要对编译器进行设置,以使其针对浮点运算生成特殊的指令.虽然在Atmel ...

  5. linux修改open files数

    概要 linux系统默认open files数目为1024, 有时应用程序会报Too many open files的错误,是因为open files 数目不够.这就需要修改ulimit和file-m ...

  6. cocos2d-x之物理引擎初试

    发现问题:监听代码部分不能在onEnter()段书写 bool HelloWorld::init() { if ( !Layer::init() ) { return false; } visible ...

  7. putExtra方法

    [开篇骂几句:fuck]1.扯淡intent.putExtra()怎么使用?2.胡说intent.putExtra(); [扯淡:其实你在问它怎么用的时候,你要明白,你知道不知道这是个什么东东,有必要 ...

  8. POJ 3304 Segments --枚举,几何

    题意: 给n条线段,问有没有一条直线,是每条线段到这条直线上的投影有一个公共点. 解法: 有公共点说明有一条这条直线的垂线过所有线段,要找一条直线过所有线段,等价于从所有线段中任选两端点形成的直线存在 ...

  9. redis 一二事 - 设置过期时间,以文件夹形式展示key显示缓存数据

    在使用redis时,有时回存在大量数据的时候,而且分类相同,ID相同 可以使用hset来设置,这样有一个大类和一个小分类和一个value组成 但是hset不能设置过期时间 过期时间只能在set上设置 ...

  10. Resharper团队协作之TODO

    TODO 需求 首先我想跟大家分享一下我们团队的代码检查流程. 1. 项目经理随时会检查成员的代码,如果发现有不符合规范的代码,会在注释里面加todo.比如,假设leo的代码不符合规范,那么项目经理就 ...