【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 ...
随机推荐
- maximo弹框设置新的功能测试总结
先介绍下弹框前的准备工作: 1.签名选项——定义系统中可授权的所有功能的唯一标识.定义签名选项是为了授权而已.定义的签名名要和相应的bean类中的方法一致. 2.签名选项中的功能实现,一般都在APPB ...
- Python自动化 【第二篇】:Python基础-列表、元组、字典
本节内容 模块初识 .pyc简介 数据类型初识 数据运算 列表.元组操作 字符串操作 字典操作 集合操作 字符编码与转码 一.模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库, ...
- Bitmap 的bitmapdata和texture区别
Texture:纹理类是对不同平台不同的图片资源的封装在HTML5中,资源是一个HTMLElement对象在OpenGL / WebGL中,资源是一个提交GPU后获取的纹理idTexture类封装了这 ...
- numpy.concatenate
import numpy as np a = np.array([[1, 2], [3, 4]]) a.shape Out[3]: (2, 2) b = np.array([[5, 6]]) b.sh ...
- ---iproute2 策略路由
http://linux.chinaunix.net/techdoc/net/2007/03/30/953750.shtml 这篇文章写的例子讲明白策略路由的使用,但是本身好像不用这么麻烦, 用系统默 ...
- 前端Html+Css——豆蔻年华(自学一个月)
详细见千万别碰我--燕十三 html .htm .shtml三者区别是什么 1..htm与.html没有本质上的区别,表示的是同一种文件,只是适用于不同的环境之下. 2.DOS仅能识别8+3的文件名, ...
- asp.net下载文件方法
/// <summary> /// 下载 /// </summary> /// <param name="url"></param> ...
- 调用百度地图API的应用混淆后出问题
1 混淆后出问题,程序异常退出 在proguard-project.txt中添加 -libraryjars libs/BaiduLBS_Android.jar -keep class com.baid ...
- VC MFC在CMFCToolBar工具栏中加入组合框
如何在CMFCToolBar工具栏中加入组合框等控件,且先看在线MSDN上怎么说的: 要增加一个组合框,需要完成以下步骤: 1.在工具栏资源中,增加一个对应ID资源号的按钮. 2.在主框架(mainf ...
- 实现MFC菜单画笔画圆,并且打钩
这个是用最简单的方法,移动客户区,圆会不见,所以下一篇我还要改进. 首先新建一个MFC单文件,在资源那里的菜单下,建立画笔,可以弹出红画笔,蓝画笔和绿画笔,,给出ID_RED,ID_BLUE,ID_G ...