[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.
Example:
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
]
题意:
拆分给定字符串,找到可将字符串分隔成回文substring的可能
思路:
backtracking枚举所有结果
容易掉坑:对于递归调用helper的时候,i+1 还是index+1 很容易就写错了!!!
代码:
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<>();
List<String> path = new ArrayList<>();
helper(s, 0, path, result);
return result;
} private void helper(String s, int index, List<String> path,List<List<String>> result){
if( index == s.length()){
result.add(new ArrayList<>(path));
}
for(int i = index ; i < s.length() ; i++){
if(isPalindrome(s, index, i)){ // 如果index--i 的这段串串是回文
path.add(s.substring(index, i+1));// 将index--i 的这段回文串串加到path里
helper(s, i+1, path, result); // 继续移动index递归生成可能的结果
path.remove(path.size() -1); //比如"baa" 从root 为'b'开始则先生成Arraylist : 'b' + 'a' + 'a' 则要去掉最后一个元素'a'
// 再看 'b' + 'a' + '其他' 能新生成Arraylist
}
}
}
// 判断是否回文
private boolean isPalindrome(String s, int left, int right){
while (left < right){
if(s.charAt(left) != s.charAt(right)){
return false;
}
left++;
right--;
}
return true;
}
}
[leetcode]131. Palindrome Partitioning字符串分割成回文子串的更多相关文章
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- [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 ...
- Palindrome Partitioning LightOJ - 1044(回文串最小分割数,O(n^2)预处理子串是否回文)
题意:将一个字符串分割成最少的字符串,使得分割出的每个字符串都是回文串.输出最小的分割数. 方法(自己的):先O(n^2)(用某个点或某个空区间开始,每次向左右扩展各一个的方法)处理出所有子串是否回文 ...
- LeetCode(5):最长回文子串
Medium! 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 长度最长为1000. 示例: 输入: "babad" 输出: "bab&quo ...
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- #leetcode刷题之路5-最长回文子串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1:输入: "babad"输出: "bab"注意: " ...
- LeetCode随缘刷题之最长回文子串
这一题我用的相对比较笨的方法. 相对于大佬们用的动态规划法,比较复杂.但却更容易理解,我主要是通过记录下标来确定最长回文串的. package leetcode.day_12_06; /** * 给你 ...
- [leetcode]5. Longest Palindromic Substring最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- php 七种数据类型介绍
PHP有7个数据类型.七个类型: 字符串, 整数, 浮动, 布尔, 数组, 对象, 资源. 字符串 字符串保持字符,如“一”.“abc”,“www.manongjc.com”等.PHP字符串是区分大小 ...
- 解决Sybase PowerDesigner 数据库设计中 Name 自动填充Code
在使用 Sybase PowerDesigner 进行数据库设计时,为了理清思路,需要将name改为中文名称,但是这个软件会自动将name填 充为code,可以通过如下配置修改: 选择tools-&g ...
- python 字体颜色的设置
实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27 ...
- UVA-575-水题-模拟
题意: 按照这个公式模拟 10120skew = 1×(25 −1)+0×(24 −1)+1×(23 −1)+2×(22 −1)+0×(21 −1) = 31+0+7+6+0 = 44. #inclu ...
- 77. sqlserver 锁表解决方式
select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tra ...
- Angular5 UI post 请求 输出 文件下载
this.httpClient.post(url1, JSON.parse(data1) , {responseType: 'blob'}).subscribe(data => { const ...
- leetcode455
public class Solution { public int FindContentChildren(int[] g, int[] s) { var listg = g.OrderBy(x = ...
- Spring MVC 确定目标方法POJO 类型参数
1:确定一个Key 2. 在implicitMode 中存在Key 对应的对象, 若存在则作为参数传入 3. 在implicitMode 中不存在Key 对应的对象, 则检查当前@SessionAtr ...
- 15 MySQL--索引
索引: http://www.cnblogs.com/linhaifeng/articles/7356064.html http://www.cnblogs.com/linhaifeng/articl ...
- centos7 防火墙配置
firewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=8080/t ...