Palindrome Partitioning II Leetcode java
题目:
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.
题解:
这道题需要用动态规划做,如果用I的DFS的方法做会TLE。
首先设置dp变量 cuts[len+1]。cuts[i]表示从第i位置到第len位置(包含,即[i, len])的切割数(第len位置为空)。
初始时,是len-i。比如给的例子aab,cuts[0]=3,就是最坏情况每一个字符都得切割:a|a|b|' '。cuts[1] = 2, 即从i=1位置开始,a|b|' '。
cuts[2] = 1 b|' '。cuts[3]=0,即第len位置,为空字符,不需要切割。
上面的这个cuts数组是用来帮助算最小cuts的。
还需要一个dp二维数组matrixs[i][j]表示字符串[i,j]从第i个位置(包含)到第j个位置(包含) 是否是回文。
如何判断字符串[i,j]是不是回文?
1. matrixs[i+1][j-1]是回文且 s.charAt(i) == s.charAt(j)。
2. i==j(i,j是用一个字符)
3. j=i+1(i,j相邻)且s.charAt(i) == s.charAt(j)
当字符串[i,j]是回文后,说明从第i个位置到字符串第len位置的最小cut数可以被更新了,
那么就是从j+1位置开始到第len位置的最小cut数加上[i,j] cuts[i] = len - i;
|| (s.charAt(i) == s.charAt(j) && matrix[i+1][j-1]))
{
matrix[i][j] = cuts[i] = Math.min(cuts[i], cuts[j+1]+1);
}
}
}
min = cuts[0]-1;
}
Reference:http://blog.csdn.net/ljphhj/article/details/22573983
Palindrome Partitioning II Leetcode java的更多相关文章
- Palindrome Partitioning II Leetcode
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 解题笔记
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
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 ...
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
随机推荐
- java常见异常集锦
1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...
- python 的编码问题
老是碰到这个问题,决定好好给整理一番思路. 翻阅资料和实践证明,以下论述为真理: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将 ...
- php中global与$GLOBALS的用法及区别-转载
php中global 与 $GLOBALS[""] 差别 原本觉得global和$GLOBALS除了写法不一样觉得,其他都一样,可是在实际利用中发现2者的差别还是很大的! 先看下面 ...
- Android mtk单路录音问题
在单路录音中,有两种情况导致底层录音资源被占用的问题: 1 开启vmLog后,拨打一个电话,挂断电话.如果挂断电话后,没有关闭vmlog进程,则会导致其它AP 无法得到底层的录音资源,从而无法录音. ...
- 烦烦烦SharePoint2013 以其他用户登录和修改AD域用户密码
sharepoint默认是没有修改AD密码 和切换 用户的功能,这里我用future的方式来实现. 部署wsp前: 部署后 点击以其他用户身份登录 点击修改用户密码: 这里的扩展才菜单我们用Custo ...
- poj 1328
http://poj.org/problem?id=1328 题意:题目大概意思就是有一群孤岛,想要用雷达来监视这些岛屿,但雷达的范围是有限的,所以需要多个雷达,题目就是要你解决最少需要几个雷达,注意 ...
- 使用kettle转换中的JavaScript对密码进行加密和解密
日常开发中,为了确保账号和密码的安全,时常要对密码进行加密和解密.然而kettle是怎么对密码进行加密和解密的呢? 下面的代码需要再转换中的JavaScript中运行. var encrypted_p ...
- 项目总结(四)--- 网络封包分析工具Charles
Charles是Mac下一款截取网络封包的工具,主要原理就是将自己设置成为熊网络访问的代理服务器,这样的话,所有的网络请求都得通过它来完成,从而实现网络封包的拦截分析. 这款软件功能整体来说还是非常强 ...
- std::map常用方法
map<string, int> Employees; Employees["Mike C."] = 12306; Employees.insert(make_pair ...
- solr6.0学习
solr6.0学习(一)环境搭建准备工作:目前最新版本6.0.下载solr 6.0:Solr6.0下载JDK8 下载jdk1.8:jdk1.8[solr6.0是基于jdk8开发的]tomcat8.0 ...