leetcode 131. Palindrome Partitioning----- java
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"]
]
递归求解,没什么难度。
public class Solution {
List list = new ArrayList<ArrayList<String>>();
char[] word ;
String ss;
String[] result; public List<List<String>> partition(String s) {
int len = s.length();
if( len == 0)
return list;
if( len == 1 ){
ArrayList<String> l1 = new ArrayList<String>();
l1.add(s);
list.add(l1);
return list;
}
ss = s;
word = s.toCharArray();
result = new String[len];
for( int i = 0;i < len;i++){
if( isPalindrome(0,i) ){
result[0] = s.substring(0,i+1);
helper(i+1,1);
}
}
return list; } public void helper(int start,int num){ if( start == word.length ){
ArrayList ll = new ArrayList<String>();
for( int i = 0;i<num;i++)
ll.add(result[i]);
list.add(ll);
return ;
} for( int i = start; i < word.length;i++){
if( isPalindrome(start,i) ){
result[num] = ss.substring(start,i+1);
helper(i+1,num+1);
}
} } public boolean isPalindrome(int start,int end){ while( start < end ){
if( word[start] == word[end] ){
start++;
end--;
}else
return false;
}
return true; }
}
leetcode 131. Palindrome Partitioning----- java的更多相关文章
- [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 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]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 ...
- 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 解题报告(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 ...
随机推荐
- 如何判断Intent有没有对应的Activity去处理?
如何判断Intent有没有对应的Activity去处理?至少有以下两种方法,最近使用过,随笔记下来,以供查阅. 第一种, 如下: public boolean isIntentResolvable(I ...
- IP数据报的格式
1. IP数据报首部的固定部分中的各字段 ①版本:占4位,指IP协议的版本.通信双方使用的 IP协议版本必须一致.日前广泛使用的 IP协议版本号为 4 (即 IPv4). IPv6 目前还处于起步阶段 ...
- Android的R.java文件
1.Android资源管理简介: Android应用程序资源可以分为两大类,分别放在assets和res文件夹下.assets目录下保存的是一些原始的文件,可以以任何方式来进行组织.这些文件最终会被原 ...
- JDBC体会
1.把mysql-connector XXXX版本导入buildpath 2.通过DriverManager 的getConnection 方法获得一个Connnection引用,方法的参数是 url ...
- 《JAVA笔记 day08 静态_单例》
//static关键字: /* 静态方法的使用注意事项: 1,静态方法不能访问非静态的成员. 但是非静态是可以访问静态成员的. 说明:静态的弊端在于访问出现了局限性.好处是可以直接别类名调用. 2,静 ...
- org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
最近在项目中发现如下异常: 六月 25, 2015 5:58:34 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet ...
- 爬虫再探实战(五)———爬取APP数据——超级课程表【四】——情感分析
仔细看的话,会发现之前的词频分析并没有什么卵用...文本分析真正的大哥是NLP,不过,这个坑太大,小白不大敢跳...不过还是忍不住在坑边上往下瞅瞅2333. 言归正传,今天刚了解到boson公司有py ...
- Linux怎么使用添加的新硬盘
一.磁盘分区 装过系统后第一块磁盘的设备号是/dev/sda,在你添加一个新的磁盘后一般情况下是/dev/sdb *******进入fdisk界面***** # fdisk /dev/sdbDevic ...
- Cryptography加密和解密
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Se ...
- VundleVim的安装与使用
git:https://github.com/VundleVim/Vundle.vim vundle是一款vim编辑器的插件管理软,用起来很方便的原因有几点: 1)只要知道插件名称,就能搜索到并下载安 ...