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 ...
随机推荐
- C#输入输出重定向
当 Process 将文本写入其标准流中时,通常将在控制台上显示该文本.通过重定向 StandardOutput 流,可以操作或取消进程的输出.例如,可以筛选文本.用不同方式将其格式化,也可以将输出同 ...
- Profiling MySQL queries from Performance Schema
转自:http://www.percona.com/blog/2015/04/16/profiling-mysql-queries-from-performance-schema/ When opti ...
- ehcache整合spring注解方式
一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件 ...
- 使用Number.parseFloat引发的悲剧
起因: 前几天,项目中有人用了Number.parseFloat(xxx)方法.在Chrome和FF中是可以使用的.然而在IE中却报错,提示不存在这个function. Solution: 经查Jav ...
- java常用开发工具类之 图片水印,文字水印,缩放,补白工具类
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- 字符串_KMP算法(求next[]模板 hdu 1711)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 问题描述:给两个序列a,b,长度分别为n,m(1<=n<=1000000,1< ...
- poj 3177 Redundant Paths
题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...
- top命令使用详解
1.top命令查看服务器负载情况,CPU使用率,进程数等信息. 0.0% wa IO等待占用CPU的百分比 86.9% id 表示空闲的CPU占比 load average数据是每隔5秒钟检查一次活 ...
- [转]Ionic 实现双击返回键退出功能
本文转自:http://ionichina.com/topic/5514b539b6421f9166aa5f88 一.准备 Toast插件 插件地址:cordova plugin add https: ...
- Geoserver发布WMS服务出错分析
使用Geoserver发布一个空间表,在图层预览的过程中没有任何问题,但是当我根据这个空间表传创建视图再发布wms服务,访问时就会报错,错误信息如下: Caused by: java.sql.SQLE ...