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"]
  ]
解题思路一:

将s分为左右两个部分,分别求出两边的partition,然后粘一块即可,JAVA实现如下:

	static public List<List<String>> partition(String s) {
Set<List<String>> set = new HashSet<List<String>>();
if (isPalindrome(s)) {
List<String> alist = new ArrayList<String>();
alist.add(s);
set.add(alist);
}
for (int i = 1; i < s.length(); i++) {
List<List<String>> left = partition(s.substring(0, i));
List<List<String>> right = partition(s.substring(i, s.length()));
for (List<String> aLeft : left)
for (List<String> aRight : right) {
List<String> alist = new ArrayList<String>(aLeft);
alist.addAll(aRight);
set.add(new ArrayList<String>(alist));
}
}
return new ArrayList<List<String>>(set);
} static boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right)
if (s.charAt(left++) != s.charAt(right--))
return false;
return true;
}

结果TLE

解题思路二:

修改下思路一,从左边入手,如果左边是Palindrome,对右边求一个partition,这样求得的结果也不会重复,这样就可以AC了,JAVA实现如下:

	static public List<List<String>> partition(String s) {
ArrayList<List<String>> list = new ArrayList<List<String>>();
if (isPalindrome(s)) {
List<String> alist = new ArrayList<String>();
alist.add(s);
list.add(alist);
}
for (int i = 1; i < s.length(); i++)
if (isPalindrome(s.substring(0, i))) {
List<String> aLeft = new ArrayList<String>();
aLeft.add(s.substring(0, i));
List<List<String>> right = partition(s.substring(i, s.length()));
for (List<String> aRight : right) {
List<String> alist = new ArrayList<String>(aLeft);
alist.addAll(aRight);
list.add(new ArrayList<String>(alist));
}
}
return list;
} static boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right)
if (s.charAt(left++) != s.charAt(right--))
return false;
return true;
}

Java for LeetCode 131 Palindrome Partitioning的更多相关文章

  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 132 Palindrome Partitioning II

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

  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. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

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

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

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

  9. 【LeetCode】131. Palindrome Partitioning

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

随机推荐

  1. mysql日常运维与参数调优

    日常运维 DBA运维工作 日常 导数据,数据修改,表结构变更 加权限,问题处理 其它 数据库选型部署,设计,监控,备份,优化等 日常运维工作: 导数据及注意事项 数据修改及注意事项 表结构变更及注意事 ...

  2. Python Random随机数

    Python产生随机数的功能在random模块中实现.实现了各种分布的伪随机数生成器 该模块能够生成0到1的浮点随机数,也能够在一个序列中进行随机选择.产生的随机数能够是均匀分布.高斯分布,对数正态分 ...

  3. vue组件class绑定

    当在一个自定义组件上使用 class 属性时,这些类将被添加到该组件的根元素上面.这个元素上已经存在的类不会被覆盖. 例如,如果你声明了这个组件: Vue.component('my-componen ...

  4. prop()方法和attr()方法以及区别

    prop()方法: prop() 方法设置或返回被选元素的属性和值. 当该方法用于返回属性值时,则返回第一个匹配元素的值. 当该方法用于设置属性值时,则为匹配元素集合设置一个或多个属性/值对. 注意: ...

  5. Host is not allowed to connect to this MySQL server解决方案

    创建远程登陆用户并授权 grant all privileges on sakila.* to root@192.168.1.210 identified by '123456'; 123456为ro ...

  6. Spring学习十三----------Spring AOP的基本概念

    © 版权声明:本文为博主原创文章,转载请注明出处 什么是AOP -面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 -主要的功能是:日志记录.性能统计.安全控制.事务处理. ...

  7. RGB颜色空间alpha混合的方法

    http://blog.csdn.net/xhhjin/article/details/6444782http://blog.csdn.net/xhhjin/article/details/64454 ...

  8. 宜人贷PaaS数据服务平台Genie:技术架构及功能

    上篇:架构及组件 一.数据平台的发展 1.1 背景介绍 随着数据时代的到来,数据量和数据复杂度的增加推动了数据工程领域的快速发展.为了满足各类数据获取/计算等需求,业内涌现出了诸多解决方案.但大部分方 ...

  9. Getting Started with the G1 Garbage Collector(译)

    原文链接:Getting Started with the G1 Garbage Collector 概述 目的 这篇教程包含了G1垃圾收集器使用和它如何与HotSpot JVM配合使用的基本知识.你 ...

  10. HDFS源码分析之数据块Block、副本Replica

    我们知道,HDFS中的文件是由数据块Block组成的,并且为了提高容错性,每个数据块Block都会在不同数据节点DataNode上有若干副本Replica.那么,什么是Block?什么又是Replic ...