leetcode-palindrome partitioning-ZZ
http://yucoding.blogspot.com/2013/08/leetcode-question-132-palindrome.html
Analysis:
When face the "return all", "get all ", "find all possible", "find the total number of", an idea is to use the recursion. Same as this problem!
To get the all the partitions of a string s:
1. find all the palindromes in substring s[0], and all the palindromes in substring s[1:end]
2. find all the palindromes in substring s[0:1], and all the palindromes in substring s[2:end]
...
find all the palindromes in substring s[1:end-1], and all the palindromes in substring s[end]
So the problem is quite clear, when we do recursion, two things should be considered:
1. stop condition: when the search goes to the last position in the string
2. for loop or while loop: for position=current start position to the end.
This problem is not complex, see the code below and you will understand the idea:
Code:
class Solution {
public: bool valid(string &str, int st, int ed){
while (st<ed){
if (str[ed]!=str[st]){
return false;
}else{
st++;
ed--;
}
}
return true;
} void find(string s, int st, vector<string> &r, vector<vector<string> > &res){
if (st>=s.size()){
res.push_back(r);
}else{
for (int i=st;i<s.size();i++){
if (valid(s,st,i)){
r.push_back(s.substr(st,i-st+));
find(s,i+,r,res);
r.pop_back();
} }
}
} vector<vector<string>> partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<string> > res;
vector<string> r;
find(s,,r,res);
return res;
}
};
======================================================
http://fisherlei.blogspot.com/2013/03/leetcode-palindrome-partitioning.html
[Thoughts]
这种需要输出所有结果的基本上都是DFS的解法。实现如下。
[Code]
1: vector<vector<string>> partition(string s) {
2: vector<vector<string>> result;
3: vector<string> output;
4: DFS(s, 0, output, result);
5: return result;
6: }
7: void DFS(string &s, int start, vector<string>& output, vector<vector<string>> &result)
8: {
9: if(start == s.size())
10: {
11: result.push_back(output);
12: return;
13: }
14: for(int i = start; i< s.size(); i++)
15: {
16: if(isPalindrome(s, start, i))
17: {
18: output.push_back(s.substr(start, i-start+1));
19: DFS(s, i+1, output, result);
20: output.pop_back();
21: }
22: }
23: }
24: bool isPalindrome(string &s, int start, int end)
25: {
26: while(start< end)
27: {
28: if(s[start] != s[end])
29: return false;
30: start++; end--;
31: }
32: return true;
33: }
leetcode-palindrome partitioning-ZZ的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome Partitioning [131]
[称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
随机推荐
- request.upload.addEventListener in not a function。
本人在使用vue开发一套后台系统时,碰到了一个上传文件的需求,因为平时做的上传是使用ajax的方式来进行上传. 现在是使用axios来进行上传,方式没有什么改变: npm i axios 直接上 ...
- IO概念解析------同步异步阻塞非阻塞
各个IO Model的比较如图所示: 阻塞和非阻塞强调的是程序在等待调用结果(消息,返回值)时的状态. 阻塞调用是指调用结果返回之前,当前线程会被挂起.调用线程只有在得到结果之后才会返回.非阻塞调用 ...
- python3.5+ asyncio await异步详解
import asyncio,time from collections import defaultdict from pprint import pprint collect=defaultdic ...
- Java学习之路(二):关键字和变量,运算符
关于关键字的一个概述 Java的关键字对Java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构,关键字不能用做变量名.方法名.类名.包名. Java常见的关键字 标识符 什么是标识 ...
- MariaDB10.2修改默认密码
1.修改 my.ini,在 [mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证 2.重启 mysqld 服务 3.使用 roo ...
- HDU 5700——区间交——————【线段树+枚举】
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Agile software Development
转自:https://www.cnblogs.com/kkun/archive/2011/07/06/agile_software_development.html 敏捷软件开发 Agile soft ...
- js 大转盘,老虎 机
http://www.helloweba.com/view-blog-215.htmlhttp://www.ui3g.com/demos/show/1408/http://www.js-css.cn ...
- 【读书笔记】读《编写可维护的JavaScript》 - 编程风格(第一部分)
之前大致翻了一遍这本书,整体感觉很不错,还是不可追求快速,需要细细理解. 这篇随笔主要对本书的第一部分中对自己触动比较大的部分及与平常组织代码最为息息相关的部分做一个记录,加深印象. 主要讲述五点内容 ...
- (Forward)5 Public Speaking Tips That'll Prepare You for Any Interview
Landing a job interview is incredibly exciting –- and often terrifying. But fear not. There are clev ...