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. wpf custom control

    最近在做WPF,记录一下自定义控件的制作过程,源码请点击:源码. 1.目标 实现一个如图所示的可增减的数字框: 2.先画Template 可以在Generic.xaml中画,也可以用MergedDic ...

  2. FG模型

    一直没搞懂CvBGStatModel和CvFGDStatModel有什么区别.CvBGStatModel模型的创建用cvCreateGaussianBGModel,CvFGDStatModel模型的创 ...

  3. FCKEditor文件上传提示信息的汉化

    FCKEditor文件上传提示信息的汉化在FCKeditor中,虽然可以自动监测客户端语言,但是仍有小部分信息未能得到汉化.例如上传图片.Flash时,上传成功和上传失败的对话框提示信息均为英文,只要 ...

  4. UITableViewCell Property “icon” cannot be found in forward class object “DJWeiBo”

    UITableViewCell 自定义表格 实体属性不显示错误 Property “icon” cannot be found in forward class object “DJWeiBo”引入实 ...

  5. 从协议VersionedProtocol开始3——ClientProtocol、DatanodeProtocol、NamenodeProtocol、RefreshAuthorizationPolicyProtocol、RefreshUserMappingsProtocol

    1.ClientProtocol这个玩意的版本号是61L:DatanodeProtocol 是26L:NamenodeProtocol是 3L;RefreshAuthorizationPolicyPr ...

  6. Visual studio 2013安装及单元测试

    vs安装过程 单元测试: 创建c#类库 创建单元测试 测试结果

  7. HTML特殊符号对应代码

    HTML中的特殊符号所对应代码,收藏使用 符号 HTML 符号 HTML     & & < < > > ⁄ ⁄ " " ¸ ¸ ° ° ½ ...

  8. Java中方法与数组

    1:方法(掌握) (1)方法:就是完成特定功能的代码块. 注意:在很多语言里面有函数的定义,而在Java中,函数被称为方法. (2)格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参 ...

  9. Add a stylesheet link programmatically in ASP.NET

    Here’s a code snippet used to programmatically insert a stylesheet link to an external CSS file: // ...

  10. Camel routes in Spring config file

    The normal spring bean definition configuration file, the xsi:schemaLocation only has two: beans and ...