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的更多相关文章

  1. [LeetCode] 131. Palindrome Partitioning 回文分割

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

  2. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  3. Java for LeetCode 131 Palindrome Partitioning

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

  4. Leetcode 131. Palindrome Partitioning

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

  5. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

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

  6. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  7. 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 ...

  8. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  9. leetcode 132. Palindrome Partitioning II ----- java

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

  10. 【LeetCode】131. Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

随机推荐

  1. wp8.1 Study8:页面过渡和主题动画(Page transition and Theme animations)

    一.在WP8.1中是有动画(Animation)的: 页面导航(默认为旋转式Turnstile).PointerDown/up(默认是倾斜).页面旋转.MenuFlyout出现等等 二.页面过渡(Pa ...

  2. DotNetBar v12.2.0.7 Fully Cracked

    PS: 博客园的程序出现问题,导致我的博客不能访问(转到登录页),而我自己由于 Cookies 问题,一直可以访问,所以一直未发现该问题. 感谢冰河之刃告知,thx! 更新信息: http://www ...

  3. SSH(1)

    假定服务器ip为192.168.1.139,ssh服务的端口号为22,服务器上有个用户为pi,两边都是ubuntu 一,Init *安装 如果是想用ssh从本机登陆别的机器,只需要安装openssh- ...

  4. windows azure中国 里面建立一个虚拟机,与虚拟机建立通信 里面部署IIS,外网访问

    在windows azure中国 里面建立一个虚拟机,里面部署IIS,外网不能访问么? 外网访问的地址是给的那个DNS地址 ,比如我的是 DNS 名称 urbanairserver.cloudapp. ...

  5. Oracle GoldenGate 12c 新特性

    针对Oracle 12c的专门优化: 针对Oracle数据库的集成交付模式:提升在oracle DB中目标端的交付速度: 针对非Oracle数据库的协调交付模式:降低非oracle DB中多线程配置的 ...

  6. Discuz 论坛的搭建(五)

    配置discus论坛 1.下载discus论坛代码 2.解压缩到ApacheProject目录下 3.把discuz的upload文件copy到discuz文件夹下,然后删除upload文件夹 4.修 ...

  7. php的字符串处理

    字符串处理: strlen("aaa");取字符串的长度 *** strcmp("aaa","aaa");比较两个字符串,相同的话输出0,不 ...

  8. hdu 2091

    PS:PE了两次....又是这种奇怪的输出格式....两个三角形直接有空行.. 代码: #include "stdio.h" void ou(int n,char a); void ...

  9. hdoj-2019

    #include "stdio.h"void sort(int number[],int n,int m);int main(){ int n,m,i,number[100]; s ...

  10. hdoj-2031

    #include "stdio.h"#include "stdlib.h"int main(){ char a[]={'0','1','2','3','4',' ...