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 ...
随机推荐
- 前台页面获取servlet传过来的数据
servlet中的代码: public void doGet(HttpServletRequest request, HttpServletResponse response) throws Serv ...
- 同步数据库数据到ES中代码
多节点部署保证HA,分布式锁代码 public class DistributedLock implements Watcher,Runnable{ private static final Logg ...
- JAVA Eclipse开发Android如何让屏幕保持为竖直或水平状态
在Manifest.xml文件中找到activity部分,添加下面这一行 android:screenOrientation="landscape" landscape是横向,po ...
- 【C语言天天练(十一)】深入理解指针
引言:在C语言中.指针的地位是不言而喻的,要想非常好的掌握C语言,掌握指针是必须的,这也是C语言不同于其它语言的地方. (一)指针的指针 样例: int i; int *pi;/*把pi初始化为指向变 ...
- 读刘未鹏老大《你应当怎样学习C++(以及编程)》
标签(空格分隔): 三省吾身 原文地址:你应当怎样学习C++(以及编程) 本人反思自己这些年在学校学得稀里糊涂半灌水. 看到这篇文章,感觉收获不少.仿佛有指明自己道路的感觉,当然真正困难的还是坚持学习 ...
- Android源代码解析之(六)-->Log日志
转载请标明出处:一片枫叶的专栏 首先说点题外话,对于想学android framework源代码的同学,事实上能够在github中fork一份,详细地址:platform_frameworks_bas ...
- 微信小程序实战 购物车功能
代码地址如下:http://www.demodashi.com/demo/12400.html 一.准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/ ...
- C#中回调函数的使用方法和区别
归纳来说有两种方式,一种是委托型回调,另一种是接口型回调 委托型回调 委托型回调包括纯委托型和事件型,他们的实现方式是通过公开成员注入的方式,其中纯委托型还可以用构造函数注入.方法注入的方式 接口型回 ...
- Android VS IOS
时间: IOS:var d = new Date("2018-04-19 14:23:00".replace(/-/g, "/")); (d = new Dat ...
- 【甘道夫】Ubuntu14 server + Hadoop2.2.0环境下Sqoop1.99.3部署记录
第一步.下载.解压.配置环境变量: 官网下载sqoop1.99.3 http://mirrors.cnnic.cn/apache/sqoop/1.99.3/ 将sqoop解压到目标文件夹,我的是 /h ...