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 ...
随机推荐
- python实现八大排序算法
插入排序 核心思想 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为 O(n^2).是稳定的排序方法.插入算法 ...
- boost 中文编码转换
Lstring CHanderHttp::CircleDesc(Lint nCurCircle, Lint nMaxCircle,Lint usercount){ std::stringstream ...
- POJ2374 Fence Obstacle Course
题意 Language:Default Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: ...
- ambassador 学习三 限速处理
与认证类似ambassador 也是委托给三方的其他服务进行限速处理 基本的环境安装可以参考相关文档,主要还是qotm 服务 官方参考实现的简单限速服务 --- apiVersion: v1 kind ...
- 华为OJ:2199 推断输入字符串中的括号匹配
依据不同的括号有个计数器.在遍历时.当计数器小于0则返回false或者当遍历完后,计数器仍旧不为零,也返回false. import java.util.Scanner; public class b ...
- centos6.6 myphpadmin
基本环境为:Centos6.6+Apache2.2.15+php5.3.3+Mysql5.1.73 开始下载了网站上最新版本myPhpAdmin4.3.8安装后打开浏览器为空白页,后百度后都讲是与PH ...
- 详解Centos7 修改mysql指定用户的密码
本文介绍了Centos7 修改mysql指定用户的密码,具体如下: 1.登陆mysql或者mariadb(两种任选其一) [root@localhost ~]# mysql -u root [root ...
- SQL Developer更改日期显示格式
工具->首选项->数据库->NLS->日期格式: DD-MON-RR 修改为: YYYY-MM-DD HH24:MI:SS
- ExtJs 扩展类CheckColumn的使用(事件触发)
[javascript] view plain copy print? 使用 Extjs 在进行数据库编程经常会遇到 checkbox 的问题(奇怪网上却没有此类文章不知道其他人是怎么解决的,在此贴上 ...
- nginx限制请求之三:Nginx+Lua+Redis 对请求进行限制
相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...