【LeetCode】Palindrome Partitioning 解题报告
【题目】
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);
}
}
}
}
【LeetCode】Palindrome Partitioning 解题报告的更多相关文章
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
随机推荐
- MyEclipse2015安装SVN插件
一.下载SVN插件subclipse 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 在打开的网 ...
- iis browse的时候,直接通过本地的局域网ip打开页面
http://www.codepal.co.uk/show/make_IIS_work_with_local_IP_addresses_instead_of_localhost 只需要设置一下webs ...
- Swift-UITextField用法
文本框的创建,如下几种方式: UITextBorderStyle.None:无边框 UITextBorderStyle.Line:直线边框 UITextBorderStyle.RoundedRect: ...
- [jzoj 5661] 药香沁鼻 解题报告 (DP+dfs序)
interlinkage: https://jzoj.net/senior/#contest/show/2703/0 description: solution: 注意到这本质就是一个背包,只是选了一 ...
- easyui 之表单验证
/** * 扩展easyui的validator插件rules,支持更多类型验证 */ $.extend($.fn.validatebox.defaults.rules, { minLength : ...
- 浅谈SpringCloud (三) Ribbon负载均衡
什么是负载均衡 当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃.为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力. 我们 ...
- ES6的基本语法
ES6 详细参考页面 简介 ECMAScript和JavaScript的关系是,前者是后者的规格,后者是前者的一种实现.一般来说,这两个词是可以互换的. let命令 ES6新增了let命令,用来声明变 ...
- React router内是如何做到监听history改变的
问题背景 今天面试的时候,被问到这么个问题.在html5的history情况下,pushstate和replacestate是无法触发pushstate的事件的,那么他是怎么做到正确的监听呢?我当时给 ...
- angular自定义指令-directive
Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. v ...
- 学Arduino 需要做哪些准备?(引自"知乎用户:郑兴芳,DhP"的回答)
本人非电子专业,使用Arduino完全出于兴趣,目前主要用于实验过程中的自动化操作. 一.基础准备主要是看一些入门介绍的电子文档,如Arduino_Basic.PDF.ArduinoL2.PDF .& ...