132. Palindrome Partitioning II (String; DP)
2024-10-21 04:02:12
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.
思路: 除了用dp[i][j]记录从i到j是否是Palindrome,还要用cut[i]存储到i位置位置最少的cut数
class Solution {
public:
int minCut(string s) {
int len = s.length();
vector<vector<bool>> dp(len, vector<bool>(len, false));
vector<int> cut(len,);
for(int i = ; i < len; i++) dp[i][i]=true;
for(int i = ; i < len; i++){
cut[i]=cut[i-]+;
for(int j = ; j < i; j++){ //traverse the length
if(s[i]==s[j]){
if(j==i-) dp[j][i] = true;
else dp[j][i]=dp[j+][i-];
if(dp[j][i]){
if(j==) cut[i]=;
else cut[i]=min(cut[j-]+, cut[i]);
}
}
}
}
return cut[len-];
}
};
最新文章
- [转] eclipse SVN中文件修改后图标不变黑星解决
- No zuo no die:DDD 应对具体业务场景,Domain Model 重新设计
- ora-02292
- 【11】在operator=中处理“自我赋值”
- QSplashScreen开机画面(不断的repaint)
- jQuery.data的是jQuery的数据缓存系统
- C语言格式化输出,空位补0,空位补空格
- Web版记账本开发记录(二)开发过程遇到的问题小结1 对数据库的区间查询
- [转]How To Send Transactional Email In A NodeJS App Using The Mailgun API
- 【impala学习之一】impala
- 微信小程序实现滚动分页加载更多
- spring boot对输入的字符串进行html转码
- 字符串使用replaceAll()方法报异常
- JAVA类加载器概念与线程类加载器
- Xcode 8 插件安装
- 8086处理器的无条件转移指令——《x86汇编语言:从实模式到保护模式》读书笔记13
- LeetCode 141——环形链表
- AngularJs学习笔记(2)——ng-include
- django1.7 HTML模板中{%url%}的使用
- 才知道 Windows Live Writer Source Code plugin for SyntaxHighlighter 更新到2.0了
热门文章
- 1111 Online Map (30 分)
- Vue引用其他组件,但组件某些部分不需要时的简单处理
- Open Live writer 远程博客管理客户端
- oletools下载安装及rtfobj使用
- [datatable]关于在DataTable中执行DataTable.Select(";条件";)返回DataTable的解决方法
- 自己写的一个jQuery分页插件
- 第3章 文件I/O(3)_内核数据结构、原子操作
- storm的流分组
- 基于Linux的Samba开源共享解决方案测试(一)
- [Python] 分段线性插值