[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 ...
随机推荐
- eclipse在线安装jd反编译插件
eclipse在线安装jd反编译插件地址 http://jd.benow.ca/jd-eclipse/update
- osx 安装redis
brew install redis 想关文章 http://www.tuicool.com/articles/nM73Enr http://www.iteye.com/topic/1124400
- timus1745题解
一.题目链接 http://acm.timus.ru/problem.aspx?space=1&num=1745 二.题意 给定$n$个由'('和')'组成的字符串,每个串最多只能使用$1$次 ...
- 第8章 信号(1)_Linux信号处理机制
1. 信号的基本概念 1.1 基本概念 (1)信号(signal)机制是linux系统中最为古老的进程之间的通信机制,解决进程在正常运行过程中被中断的问题,导致进程的处理流程会发生变化. (2)信号本 ...
- 第7章 进程关系(5)_贯穿案例2:mini shell(2)
5. 贯穿案例2:mini shell(2) (1)己经完成的功能:pwd.cd.exit命令 (2)阶段性目标: ①env.export.echo及其他命令 ②标准输入.输出重定向"> ...
- 可怕的SCN!(转载)
一.什么是SCNSCN(System Change Number 简称 SCN)是当Oracle数据库更新后,由DBMS自动维护去累积递增的一个数字.在Oracle中,有四种SCN,分别为:系统检查点 ...
- hive-hbase-handler方式导入hive表数据到hbase表中
Hive与HBase的整合功能的实现是利用两者本身对外的API接口互相进行通信,相互通信主要是依靠hive-hbase-handler.jar工具类 : hive-hbase-handler.jar在 ...
- DOS 批处理命令For循环命令详解
for命令是一种对一系列对象依次循环执行同一个或多个命令的在命令行或批处理中运行的命令,结合一些Windows管理中的程序后,其处理功能强大.应用灵活方便程度令人刮目相看 for命令是一种对一系列 ...
- 查看shell环境下,网络是否连通-curl/ping
检查网络是否可用 curl www.baidu.com <!--STATUS OK--><html>...</html> ping www.baidu.com注意: ...
- 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST
▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...