[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 ...
随机推荐
- Python 常用PEP8规范
目录 目录 代码布局 缩进 最大行宽 空行 模块导入 字符串 表达式和语句中的空格 注释 命名规则 编程建议 代码布局 缩进 每级缩进用4个空格. 括号中使用垂直隐式缩进或使用悬挂缩进. EXAMPL ...
- tensorflow入门资料
google出的说明文档 tensorflow_manual_cn.pdf google出的视频 https://www.zhihu.com/question/41667903/answer/1306 ...
- C++并发编程 02 数据共享
在<C++并发编程实战>这本书中第3章主要将的是多线程之间的数据共享同步问题.在多线程之间需要进行数据同步的主要是条件竞争. 1 std::lock_guard<std::mute ...
- cmd命令总结
1.进入d盘 2.命令运行D:\mongodb\bin中的mongod.exe 显示安装成功!(命令行下运行 MongoDB 服务器)
- ioncube 加密软件 linux 使用方法
https://www.ioncube.com/sa_encoder.php?page=pricing 购买成功后 解压文件包 装了一个linux 版的加密软件 目录:/webdata/soft/io ...
- KDD 2018 | 最佳论文:首个面向Facebook、arXiv网络图类的对抗攻击研究
8 月 19 日至 23 日,数据挖掘顶会 KDD 2018 在英国伦敦举行,昨日大会公布了最佳论文等奖项.最佳论文来自慕尼黑工业大学的研究者,他们提出了针对图深度学习模型的对抗攻击方法,是首个在属性 ...
- solr 7+tomcat 8 + mysql实现solr 7基本使用(安装、集成中文分词器、定时同步数据库数据以及项目集成)
基本说明 Solr是一个开源项目,基于Lucene的搜索服务器,一般用于高级的搜索功能: solr还支持各种插件(如中文分词器等),便于做多样化功能的集成: 提供页面操作,查看日志和配置信息,功能全面 ...
- angular ui.router 路由传参数
angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...
- JS 操作 file标签只上传照片
在当前高版本浏览器里 在标签里加这个属性就够用了 accept="image/*" $('input[type="file"]').live('change', ...
- 使用SendMessage进行进程间通信
Imports System.Runtime.InteropServices Public Class Monitor <DllImport("user32.dll", Ch ...