LeetCode之Palindrome Partitioning
来源:互联网 发布:形容网络不好的说说 编辑:程序博客网 时间:2024/05/13 18:01
/*采用深度搜索算法。写法一。*/class Solution {public:vector<vector<string>> partition(string s) {vector<vector<string> > res;if(s.empty()) return res;vector<string> path;partition(s, res, path, -1, 0);return res;}void partition(const string &s, vector<vector<string> > &res, vector<string> &path, int pre, int cur){if(cur == s.size()-1){if(is_palindrome(s, pre+1, cur)){path.push_back(s.substr(pre+1, cur-pre));res.push_back(path);path.pop_back();}return;}else{if(is_palindrome(s, pre+1, cur)){path.push_back(s.substr(pre+1, cur-pre));partition(s, res, path, cur, cur+1);path.pop_back();}partition(s, res, path, pre, cur+1);}}bool is_palindrome(const string &s, int left, int right){if(left >= right) return true;while(left < right){if(s[left] != s[right]) return false;++left;--right;}return true;}};/*写法二。*/class Solution {public:vector<vector<string>> partition(string s) {vector<vector<string> > res;if(s.empty()) return res;vector<string> path;partition(s, res, path, 0);return res;}void partition(const string &s, vector<vector<string> > &res, vector<string> &path, int cur){if(cur == s.size()){res.push_back(path);return;}else{for(int i = cur; i < s.size(); ++i){if(is_palindrome(s, cur, i)){path.push_back(s.substr(cur, i-cur+1));partition(s, res, path, i+1);path.pop_back();}}}}bool is_palindrome(const string &s, int left, int right){if(left >= right) return true;while(left < right){if(s[left] != s[right]) return false;++left;--right;}return true;}};
0 0
- LeetCode之Palindrome Partitioning
- leetcode之Palindrome Partitioning
- leetcode之Palindrome Partitioning
- leetcode之Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- leetcode之 Palindrome Partitioning
- LeetCode之Palindrome Partitioning II
- leetcode之Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II
- leetcode之 Palindrome Partitioning I&II
- leetcode之 Palindrome Partitioning I&II
- leetcode之 Palindrome Partitioning I&II
- 【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
- android 自定义 ProgressBar
- <网络编程培训之一> 编程实现IP/TCP/UDP报文
- "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")"的分析
- Hadoop Linux下txt文件乱码
- 控件的使用
- LeetCode之Palindrome Partitioning
- Kth Smallest Element in a BST
- [LeetCode]171 Excel Sheet Column Number
- HTML样式
- OpenStack在计算节点安装Docker遇到的问题
- ARC小解(一)
- LoaderManager杂记
- MySQL——存储过程
- easyUI学习记录