【LeetCode】131. Palindrome Partitioning
来源:互联网 发布:软件框架设计工具 编辑:程序博客网 时间:2024/05/21 17:37
131. Palindrome Partitioning
- Total Accepted: 84076
- Total Submissions: 272482
- Difficulty: Medium
- Contributors: Admin
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"]]
Subscribe to see which companies asked this question
分析
在每一步都可以判断中间结果是否为合法结果,用回溯法
一个长度为n的字符串,有n-1个地方可以砍断,每个地方可断可不断,因此时间复杂度为O(2^n)
class Solution {public:vector<vector<string>> partition(string s) {vector<vector<string>> result;vector<string> path; // ???? partition ????dfs(s, path, result, 0, 1);return result;}void dfs(string &s, vector<string>& path,vector<vector<string>> &result, size_t prev, size_t start) {if (start == s.size()) { if (isPalindrome(s, prev, start - 1)) { path.push_back(s.substr(prev, start - prev));result.push_back(path);path.pop_back();}return;}dfs(s, path, result, prev, start + 1);if (isPalindrome(s, prev, start - 1)) {path.push_back(s.substr(prev, start - prev));dfs(s, path, result, start, start + 1);path.pop_back();}}bool isPalindrome(const string &s, int start, int end) {while (start < end && s[start] == s[end]) {++start;--end;}return start >= end;}};
0 0
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- Leetcode 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode *** 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode-131.Palindrome Partitioning
- 【leetcode】131. Palindrome Partitioning
- [leetcode] 131. Palindrome Partitioning
- 【Leetcode】131. Palindrome Partitioning
- [LeetCode] 131. Palindrome Partitioning
- Leetcode-131. Palindrome Partitioning
- 【LeetCode】131. Palindrome Partitioning
- Leetcode 131. Palindrome Partitioning
- Undraw the Trees 看图写树 UVA 10562 (DFS)
- 进制之和 2106
- redis linux 开机自启
- 初识RecyclerView
- kali-linux网络渗透
- 【LeetCode】131. Palindrome Partitioning
- 使用TraceView分析CPU占用-解决ANR笔记
- 欢迎使用CSDN-markdown编辑器
- 线索二叉树(下)
- 手机无须ROOT不用修改hosts即可在本地测试安卓、苹果APP和H5应用
- Eclipse启动Tomcat端口被占用解决方法
- Spring Extensible XML
- 【数据结构与算法】最大子序列
- 四大组件之Activity(活动)小结