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平台的开发环境的发展演变
· 安卓是以linux为基础的开放源码操作系统.因为安卓的开源等原因,所以现在市场上会有大量的APP可供使用,且各个方面都功能强大. · 也许是因为开源的原因,安卓过于碎片化.每个APP互相独立. ...
- ruby 删除文件
f = "app/assets/#{vm.uuid}.rrd" if FileTest::exist?(f) File.delete(f) end
- LightSpeed 的Left Join Bug解决方案
在使用LightSpeed对数据库进行Left Join或Right Join操作时,经常会报一些匪夷所思的异常. 明明表没有问题,表面上语句写的也没问题,可总是报错.看分析器里的SQL就知道了,是L ...
- Linux Crash/Hang on Bay Trail/J1900/N2940
近几年的linux kernel, 尤其是4.1以后,在Bay Trail平台上会随机挂起和死机,亲测j1900,死机非常频繁,而且死机前毫无征兆,直接就挂起了,console也没有相应. 这个问题在 ...
- 版本控制工具VSS使用介绍
什么是版本控制? 1.怎样对研发项目进行整体管理 2.项目开发小组的成员之间如何以一种有效的机制进行协调 3.如何进行对小组成员各自承担的子项目的统一管理 4.如何对研发小组各成员所作的修改进行统一汇 ...
- PAC学习框架
PAC学习框架是机器学习的基础.它主要用来回答以下几个问题: 什么问题是可以高效学习的? 什么问题本质上就难以学习? 需要多少实例才能完成学习? 是否存在一个通用的学习模型? PAC=probably ...
- uva 839 not so mobile——yhx
Not so Mobile Before being an ubiquous communications gadget, a mobile was just a structure made of ...
- 手把手教你如何用ZBrush刻画脸部
今天的ZBrush教程我们将参照一张效果图对模型进行脸部刻画.在进入课堂之前我们有必要对Layers层概念和操作有所了解,然后结合之前学习的雕刻笔刷对模型的特征表情给予重点刻画. 详细的视频教程地址请 ...
- ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS
题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...
- WPF数字输入框和IP地址输入框
数字输入框 简介 在业务中,我们经常需要限制用户的输入,比如限制输入长度,限制只能输入数字等等.限制输入长度WPF内置的TextBox已经帮我们解决了,但是限制输入数字却并未在WPF中内置解决方案.使 ...