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 ...
随机推荐
- 从C的声明符到Objective-C的Blocks语法
原文链接:http://nilsou.com/blog/2013/08/21/objective-c-blocks-syntax/ 在这个post中,我先以C简单和内置复杂的声明开始,直到我们开始接触 ...
- 【spring boot logback】日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么
本篇 将针对[日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么]这一个主题进行探索. 这个测试项目是根据[spr ...
- 从Android动画到贝塞尔曲线
基础知识: 动画通过连续播放一系列画面,给视觉造成连续变化的图画.很通俗的一种解释.也很好理解.那么我们先来一个案例看看. 动画案例:百度贴吧小熊奔跑 效果: topic.gif 代码: <?x ...
- Codis连接异常问题处理
报错信息可以看出:由于没有正常的关闭连接,导致连接异常 Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Unex ...
- hdu 3392(滚动数组优化dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others) Me ...
- 何为SLAM
名词解释: SLAM (simultaneous localization and mapping),也称为CML (Concurrent Mapping and Localizatio ...
- Smart Battery Specification Revision 1.1
1.SBS Specifications 2.System Management Bus (SMBus) Specification
- Hibernate学习一----------Hibernate初实现
© 版权声明:本文为博主原创文章,转载请注明出处 ORM(Object/Relationship Mapping):对象/关系映射 - 利用面向对象思想编写的数据库应用程序最终都是把对象信息保存在关系 ...
- *Android 多线程下载 仿下载助手
今天带来一个多线程下载的 样例.先看一下效果.点击 下载 開始下载,同一时候显示下载进度.完成下载,变成程 安装,点击安装 提示 安装应用. 界面效果 线程池 ThreadPoolExecutor , ...
- Android 进阶自定义 ViewGroup 自定义布局
前言 在我们的实际应用中, 经常需要用到自定义控件,比如自定义圆形头像,自定义计步器等等.但有时我们不仅需要自定义控件,举个例子,FloatingActionButton 大家都很常用,所以大家也很经 ...