132. Palindrome Partitioning II (String; DP)
2015-10-23 21:01
344 查看
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 =
Return
思路: 除了用flag[i][j]记录从i到j是否是Palindrome,还要用cut[i]存储到i位置位置最少的cut数
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
"aab",
Return
1since the palindrome partitioning
["aa","b"]could be produced using 1 cut.
思路: 除了用flag[i][j]记录从i到j是否是Palindrome,还要用cut[i]存储到i位置位置最少的cut数
class Solution { public: int minCut(string s) { int sLength = s.length(); vector<vector<bool>> flag(sLength,vector<bool>(sLength, false)); int cut[sLength]; cut[0]=0; for (int i=1;i<sLength;i++) { cut[i]=1+cut[i-1]; for(int j=0;j<i;j++) if(s[i]==s[j]&&(i-j<=2||flag[j+1][i-1])) { flag[j][i]=true; if(j==0) { cut[i] = 0; break; } else cut[i] = min(cut[i],1+cut[j-1]); } } return cut[sLength-1]; } };
相关文章推荐
- UITableView
- Android源码阅读笔记二 消息处理机制
- CGContextRef CGMutablePathRef UIBezierPath
- 2015.10.23 单片机课程设计总结
- module/config/module.config.php文件内涵定义
- RSA笔记
- 自我介绍
- 阶段一功能测试技术点
- C#调用Python语言
- javascript基础之八(原型揭秘)
- 网易cc题3
- xls信息筛选
- Jsp标准标签库JSTL
- 常用打包和压缩格式文件处理
- Android 输入法键盘和activity页面遮挡问题解决
- Windows8.1下PHP环境配置(PHP5.6、Apache2.4、MySql5.6)
- [线段树双lazy]UVa11992 - Fast Matrix Operations
- 和菜鸟一起学android4.0.3源码之硬件gps简单移植
- linux下各种形式的shell加法操作总结
- ThreadLocal