leetcode: Palindrome Partitioning
来源:互联网 发布:淘宝追加评论期限 编辑:程序博客网 时间:2024/05/16 06:29
用动态规划可解。
二维数组dp[i][j]表示i到j是回文
状态转换方程 dp[j+1][i-1] && s[j] == s[i]
class Solution {public: vector<vector<string>> partition(string s) { if( s == "") return vector< vector< string> >(); int size = s.size(); vector< vector< bool> > dp( size, vector< bool>( size, false)); for( int i = 0; i < size; ++i) dp[i][i] = true; for( int i = 0; i < size; ++i){ for( int j = 0; j < i; ++j){ if( s[j] == s[i] && dp[j+1][i-1]){ dp[j][i] = true; } if( s[j] == s[i] && j == i - 1){ dp[j][i] = true; } } } vector< vector< string> > res; vector< string> t; allPalindrome( res, s, dp, t, 0); return res; } void allPalindrome( vector<vector< string> > &res, string s, vector< vector< bool> > &dp, vector< string> &t, int col){ if( col == s.size()){ res.push_back(t); return; } for( int j = col; j < s.size(); ++j){//这里行坐标是固定的,因为如果这行没有true表示当前字母到后面所有字母没有回文 if(dp[col][j]){ vector< string> tmp = t; tmp.push_back( s.substr(col, j-col+1)); allPalindrome( res, s, dp, tmp, j + 1); } } }};
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- Smali代码注入
- 图书数据库脚本
- CCDrawNode(是一个节点有drawDot drawSegment drawPolygon方法 可以类绘制点 线段 多边形--与drawing primitives不同 采用批渲染)
- WM_PAINT 与 WM_ERASEBKGND
- python 实现spectral clustering
- leetcode: Palindrome Partitioning
- 安卓多平台批量打包
- 第一篇
- Eclipse中DTD验证导致无法进行代码提示的问题 .
- CCDrawingPrimitives(使用gl原始方法 绘制Points Line Rect Poly Circle Bezier CatmullRom CardinalSpline。。。。)
- 【文件管理】文件系统概述
- Angela王诗龄获邀于纽约公共图书馆
- 用Python库picamera控制树莓派摄像头模块
- java socket参数详解:OOBInline和UrgentData