Leetcode---Palindrome Partitioning
来源:互联网 发布:阿里云主机80端口打开 编辑:程序博客网 时间:2024/06/08 06:04
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"] ]
Have you been asked this question in an interview?
这道题很值得推敲。首先给出先dp再dfs的方法,直接tls了,这里的dp比较粗暴,是计算字符串中所有字符是不是回文。估计这个地方消耗了很多时间,然后是dfs,dfs利用之前找到的dp数组,判断某段字符串是不是回文,然后形成了一颗dfs的树,在这些树里找问题的解。
这里总结下dfs:dfs其实是一种特殊的递归,只不过通常的递归是这样的:
fun(){
...
fun();
...
}
而dfs是这样的:
fun(){
...
for(int i=0;i<n;i++){
fun();
}
...
}
dfs每次运行到for的时候,它的解空间都分叉成n支,然后每一支又分叉成n支,这就形成了解空间的树。而dfs是深度优先访问这棵树的,这也是它的名字的由来。
通过dfs,我们考察了解的所有可能性,下面是code:
- void dfs(string s, vector<string> &path, vector<vector<string>> &res)
- {
- if(s.size() < 1)
- {
- res.push_back(path);
- return;
- }
- for(int i = 0; i < s.size(); i++)
- {
- int begin = 0;
- int end = i;
- while(begin < end)
- {
- if(s[begin] == s[end])
- {
- begin++;
- end--;
- }
- else
- break;
- }
- if(begin >= end)//bool isPalindrome = true;
- {
- path.push_back(s.substr(0,i+1));
- dfs(s.substr(i+1),path,res);
- path.pop_back();
- }
- }
- }
- vector<vector<string>> partition(string s) {
- vector<vector<string>> res;
- vector<string> path;
- dfs(s,path,res);
- return res;
- }
0
上一篇:Leetcode---Palindrome Number
下一篇:Leetcode---Sudoku Solver
相关热门文章
- test123
- 编写安全代码——小心有符号数...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- sql relay的c++接口
- 谁能够帮我解决LINUX 2.6 10...
- 现在的博客积分不会更新了吗?...
- shell怎么读取网页内容...
- ssh等待连接的超时问题...
- curl: (56) Recv failure: Con...
给主人留下些什么吧!~~
评论热议
0 0
- 【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
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- 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
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- Summarize 2014 Look Ahead 2015
- Leetcode---Unique Binary Search Trees II
- Leetcode---Valid Palindrome
- Xcode文档使用
- Leetcode---Palindrome Number
- Leetcode---Palindrome Partitioning
- Leetcode---Sudoku Solver
- Leetcode---Surrounded Regions
- 在腾讯实习的那段日子:不要在难受的时候选择 '逃避/离开'
- Leetcode---Word Break II
- Leetcode---Word Break
- Leetcode---word ladder
- Leetcode---Word Ladder II
- Leetcode---Restore IP Addresses
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
昌乐人才网
昌乐二街
爱昌乐
昌乐蓝宝石
昌乐二手房
中国昌乐
杨昌乐
昌乐教育网
山东昌乐
昌乐站
昌乐远古火山口
昌乐县城怎样找女性倍睡
昌乐民生网站
山东省昌乐二中
昌乐县第一中学
昌乐二手房出售新信息
昌乐二手房出售信息
昌乐孟凡勇与女人照片
昌乐县人民医院
昌乐人民医院
昌乐出租车电话
昌乐出租车叫车电话
昌乐成人用品
昌乐到潍坊的公交车时间表
昌乐信息港二手房
昌乐信息港人才招聘
昌乐人才信息港
昌乐信息港房产网
昌化鸡血石
昌化
昌化镇
浙江昌化
昌化镇旅游
昌化鸡血石图片
临安昌化旅游景点
临安昌化花店
临安昌化酒店
昌原
昌吉
昌吉学院
昌吉赣