leetcode — palindrome-partitioning-ii
import java.util.Arrays;
/**
*
* Source : https://oj.leetcode.com/problems/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.
*/
public class PalindromePartition2 {
/**
* 将字符串分割为多个回文字符串的最小分割次数
*
* 定义数组cut[len+1],cut[i]表示从0-的最小分割数,初始化cut[0] = -1,
* 当i-j是回文字符串的时候,cut[i] = min(cut[i], cut[j]+1)
*
* 最后得出的cut[len+1]就是0-(len+1)之间的最小分割数
*
* @param str
* @return
*/
public int minPartition (String str) {
if (str.length() <= 1) {
return 0;
}
int[] cut = new int[str.length()+1] ;
Arrays.fill(cut, Integer.MAX_VALUE);
boolean[][] table = new boolean[str.length()][str.length()];
cut[0] = -1;
for (int i = str.length()-1; i >= 0; i--) {
for (int j = i; j < str.length(); j++) {
if ((i+1 > j - 1 || table[i+1][j-1]) && str.charAt(i) == str.charAt(j)) {
table[i][j] = true;
}
}
}
for (int i = 1; i <= str.length(); i++) {
for (int j = i-1; j > -1; j--) {
if (table[j][i-1]) {
cut[i] = Math.min(cut[i], cut[j] + 1);
}
}
}
return cut[str.length()];
}
public static void main(String[] args) {
PalindromePartition2 palindromePartition2 = new PalindromePartition2();
System.out.println(palindromePartition2.minPartition("aab") + "==1");
}
}
leetcode — palindrome-partitioning-ii的更多相关文章
- [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 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]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- 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】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
随机推荐
- vscode设置中文语言
https://jingyan.baidu.com/article/7e44095377c9d12fc1e2ef5b.html
- 微信跳转技术,浏览器唤起微信,weixin://dl/business/?ticket=
weixin://dl/business/?ticket= 到底怎么生成的?调用以下接口 weixin://dl/scan 扫一扫weixin://dl/feedback 反馈weixin://dl ...
- Docker安装及基本操作
系统环境 CentOS Linux release 7.5.1804 (Core) 安装依赖包 更新系统软件 yum update 安装docker yum install docker 启动dock ...
- CSS3 神器总结
1. 选择类 1.1 /* 鼠标选中区域,改变背景/字体颜色 */ /*遍历写法*/ div::selection { background-color: red; color: #fff; /* f ...
- vue项目开发基本目录结构
§ 目录结构 . ├── build/ # Webpack 配置目录 ├── dist/ # build 生成的生产环境下的项目 ├── src/ # 源码目录(开发都在这里进行) │ ├── ass ...
- diy 滚动条 样式 ---- 核心代码
参考自 : https://blog.csdn.net/qq_38881495/article/details/83689721 .chapter_data position relative wid ...
- 929. Unique Email Addresses
929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...
- 口袋appnabcd
N(need)需求:依据我们学习经历的情况而言,对于初次接触的专业的学生来说,对学习的方向上会感到迷茫,不知道如何学习以及不知道学什么.比如对于计算机专业来说,对于一些软件的选择和下载,应用环境配置等 ...
- 接口自动化集成到jenkins(Java+testng+maven+git)
一jenkins启动命令:jenkins 查看端口号: 1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 二: 登录:http://localhost:8080 输入:u ...
- 记录 制作校园网登陆脚本 python编写 附源码
‘’‘ 首先我们分析一下 1.需要本机的IP 使用 socket 获取 2.需要向服务器提交的数据 构造请求数据 并分析数据可替换 3.检测登陆成功 检测登陆是否成功 ’‘’ 获取IP 这样会返回 本 ...