[leetcode] 131. Palindrome Partitioning 解题报告
来源:互联网 发布:模板支撑计算软件 编辑:程序博客网 时间:2024/05/18 18:17
题目链接: https://leetcode.com/problems/palindrome-partitioning/
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]
思路:DFS递归搜索每个可能的子串,即每个子串长度可能为[1, length], 取一个长度为k的子串,判断其是否为回文数,分两种情况讨论:
1. 如果当前子串是回文数,将当前子串保存到数组里,然后剩下的字符作为原始字符串继续递归重复此操作,直到字符串剩余长度为0,将当前的一个解决方案保存起来
2. 如果不是回文数,k长度加1继续进行判断
代码如下:
class Solution {public: void DFS(string s, int k, vector<string> vec) { if(k == s.size()) return ans.push_back(vec); for(int i = 1, j; i <= s.size()-k; i++) { string sub = s.substr(k, i); for(j =0; j < i/2; j++) if(sub[j]!=sub[i-1-j]) break; if(j < i/2) continue; vec.push_back(sub); DFS(s, k+i, vec); vec.pop_back(); } } vector<vector<string>> partition(string s) { if(s.size()==0) return {}; DFS(s, 0, vector<string>{}); return ans; }private: vector<vector<string>> ans;};
0 0
- [leetcode] 131. Palindrome Partitioning 解题报告
- [Leetcode] 131. Palindrome Partitioning 解题报告
- [LeetCode]Palindrome Partitioning,解题报告
- 【LeetCode】Palindrome Partitioning 解题报告
- 【LeetCode】Palindrome Partitioning II 解题报告
- [leetcode] 132. Palindrome Partitioning II 解题报告
- [Leetcode] 132. Palindrome Partitioning II 解题报告
- Palindrome Partitioning解题报告
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetcode解题笔记-Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- VS2010 + QT5.2+ QT-VS-Addin1.2.2开发环境配置
- 关于Scanner的位置问题
- 我的目标是睁十个小时的眼干九个小时的活,期望出十一个小时的效果,多挣会眼享受慢节奏。如果干十二个小时,一定要期望它要让它出十五个小时的效果,决不能出十个小时的效果,那就有点白费时间啦
- Codeforces 602B
- Java中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具
- [leetcode] 131. Palindrome Partitioning 解题报告
- 计步器算法简述和模块使用
- 上传Apk至测试服务器
- 框架和类库的区别
- 发送手机物理标识请求
- activity生命周期完整版
- 泛型的内部原理:类型擦除以及类型擦除带来的问题
- 欢迎使用CSDN-markdown编辑器
- Java菜鸟入门(20) Producer Consumer经典代码