[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 ...
随机推荐
- mongodb 如何区分大小写
mongodb是区分大小写的,在做mongodb数据库操作是经常使用toUpperCase()等方法将value转换为大写存到数据库中 e.g. 在做数据库模糊查询时语句如下 db.COLLECTIO ...
- Immunity Debugger学习笔记
图1::Immunity主界面 注意事项:最下方的PyCommands窗格既可以执行调试命令也可以执行python脚步文件. 1.PyCommands学习 在 Immunity 中执行 Python ...
- javascript节点操作移出节点removeChild()
removeChild(a)是用来删除文档中的已有元素 参数a:要移出的节点 <div id="guoDiv"> <span>1</span> ...
- Apache Doris通过supervisor进行进程管理
下面一段文字是摘自doris官方文档:注:在生产环境中,所有实例都应使用守护进程启动,以保证进程退出后,会被自动拉起,如 Supervisor.如需使用守护进程启动,需要修改各个 start_xx.s ...
- ubuntu 命令汇总
1.linux添加全局变量 //查看当前的全局变量 echo $PATH //打开bashrc 文件,在最后面添加需要 加入的目录路径 vi ~/.bashrc //例如: export PATH=$ ...
- java内存模型(一)正确使用 Volatile 变量
文章转载自: 正确使用 Volatile 变量 Java 语言中的 volatile 变量可以被看作是一种 "程度较轻的 synchronized":与 synchronize ...
- node进阶之用流实现上传文件
内容: 1.文件上传基础 2.node文件处理机制 3.用流实现文件上传 1.文件上传基础 前端代码: <form action="localhost:8080/" meth ...
- 高负载均衡学习haproxy之安装与配置
https://www.cnblogs.com/ilanni/p/4750081.html
- ADODB 调用存储过程
追加参数法调用存储过程 追加参数通过CreateParameter方法,用来指定属性创建新的Parameter对象.具体语法如下: Set parameter = command.CreatePara ...
- 在mfc中picture控件中显示Mat图片<转>
void ShowMatImgToWnd(CWnd* pWnd, cv::Mat img) { if(img.empty()) return; CRect drect; pWnd->GetCli ...