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

  1. [
  2. ["aa","b"],
  3. ["a","a","b"]
  4. ]

思路: 首先使用动态规划纪录从i到j是否是回文数;然后遍历字符串,对于dp[depth][i]有选择与不选择两种情况,所以使用带回溯的递归,回溯法注意在递归返回后要将递归前改动的内容复原。

  1. class Solution {
  2. public:
  3. void backTracking(string s, int depth, vector<vector<bool>>& dp, vector<string>& current){
  4. for(int i = depth; i <s.length(); i++){
  5. if(dp[depth][i]){
  6. current.push_back(s.substr(depth, i-depth+));
  7. if(i==s.length()-) ret.push_back(current);
  8. else backTracking(s,i+, dp,current);
  9. current.pop_back(); //back track
  10. }
  11. }
  12. }
  13. vector<vector<string>> partition(string s) {
  14. //dp[i][j]: s[i...j] is parlindrome
  15. //dp[i][j] = dp[i-1][j+1] && s[i]==s[j]
  16. //traverse order: shorter one should be checked first, like insert sort
  17. int len = s.length();
  18. vector<vector<bool>> dp(len, vector<bool>(len, false));
  19. vector<string> current;
  20. for(int i = ; i < len; i++) dp[i][i]=true;
  21. for(int i = ; i < len; i++){
  22. for(int j = ; j < i; j++){ //traverse the length
  23. if(s[i]==s[j]){
  24. if(j==i-) dp[j][i] = true;
  25. else dp[j][i]=dp[j+][i-];
  26. }
  27. }
  28. }
  29. backTracking(s, , dp, current);
  30. return ret;
  31. }
  32. private:
  33. vector<vector<string>> ret;
  34. };

131. Palindrome Partitioning (Back-Track, DP)的更多相关文章

  1. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  2. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  3. 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 ...

  4. 132. Palindrome Partitioning II (String; DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  6. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  7. 131. Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  8. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. 【LeetCode】131. Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

随机推荐

  1. python实现八大排序算法

    插入排序 核心思想 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为 O(n^2).是稳定的排序方法.插入算法 ...

  2. boost 中文编码转换

    Lstring CHanderHttp::CircleDesc(Lint nCurCircle, Lint nMaxCircle,Lint usercount){ std::stringstream ...

  3. POJ2374 Fence Obstacle Course

    题意 Language:Default Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: ...

  4. ambassador 学习三 限速处理

    与认证类似ambassador 也是委托给三方的其他服务进行限速处理 基本的环境安装可以参考相关文档,主要还是qotm 服务 官方参考实现的简单限速服务 --- apiVersion: v1 kind ...

  5. 华为OJ:2199 推断输入字符串中的括号匹配

    依据不同的括号有个计数器.在遍历时.当计数器小于0则返回false或者当遍历完后,计数器仍旧不为零,也返回false. import java.util.Scanner; public class b ...

  6. centos6.6 myphpadmin

    基本环境为:Centos6.6+Apache2.2.15+php5.3.3+Mysql5.1.73 开始下载了网站上最新版本myPhpAdmin4.3.8安装后打开浏览器为空白页,后百度后都讲是与PH ...

  7. 详解Centos7 修改mysql指定用户的密码

    本文介绍了Centos7 修改mysql指定用户的密码,具体如下: 1.登陆mysql或者mariadb(两种任选其一) [root@localhost ~]# mysql -u root [root ...

  8. SQL Developer更改日期显示格式

    工具->首选项->数据库->NLS->日期格式: DD-MON-RR 修改为: YYYY-MM-DD HH24:MI:SS

  9. ExtJs 扩展类CheckColumn的使用(事件触发)

    [javascript] view plain copy print? 使用 Extjs 在进行数据库编程经常会遇到 checkbox 的问题(奇怪网上却没有此类文章不知道其他人是怎么解决的,在此贴上 ...

  10. nginx限制请求之三:Nginx+Lua+Redis 对请求进行限制

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...