131. Palindrome Partitioning (Back-Track, DP)
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到j是否是回文数;然后遍历字符串,对于dp[depth][i]有选择与不选择两种情况,所以使用带回溯的递归,回溯法注意在递归返回后要将递归前改动的内容复原。
class Solution {
public:
void backTracking(string s, int depth, vector<vector<bool>>& dp, vector<string>& current){
for(int i = depth; i <s.length(); i++){
if(dp[depth][i]){
current.push_back(s.substr(depth, i-depth+));
if(i==s.length()-) ret.push_back(current);
else backTracking(s,i+, dp,current);
current.pop_back(); //back track
}
}
}
vector<vector<string>> partition(string s) {
//dp[i][j]: s[i...j] is parlindrome
//dp[i][j] = dp[i-1][j+1] && s[i]==s[j]
//traverse order: shorter one should be checked first, like insert sort
int len = s.length();
vector<vector<bool>> dp(len, vector<bool>(len, false));
vector<string> current;
for(int i = ; i < len; i++) dp[i][i]=true;
for(int i = ; i < len; i++){
for(int j = ; j < i; j++){ //traverse the length
if(s[i]==s[j]){
if(j==i-) dp[j][i] = true;
else dp[j][i]=dp[j+][i-];
}
}
}
backTracking(s, , dp, current);
return ret;
}
private:
vector<vector<string>> ret;
};
131. Palindrome Partitioning (Back-Track, DP)的更多相关文章
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 131. Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
随机推荐
- resin WED服务器初用遇到的问题和解决方法 java.lang.RuntimeException: java.net.SocketException: Unrecognized Windows Socke ts error: 0: JVM_Bind
开启resin 服务器以后提示如下:(控制台不断的循环循环打印如下错误提示) java.lang.RuntimeException: java.net.SocketException: Unrecog ...
- Winform菜单之Menustrip
有窗体必定有菜单了,可以直接使用菜单组件,也可以使用按钮(按钮就没法显示级联菜单的形式了). 下面重点介绍一下各种菜单 1.Menustrip 最常用的莫过于此菜单了,从工具栏中拖入一个menustr ...
- linux命令-定时任务at
本文主要讲解一次性计划任务的命令at的用法! 1.相关操作命令及功能介绍: 在指定的时间执行一个任务,只能执行一次进程名为atd,首先需要确认该进程是否启动,并且配置开机自启动 ps -ef | gr ...
- python requests 设置headers 和 post请求体x-www-form-urlencoded
1.application/json:是JSON格式提交的一种识别方式.在请求头里标示.2.application/x-www-form-urlencoded : 这是form表单提交的时候的表示方式 ...
- DesignPattern(二) 创建型模式
创建型模式 创建型模式就是用来创建对象的模式,抽象了实例化的过程.所有的创建型模式都有两个共同点.第一,它们都将系统使用哪些具体类的信息封装起来:第二,它们隐藏了这些类的实例是如何被创建和组织的.创建 ...
- Java与WCF交互(二):WCF客户端调用Java web service【转】
原文:http://www.cnblogs.com/downmoon/archive/2010/08/25/1807982.html 在上篇< Java与WCF交互(一):Java客户端调用WC ...
- CenOS7.4内核升级修复系统漏洞
先查看当前内核版本# uname -a 一. 导入key# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 二. 安装elrepo ...
- 关于Android应用开发的一些安全注意事项
原文地址: http://www.javacodegeeks.com/2014/05/simple-tips-to-secure-android-app.html ...
- python2用pip进行安装时报错Fatal error in launcher: Unable to create process using '"'
win7下python3和python2共存环境 用pip安装一个包执行pip2 install xxx的时候报错Fatal error in launcher: Unable to create p ...
- 汇编_指令_FLAGS
标志名 标志 1 标志 0 OF (溢出标志) OV ...