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. iOS中倒计时

    方法一:使用NSTimer来实现(比较适用于发送短信验证码倒计时) 主要是利用NSTimer的scheduledTimerWithTimeInterval方法来每秒执行一次changeTime方法 / ...

  2. MySQL添加字段和删除字段

    MySQL添加字段应该如何实现呢?这是很多刚刚接触MySQL数据库的新人都提到过的问题,下面就为您介绍MySQL添加字段和删除字段的方法,希望对您能有所启迪. MySQL添加字段: alter tab ...

  3. 【DPDK】虚拟机开发环境配置

    DPDK介绍见:www.dpdk.org 本文介绍的步骤基本适用于dpdk 1.7.0 - dpdk 2.0.0 各版本.只是setup.sh显示的菜单有一些小的不同:同样的,也适用于ubuntu更高 ...

  4. poj 1144 Network 图的割顶判断模板

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8797   Accepted: 4116 Descripti ...

  5. Lua面向对象编程

    Lua中的table就是一种对象,看以下一段简单的代码: , b = } , b = } local tb3 = tb1 if tb1 == tb2 then print("tb1 == t ...

  6. 用命令测试安装好的OpenStack环境

    OpenStack三个节点icehouse-gre模式部署一文部署了一套OpenStack环境,接下来使用命令测试一遍. 首先要明确几个概念: 外网:可分配floating ip绑定到虚拟机,外部就可 ...

  7. Ajax与用户交互的存储格式JSON

    数据存储是JavaScript的核心功能,这是一个在学习前期的一个容易让人迷惑的问题.它并不是那种像页面滑动.幻灯片展示.淡入淡出等吸引人眼球的特效.适当的存放好数据,就有利于我们组织起结构,又能使应 ...

  8. Hadoop_YARN框架

    Hadoop学习笔记总结 01. YARN框架 1. 新一代的框架介绍 YARN的职能就是将资源调度和任务调度分开.资源管理器ResourceManager全局管理所有应用程序计算资源的分配,每一个j ...

  9. jmeter的使用(四)

    jmeter如何调用java程序呢,下面做简单介绍.1.打开eclipse,新建项目,导入jmeter依赖的包ApacheJMeter_core.jar和ApacheJMeter_java.jar,这 ...

  10. 【温故而知新-Javascript】窗口效果 (全屏显示窗口、定时关闭窗口)

    1.全屏显示窗口 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...