【Leetcode】【Medium】Palindrome Partitioning
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"]
]
解题:
使用深入优先搜索的思想;使用递归;
从头i = 0开始遍历字符串,找到满足要求的回文列,将其放入结果列表中,继续查找下面的回文字符;
解题步骤:
1、主函数,建立返回的二维数组,建立存放结果的一维数组,调用递归函数;
2、递归函数,输入为二维数组ret,一维数组path,字符串str,当前检查到的index值:
(1)如果index值已经等于str的长度,说明搜索完毕,将path插入ret中,返回;
(2)否则从i从index开始,遍历到str末尾,找index~i范围哪些是回文串:
a. 将回文串摘出放入path中,更新index,继续递归;
b. 从path中pop出放入的回文串,继续遍历循环;
3、返回ret
代码:
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string> > ret;
if(s.empty())
return ret;
vector<string> path;
dfs(ret, path, s, );
return ret;
}
void dfs(vector<vector<string> >& ret, vector<string>& path, string& s, int index) {
if(index == s.size()) {
ret.push_back(path);
return;
}
for(int i = index; i < s.size(); ++i) {
// find all palindromes begin with index
if(isPalindrome(s, index, i)) {
path.push_back(s.substr(index, i - index + ));
dfs(ret, path, s, i+);
path.pop_back();
}
}
}
bool isPalindrome(const string& s, int start, int end) {
while(start <= end) {
if(s[start++] != s[end--])
return false;
}
return true;
}
};
【Leetcode】【Medium】Palindrome Partitioning的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, 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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode每天一题】Palindrome Number( 回文数字)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【leetcode刷题笔记】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
随机推荐
- zend 快捷键
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当前 ...
- Java和C++中的static
1.Java类中的static变量和static方法会在类装载的过程中就得到内存分配,然后就会进行初始化工作.最多可能会被初始化3次,静态代码块的执行在main方法之前. static变量不可以在构造 ...
- 命令行启动win7系统操作部分功能
control.exe /name microsoft.folderoptions 启动资源管理器的 文件夹属性 选项卡 control.exe /name Microsoft.AddHardware ...
- Unix网络编程 -- ubuntu下搭建编译环境( 解决unp.h 编译等问题)
1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下载unpv13e http://ishare.i ...
- 给Xcode配置VVDocumenter-Xcode-master,注释插件
1. 去github上下载 https://github.com/onevcat/VVDocumenter-Xcode . 2. 打开工程,command+B 编译成功 ...
- 使用signal、setjmp、longjmp进行Linux/Android C异常处理
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <setjmp.h&g ...
- 学习Spring必学的Java基础知识
[1] Java反射知识-->Spring IoC :http://www.iteye.com/topic/1123081 [2] Java动态代理-->Spring AOP :http: ...
- shell脚本学习--shell中的变量$
$$ :Shell本身的PID(ProcessID) $! :Shell最后运行的后台Process的PID $? :最后运行的命令的结束代码(返回值) $- :使用Set命令设定的Flag一览 $* ...
- 关于内存数据与 JSON
闲话: 正在用 WebBroker 写一个小网站,感受颇多: 1.如果是写一个小东西,应该先考虑下 WebBroker,因为它是分分钟即可上手的. 2.如果要写一个大东西,或许也应该考虑下 WebBr ...
- .net mvc 微信支付
一.微信第三方登录 通过微信打开链接:http://www.hzm.com/Entry/Login 微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server ...