「回溯法」Palindrome Partitioning
来源:互联网 发布:全球鹰与翔龙数据对比 编辑:程序博客网 时间:2024/06/07 19:20
/*************************************************************************> File Name: PalindromePartitioning.cpp> Author: Shaojie Kang> Mail: kangshaojie@ict.ac.cn > Created Time: 2015年09月16日 星期三 16时46分38秒 > Problem: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Have you met this question in a real interview? Yes Example given s = "aab", Return [ ["aa","b"], ["a","a","b"] ] ************************************************************************/#include<iostream>#include<vector>#include<string>using namespace std;class Solution {public: bool isPalindrome(const string &str) { if(str.empty()) return true; int size = str.size(); for(int i = 0, j = size-1; i < j; ++i, --j) { if(str[i] != str[j]) return false; } return true; } vector<vector<string> > partition(string s) { vector<vector<string> >result; vector<string> oneResult; partitionCore(s, 0, result, oneResult); return result; } void partitionCore(string &s, int start, vector<vector<string> > &result, vector<string> oneResult) { int size = s.size(); if(start == size) { result.push_back(oneResult); return; } for(int i = start; i < size; ++i) { string strTemp(s.substr(start, i-start+1)); if(isPalindrome(strTemp)) { oneResult.push_back(strTemp); partitionCore(s, i+1, result, oneResult); oneResult.pop_back(); } } }};void print(const vector<vector<string> > &result){ for(int i = 0; i < result.size(); ++i) { for(int j = 0; j < result[i].size(); ++j) { cout<<result[i][j]<<" "; } cout<<endl; }}int main(){ string str("aab"); Solution sol; vector<vector<string> > result = sol.partition(str); print(result); return 0;}
0 0
- 「回溯法」Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning--回溯法
- 131. Palindrome Partitioning回溯算法
- [leetcode][回溯][DP] Palindrome Partitioning //TODO
- leetcode 131. Palindrome Partitioning-回溯算法
- leetcode---Palindrome Partitioning---动规、回溯
- 131. Palindrome Partitioning(字符串,dfs回溯)
- 「动态规划」Palindrome Partitioning II
- [C++]LeetCode: 121 Palindrome Partitioning (分割回文子串 回溯法)
- LeetCode 131. Palindrome Partitioning 树形问题,状态回溯
- Palindrome partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Android快速开发常用工具类
- iOS 打包工具生成
- LeetCode|Pascal's Triangle II-java
- Java从包中读取所有的class文件
- 【LeetCode】Balanced Binary Tree 解题报告
- 「回溯法」Palindrome Partitioning
- 【Android应用开发】分享一个录制 Android 屏幕 gif 格式的小技巧
- BellmanFord判环算法
- ruby:$LOAD_PATH.unshift
- sizeof() 类大小,空类大小
- Xml解析
- MAC OS绕过开机密码
- solr HMMChineseTokenizer 中文解词
- 《程序员面试金典》有向路径检查