【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 ...
随机推荐
- mongodb账号安全操作
安装服务 mongod --install --serviceName mongodb --storageEngine=mmapv1 --dbpath i:\mongodb\data --journa ...
- 跨域文件 clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-do ...
- Django自定义分页、bottle、Flask
一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1"> ...
- kindeditor编辑器的使用
KindEditor是一款用Javascript编写的开源在线HTML编辑器,主要用户是让用户在网站上获得可见即可得的编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(texta ...
- 济南学习D2T2__数学分析题
[问题描述]有N个数,随机选择一段区间,如果这段区间的所有数的平均值在[l,r]中则你比较厉害.求你比较厉害的概率.[输入格式]第一行有三个数N,l,r,含义如上描述.接下来一行有N个数代表每一个数的 ...
- Cadence学习之——多部分元件原理图封装的画法
在这里以NE5532为例 1.打开新建元件的属性设置框 (1)这里的Package per Pkg设置框就是用来设置元件共有几个部分的. (2)Package Type有两个选项Homogeneous ...
- JS之延迟处理
$(document).ready(function () { $("#zidong3,#zidong1").click(function () { $("#zidong ...
- c# SqlHelper Class
using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...
- Dev GridControl数据导出格式问题
环境:DevExpress9.3,Vs2008 DevExpress的GridControl提供方便的数据导出到Excel功能,导出中用户可以根据GridControl的格式进行导出(ExportTo ...
- [PHP]Yaf + composer 引起大幅性能下降
composer.json 文件可以用命令 composer init 创建,命令是交互式的. 也可以直接编辑一个 json 文件,如下: repositories 中 url 使用中国全量镜像地址. ...