LeetCode 笔记24 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.
这种最小啊,最大啊,最长啊,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 (智商碾压)的更多相关文章
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
随机推荐
- Android keystore 密码忘记了的找回办法
keystore密码忘记了,准备给自己的应用发布一个新版本,在apk打包时,发现之前的用的keystore密码忘了.如果换一个keystore,则之前已经安装应用的用户就必须手工卸载原应用才能安装,非 ...
- eclipse常用快捷键及调试方法(虽然现在看不懂,但是感觉以后肯定会用到,先转了)
常用快捷键 Eclipse最全快捷键,熟悉快捷键可以帮助开发事半功倍,节省更多的时间来用于做有意义的事情. Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl ...
- 烂泥:ubuntu中使用virt-manager图形化新建虚拟机
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上一篇文章介绍了,如何在ubuntu下安装KVM的虚拟机管理器virt-manager,这篇文章我们来介绍,如何在图形界面下使用virt-manager ...
- 02_嵌套矩形(DAG最长路问题)
来源:刘汝佳<算法竞赛入门经典--训练指南> P60 问题2: 问题描述:有n个矩形,每个矩形可以用两个整数a,b描述,表示它们的长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中的条件 ...
- Add Binary
Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- 如何用dos命令运行testng
写好的自动化程序怎么让它运行呢,总不能每次都启动eclipse吧,下面就先介绍一种用dos命令运行testNG的方法. 1.把项目打成jar吧,我用的是Fat jar工具. 2.在电脑的某个盘建一个文 ...
- 事件查看器常见ID代码解释
ID 类型 来 源 代 表 的 意 义 举 例 解 释 信息 Serial 在验证 \Device\Serial1 是否确实是串行口时,系统检测到先进先出方式(fifo).将使用该方式. 错误 W ...
- 如何用ZBrush快速雕刻LOL中的Lissandra
话说<魔兽>还有1天就上映了,热爱这款游戏的你想必早已按耐不住了吧,别急,再耐心等一下.不过今天我们要讲的,不是魔兽,而是另一款很多人为 之疯狂的游戏—英雄联盟,也就是你们熟悉的LOL啦, ...
- BZOJ 2186 [Sdoi2008]沙拉公主的困惑 【逆元】
题意:求中互质的数的个数,其中. 分析:因为,所以,我们很容易知道如下结论 对于两个正整数和,如果是的倍数,那么中与互素的数的个数为 本结论是很好证明的,因为中与互素的个数为,又知道, ...