Java for LeetCode 131 Palindrome Partitioning
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的更多相关文章
- [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 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
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 ...
- 【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 ...
随机推荐
- 【spring boot】10.spring boot下的单元测试
spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...
- Linux sed 批量替换字符串和更多用法
比如,要将目录/modules下面所有文件中的zhangsan都修改成lisi,这样做: # sed -i “s/zhangsan/lisi/g” `grep zhangsan -rl /module ...
- VS2010 MFC中 Date Time Picker控件的使用
1. 在工具箱中找到Date Time Picker控件,然后拖放到对话框上. 2. 在其属性中按自己的需求做一些设置. Format 属性:Long Date (长日期):****年**月**日 S ...
- 用Golang写的域名信息搜集工具
blacksheepwall是一款由Go语言编写的域名信息搜集工具 ,你也可以在你的工具中将它作为一个独立软件包来使用. 下载 blacksheepwall支持跨平台,目前它所支持的系统有window ...
- Injection of resource dependencies failed解决办法总结
今天调试项目代码,出现的引resource的报错,查原因查了好长时间才找到,现在这里总结一下,以免以后忘掉以及给大家参考. 报错大致内容入下: org.springframework.beans.fa ...
- xml 文件不给提示(以mybatis 的 mapper映射文件为例)
在xxx.xml 映射文件的头部可以看到 如下: (mybatis generate 自动生成) <!DOCTYPE mapper PUBLIC "-//mybatis.org//DT ...
- Linux系统内核性能调优
做过Linux平台性能测试的童鞋平时可能会遇到如下问题: 1. TCP端口号不够用导致并发上不去(即与服务器端建立新连接失败) 2. TIME_WAIT状态连接过多导致应用服务器(Nginx.Hapr ...
- HTML5 Canvas 绘制新西兰国旗
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- js 考记忆力得小游戏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 非常酷的word技巧---删除行前的空格
今天整理一篇文章的时间遇见一个问题,非常多行前的空格严重影响美观.搞计算机的就是爱折腾.于是做了各种尝试完美解决,以下把方法发布例如以下,事实上非常easy哦! 问题例如以下情况所看到的: 解决的方法 ...