【题目】

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"]]

【回溯】

public class Solution {public ArrayList<ArrayList<String>> partition(String s) {ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();ArrayList<String> list = new ArrayList<String>();if (s == null || s.length() == 0)return result;calResult(result,list,s);return result;}/*** 推断一个字符串是否是回文字符串*/private boolean isPalindrome(String str){int i = 0;int j = str.length() - 1;while (i < j){if (str.charAt(i) != str.charAt(j)){return false;}i++;j--;}return true;}/*** 回溯* @param result : 终于要的结果集 ArrayList<ArrayList<String>>* @param list : 当前已经增加的集合 ArrayList<String>* @param str : 当前要处理的字符串*/private void calResult(ArrayList<ArrayList<String>> result, ArrayList<String> list, String str){//当处理到传入的字符串长度等于0,则这个集合list满足条件,增加到结果集中if (str.length() == 0)result.add(new ArrayList<String>(list));int len = str.length();//递归调用//字符串由前往后,先推断str.substring(0, i)是否是回文字符串//假设是的话,继续调用函数calResult,把str.substring(i)字符串传入做处理for (int i=1; i<=len; ++i){String subStr = str.substring(0, i);if (isPalindrome(subStr)){list.add(subStr);String restSubStr = str.substring(i);calResult(result,list,restSubStr);list.remove(list.size()-1);}}}
}

转载于:https://www.cnblogs.com/blfbuaa/p/6913823.html

【LeetCode】Palindrome Partitioning 解题报告相关推荐

  1. LeetCode Palindrome Partitioning II

    原题链接在这里:https://leetcode.com/problems/palindrome-partitioning-ii/ 题目: Given a string s, partition s  ...

  2. LeetCode Palindrome Partitioning(dfs +回文串 )

    问题:给出一个字符串,输出回文串,使得拼接后为原来的字符串 思路:先通过动态规划得到所有的回文串,然后使用深度优先搜索得到所有的解 具体代码参考: https://github.com/wuli249 ...

  3. LeetCode | Palindrome Partitioning I,II

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

  4. [LeetCode] Multiply Strings 解题报告

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  6. Leetcode Weekly 188 解题报告

    文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...

  7. [LeetCode]Palindrome Partitioning 找出所有可能的组合回文

    给定一个字符串,切割字符串,这样每个子字符串是一个回文字符串. 要找出所有可能的组合. 办法:暴力搜索+回溯 class Solution { public:int *b,n;vector<ve ...

  8. [LeetCode]Distinct Subsequences,解题报告

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  9. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

最新文章

  1. canal下载 linux_canal实时同步mysql数据到redis或ElasticSearch
  2. 帝国cms后台上传大视频上传m3u8批量上传一次多个的解决思路和方法
  3. 腾讯:互联网正进入社区化阶段
  4. Firebug 1.7正式版发布,支持Firefox 4
  5. 90后女科学家,四年完成清华大学硕博连读,解决多个世界级难题
  6. 洛谷2657 windy数(数位DP)
  7. 纳税人基本信息与服务器端基本信息不符,网上报税对浏览器有什么要求吗
  8. CSS垂直居中的方法
  9. python千行代码项目_p2:千行代码入门python
  10. 【BZOJ 3294】 3294: [Cqoi2011]放棋子 (DP+组合数学+容斥原理)
  11. 设计模式之(Facade)外观模式
  12. java改变this指向_[Java教程]JavaScript中的this指向
  13. error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用
  14. jdk7 HashSet和HashMap源码分析
  15. 英语语法最终珍藏版笔记-1英语动词的时态
  16. 手把手教你使用R语言做出SCI论文中的表二(单因素分析表)(1)
  17. 中科院大学计算机科学与技术王伟强,王伟强-中国科学院大学-UCAS
  18. android简单旅游界面,快乐五一行 十大Android出游必备应用
  19. 关于家用路由器下挂多个设备包括下挂路由器的操作
  20. jmeter 打开报错_Jmeter-打开jmx文件报错

热门文章

  1. ECS(Linux)连接RDS,使用命令行方式连接实例
  2. 简单两步,spring aop上手即用即会
  3. 2022-2028年中国加气站行业市场研究及前瞻分析报告
  4. python 通过双栈实现队列
  5. debian10 更换阿里源
  6. java数据类型相互转换工具类
  7. windows下多版本python安装与pip安装和pip使用 吐血总结
  8. python中使用指定GPU
  9. Navicat导出表结构导出成Excel
  10. cuSPARSELt开发NVIDIA Ampere结构化稀疏性