【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 ...
随机推荐
- iOS-获取Model(设备型号)、Version(设备版本)、app(程序版本)等
IOS-获取Model(设备型号).Version(设备版本).app(程序版本)等 NSLog(@"uniqueIdentifier: %@", [[UIDevice curre ...
- 【剑指Offer学习】【面试题26:复杂链表的复制】
题目:请实现函数ComplexListNode clone(ComplexListNode head),复制一个复杂链表. 在复杂链表中,每一个结点除了有一个next 域指向下一个结点外,另一个sib ...
- Ubuntu18.04修改Hostname
1. 设置新的hostnamesudo hostnamectl set-hostname newNameHere 2. 修改配置文件使hostname可以保存编辑这个文件: /etc/cloud/cl ...
- Java中的作用域有哪些
在Java语言中,变量的类型主要有3种:成员变量.静态变量和局部变量 首先说静态变量跟局部变量 静态变量不依赖于特定的实例,而是被所有实例共享,也就是说,只要一个类被加载,JVM就会给类的静态变量分配 ...
- vue 2.x axios 封装的get 和post方法
import axios from 'axios' import qs from 'qs' export class HttpService { Get(url, data) { return new ...
- Android蓝牙2.0连接以及数据接收发送
1.加入权限 <uses-feature android:name="android.hardware.bluetooth_le" android:required=&quo ...
- Paper-[arXiv 1710.03144]Island Loss for Learning Discriminative Features in Facial Expression
[arXiv 1710.03144]Island Loss for Learning Discriminative Features in Facial Expression ABSTRACT 作者在 ...
- Stack Overflow大揭密:哪一种程序员工资最高?
Stackoverflow在程序员之间可以說是无人不知无人不晓,甚至常有人开玩笑说:“如果stackoverflow倒闭了,全世界代码的产出率将下降一半以上”或许听起来有点夸张,但是不难想像这个网站在 ...
- CorelDRAW简单绘制的一杯满满的橙汁教程
CorelDRAW怎么画一杯橙汁?方法很简单,首先绘制一个闭合路径,执行线性渐变,填充颜色:复制图形,使用刻刀工具裁剪两半,更改不透明度:然后为橙汁增加底部椭圆:修剪橙子片:绘制吸管:最后加上一层橙子 ...
- Codeforces Round #487 (Div. 2) C. A Mist of Florescence 构造
题意: 让你构造一个 n∗mn*mn∗m 矩阵,这个矩阵由 444 种字符填充构成,给定 444 个整数,即矩阵中每种字符构成的联通块个数,n,mn,mn,m 需要你自己定,但是不能超过505050. ...