【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 ...
随机推荐
- godoc工具使用
golang除了语言有一定的规范外,对于文档的生成也是非常不错的.仅仅要按go的格式来写的程序,都能够非常easy的生成文档. godoc命令介绍: http://golang.org/cmd/god ...
- canvas绘制爱心
需求:绘制爱心图像轨迹. 实现:直接贴代码吧! 预览地址:https://codepen.io/wzc570738205/pen/dqqBpj <!DOCTYPE> <html> ...
- (转)用Lottie制作动画,我的月薪翻了一番!!
Lottie是Airbnb发布的开源动画库. 帮助动效落地.学会使用Lottie,会极大地提高工作效率. Lottie是一种新的开发动画的方式. 学会使用Lottie,会极大改善你和开发小哥哥撕逼的情 ...
- 本地PC安裝Centos 6.5 操作手冊
http://www.xlgps.com/article/130038.html 一.准备工作 1.下载Centos6.5 ISO文件 我在官网上下的6.5版本CentOS-6.5-x86_64-bi ...
- vue 父子组件通信props/emit
props 1.父组件传递数据给子组件 父组件: <parent> <child :childMsg="msg"></child>//这里必须要 ...
- CSS鼠标悬停图片加边框效果,页面布局发生错位的解决办法
CSS鼠标悬停图片加边框效果,页面布局发生错位的解决办法 .recomend-list{ width:1200px; a{ @extend %fl; margin-right: 30px; width ...
- 使用C++部署Keras或TensorFlow模型
本文介绍如何在C++环境中部署Keras或TensorFlow模型. 一.对于Keras, 第一步,使用Keras搭建.训练.保存模型. model.save('./your_keras_model. ...
- java导出html页面
http://blog.csdn.net/zhyh1986/article/details/8727523#t6 http://blog.csdn.net/zuozuofuwaiwai/article ...
- 全球NTP服务器列表
全球- pool.ntp.org 欧洲- europe.pool.ntp.org 亚洲- asia.pool.ntp.org 大洋洲- oceania.pool.ntp.org 北美- north-a ...
- HDU 5533 Dancing Stars on Me( 有趣的计算几何 )
链接:传送门 题意:给出 n 个点,判断能不能构成一个正 n 边形,这 n 个点坐标是整数 思路:这道题关键就在与这 n 个点坐标是正整数!!!可以简单的分析,如果 n != 4,那一定就不能构成正 ...