【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 ...
随机推荐
- set QUOTED_IDENTIFIER ON事故记录
作业执行失败: 看了一下执行脚本 delete top(8000) from "interface"."完成" where loggid in( selec ...
- angularjs发送delete请求传参数的方法
angularjs使用$http.delete()发送请求,默认是没法通过变量来传参数
- Jmeter之逻辑控制器(Logic Controller)
Jmeter之逻辑控制器(Logic Controller) 前言: 1. Jmeter官网对逻辑控制器的解释是:“Logic Controllers determine the order in w ...
- make file
CPPUTEST_USE_EXTENSIONS = Y如果没有这一句定义,CppUTestExt/MockSupport.h和CppUTestExt/MockSupport_c.h文件中的定义就不能用 ...
- URL Quoting
[URL Quoting] The URL quoting functions focus on taking program data and making it safe for use as U ...
- PHP数组的知识
- C语言学习笔记(一)_hello world
一.建立一个文件a.c,写入: #include <stdio.h> //使用printf库函数之前,必须include <stdio.h>int main()//main函数 ...
- PTA List Components
For a given undirected graph with N vertices and E edges, please list all the connected components b ...
- weblogic myeclipse小知识
新建域 http://jingyan.baidu.com/article/f7ff0bfc72904e2e27bb136f.html svn 上down下来一些新项目的时候没法添加到weblogic ...
- python学习笔记-Day6(2)
xml处理模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融 ...