132.leecode-Palindrome Partitioning II
这个题需要两个dp,一个保存从i到j是否为回文串
另一个保存0到i的最小的分割
下面是我的效率不太高的代码
class Solution {
public:
int minCut(string s) {
vector<vector<bool> > dp(s.size(), vector<bool>(s.size(), false));
vector<int> res(s.size(), );
//辅助数组
for(int i = ; i < s.size(); i++)
{
for(int j = i, k = ; j < s.size(); j++, k++)
{
if(j-k <= )
dp[k][j] = s[k] == s[j] ? true : false;
else
dp[k][j] = (s[k] == s[j]) && (dp[k+][j-]);
}
}
//如果0-i是回文串,那么不用切res[i]为0
//如果j-i是回文串,那么res[i]为res[j-1]+1
res[] = ;
for(int i = ; i < s.size(); i++)
{
res[i] = i;
for(int j = ; j <= i; j++)
{
if(dp[j][i])
res[i] = j == ? : min(res[i], res[j-] + );
}
}
return res[s.size()-];
}
};
132.leecode-Palindrome Partitioning II的更多相关文章
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- 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 ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【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 ...
- 动态规划——Palindrome Partitioning II
Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...
随机推荐
- Windows防火墙开启ping,禁ping的配置
当我通过本机Ping另一台在同一局域网内(即在同一网段)的计算机时,发现,如果防火墙开启的话,无论如何也ping不通.一旦关闭防火墙就可以ping通了.这是为什么呢?究竟该怎么设置呢? 原因是这样的, ...
- Error merging: refusing to merge unrelated histories
解决方案: git pull git pull origin master git pull origin master --allow-unrelated-histories idea提交git提交 ...
- MoneyRunner API汇总
MonkeyRunner API 汇总 MonkeyRunner工具主要有三个类: MonkeyRunner MonkeyDevice MonkeyImage 1.MonkeyRunner类: Mon ...
- nohup python 没有print输出
nohup python -u crake.py >run.log 2>&1 &
- 手把手教学在Springboot中搭建使用Guava cache,包教包会,不会我输一包辣条给你
guava cache使用简介 概述 缓存是日常开发中经常应用到的一种技术手段,合理的利用缓存可以极大的改善应用程序的性能. Guava官方对Cache的描述连接 缓存在各种各样的用例中非常有用.例 ...
- K3精益版给物料添加属性,并在BOM中新增字段引用该属性
1.给物料新增属性 打开“系统--基础资料--公共资料--核算项目管理”,然后双击物料,弹出核算项目类别-修改对话框.再点新增按钮: 输入你想新增字段的类型,长度,想要放置的位置. 相关属性里面选的是 ...
- python requests库爬取网页小实例:ip地址查询
ip地址查询的全代码: 智力使用ip183网站进行ip地址归属地的查询,我们在查询的过程是通过构造url进行查询的,将要查询的ip地址以参数的形式添加在ip183url后面即可. #ip地址查询的全代 ...
- linux-kernel-4.4 移植 (3) 网卡移植
开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ...
- Jenkins+Gradle+Sonar进行Java项目代码分析
Jenkins+Maven+Sonar与Jenkins+Gradle+Sonar配置方法很相似,区别就是Java项目所用的编译工具不同,一个是maven,一个是gradle 使用maven编译工具的可 ...
- Jython 在 Eclipse 控制台报错 console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
在 Eclipse 中使用 Jython 时报错 解决办法 右键 --> Run As --> Run Configurations --> Arguments --> 设置 ...