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 ...
随机推荐
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- 20155223 2016-2017-2 《Java程序设计》第8周学习总结
20155223 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第14章 Channel是Java NIO用来衔接数据节点的功能,可定义缓冲区容量.标记内容 ...
- 《DSP using MATLAB》Problem 2.2
1.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...
- 进程的proc文件系统信息
一.实验代码 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include &l ...
- INET_ADDRSTRLEN 和 INET6_ADDRSTRLEN 长度
在<netinet/in.h>中有这两个宏的定义 #define INET_ADDRSTRLEN 16 #define INET6_ADDRSTRLEN 46 INET_ADDRSTRLE ...
- hadoop之 安全模式及SafeModeException
问题: hadoop启动的时候报错 HTTP ERROR 500 Problem accessing /nn_browsedfscontent.jsp. Reason: Cannot issue de ...
- js setInterval每隔一段时间执行一次
js setInterval每隔一段时间执行一次setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval() 方法会不停地调用函数,直到 clearI ...
- emacs之配置etags
emacsConfig/etags-setting.el (require 'auto-complete-etags) (setq ac-sources (append '(ac-source-eta ...
- vim自定义配置之代码折叠
vimConfig/plugin/codeFold-setting.vim "--fold setting-- set foldmethod=syntax " 用语法高亮来定义折叠 ...
- USACO 2016 US Open Contest, Gold解题报告
1.Splitting the Field http://usaco.org/index.php?page=viewproblem2&cpid=645 给二维坐标系中的n个点,求ans=用一个 ...