leetcode第一刷_Palindrome Partitioning
来源:互联网 发布:js时间选择器插件 编辑:程序博客网 时间:2024/06/06 11:37
仍然是一个递归加记录路径的题目。有几个问题需要注意一下,一,需不需要打表呢?我认为这个问题是不需要打表的,因为每次的起点和长度都在变,即使重复到达某个点,他的起点也可能没有访问过,直接返回是没有道理的。二,提前算出字符串的各个回文字串的起点和终点怎么样?可以是可以,不过空间开销有点大,觉得不是很有必要。三,用引用保存中间结果有个问题,回退的时候要把中间结果也回退,回退几次多写写应该就可以掌握了。
贴ac代码如下:
bool isPalindorme(string &s, int start, int end){ if(start == end) return true; for(int i=start, j=end;i<j;i++, j--){ if(s[i] != s[j]) return false; } return true;}void getAllPartitions(string &s, int start, vector<string> &pres, vector<vector<string> > &res){ for(int i=0;i<s.length()-start;i++){ if(isPalindorme(s, start, start+i)){ string sb = s.substr(start, i+1); pres.push_back(sb); if(start+i+1 == s.length()){ res.push_back(pres); pres.pop_back(); return; } getAllPartitions(s, start+i+1, pres, res); pres.pop_back(); } }}class Solution {public: vector<vector<string> > partition(string s) { vector<vector<string> > res; vector<string> pres; if(s.length()<=0) return res; getAllPartitions(s, 0, pres, res); return res; }};
0 0
- leetcode第一刷_Palindrome Partitioning
- leetcode第一刷_Palindrome Number
- leetcode第一刷_ Palindrome Partitioning II
- LeetCode 9_Palindrome Number
- Leetcode第九题_Palindrome Number
- LeetCode之9_Palindrome Number
- [leetcode刷题系列]Palindrome Partitioning
- [leetcode刷题系列]Palindrome Partitioning II
- LeetCode刷题笔录Palindrome Partitioning
- 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
- 三维点云匹配算法ICP
- 文件读取和输出有关问题
- 图层旋转
- jquery.getScript以及onclick时间在ie下不生效
- 关于 ascii 码的问题(两者的区别)
- leetcode第一刷_Palindrome Partitioning
- 在深秋- 阿悄
- 编译Boost 详细步骤 适用 VC6 VS2003 VS2005 VS2008 VS2010
- 黑马程序员-Oracle(第八讲)
- Hbase分布式安装
- 文件上传需要注意的几个点
- hbase伪分布式安装详解及错误分析
- 视觉设计人员必备-网址篇(持续完善中)
- java基础:面向对象(三)