leetcode 131. Palindrome Partitioning----- java
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 {
List list = new ArrayList<ArrayList<String>>();
char[] word ;
String ss;
String[] result;
public List<List<String>> partition(String s) {
int len = s.length();
if( len == 0)
return list;
if( len == 1 ){
ArrayList<String> l1 = new ArrayList<String>();
l1.add(s);
list.add(l1);
return list;
}
ss = s;
word = s.toCharArray();
result = new String[len];
for( int i = 0;i < len;i++){
if( isPalindrome(0,i) ){
result[0] = s.substring(0,i+1);
helper(i+1,1);
}
}
return list;
}
public void helper(int start,int num){
if( start == word.length ){
ArrayList ll = new ArrayList<String>();
for( int i = 0;i<num;i++)
ll.add(result[i]);
list.add(ll);
return ;
}
for( int i = start; i < word.length;i++){
if( isPalindrome(start,i) ){
result[num] = ss.substring(start,i+1);
helper(i+1,num+1);
}
}
}
public boolean isPalindrome(int start,int end){
while( start < end ){
if( word[start] == word[end] ){
start++;
end--;
}else
return false;
}
return true;
}
}
leetcode 131. Palindrome Partitioning----- java的更多相关文章
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- Java for LeetCode 131 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- Java for LeetCode 132 Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
随机推荐
- Linux-VLAN
Why Vlan? VLAN是为解决以太网的广播问题和安全性而提出的一种协议,它在以太网帧的基础上增加了VLAN头,用VLAN ID把用户划分为更小的工作组,限制不同工作组间的用户二层互访,每个工作组 ...
- HDU 3341 状态压缩DP+AC自动机
题目大意: 调整基因的顺序,希望使得最后得到的基因包含有最多的匹配串基因,使得所能达到的智商最高 这里很明显要用状态压缩当前AC自动机上点使用了基因的情况所能达到的最优状态 我最开始对于状态的保存是, ...
- 赋值运算符、拷贝初始化和this指针
一.赋值运算符和拷贝构造函数(重载技术) 赋值运算符和拷贝构造函数有编译器默认提供,但如果想做更复杂的事,需要重载. 1.下面用一个简单的例子先区分一下赋值运算符和拷贝构造函数: #include&l ...
- 获取Android系统的版本号
int currentVersion = android.os.Build.VERSION.SDK_INT;
- 【转】CentOS yum安装和卸载软件的使用方法
在CentOS yum安装和卸载软件的使用方法安装方法安装一个软件时. CentOS yum -y install httpd安装多个相类似的软件时 CentOS yum -y install ...
- Note_Master-Detail Application(iOS template)_07_ YJYDetailViewController.m
// YJYDetailViewController.m #import "YJYDetailViewController.h" @interfaceYJYDetailViewC ...
- SVG 2D入门3 - 文本与图像
SVG中渲染文本 SVG的强大能力之一是它可以将文本控制到标准HTML页面不可能有的程度,而无须求助图像或其它插件.任何可以在形状或路径上执行的操作(如绘制或滤镜)都可以在文本上执行.尽管SVG的文本 ...
- BZOJ 3999 旅游
.......好长啊. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- BZOJ 2442 修剪草坪
NOIP的时候一定要看清楚数据范围. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- SSO单点登录
登录持久化机制:Cookies&&Session Cookie:是将信息存储到客户端,所有的信息不安全,都会对信息进行加密,cookies中会存储当前会话的唯一标识,即SessionI ...