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. Android Retrofit使用教程

    Square公司开源了许多优秀的库,Retrofit就是其中之一. Retrofit是用来简化APP访问服务器API,如果你的服务器使用的使RESTAPI,那么赶紧使用Retrofit吧. 官方的文档 ...

  2. NV 3D投影方案 【转】

    http://tu.pcpop.com/all-677013.htm 1/8 NVIDIA 3D方案涉及图形处理器(GPU).信号发射器.快门式3D眼镜.3D播放软件以及经过NVIDIA认证的显示器. ...

  3. [Android实例] Scroll原理-附ScrollView源码分析 (转载)

    想象一下你拿着放大镜贴很近的看一副巨大的清明上河图, 那放大镜里可以看到的内容是很有限的, 而随着放大镜的上下左右移动,就可以看到不同的内容了 android中手机屏幕就相当于这个放大镜, 而看到的内 ...

  4. git工程迁移(修改提交服务器地址)方法

    git remote set-url [--push] <name> <newurl> [<oldurl>]git remote set-url --add [-- ...

  5. 【开发总结】—— BABYLON 项目开发必备系列

    前言:在公司主要使用Babylon.js作为前端引擎,我自己在开发中总结到基本上每一个新项目都会需要这些基本设置. 一.ios兼容:解决镜面反射   如果不加offline,材质中有镜面反射,苹果手机 ...

  6. 过滤器Filter_03_多个Filter的执行顺序

    过滤器Filter_03_多个Filter的执行顺序 学习了:https://www.cnblogs.com/HigginCui/p/5772514.html 按照在web.xml中的顺序进行filt ...

  7. linux中nl用法

    linux 中nl 命令使用 nl :添加行号打印 -b:   指定行号指定的方式,主要有两种:    -b a : 表示不论是否为空行,都同样列出行号    -b t : 如果有空行,则不列出那一行 ...

  8. 好工具MyEclise2016 CI下载

    地址:http://pan.baidu.com/s/1gfBw9Ab 安装后,点开crack目录,按步骤走. 下面是我安装成功的画面.

  9. 简单记录一次ORA-00600: internal error code, arguments: [2662]

    接上一个,REDO报错搞定后OPEN数据库时又报错ORA-00600: internal error code, arguments: [2662]. 原因是_ALLOW_RESETLOGS_CORR ...

  10. 兔子--改动Android Studio的快捷键,改动成eclipse的快捷键

    仅仅须要2步 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill ...