131. 分割回文串

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例:

输入: "aab"
输出:
[
["aa","b"],
["a","a","b"]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-partitioning
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 1 public class Solution {
 2     private char[] s = null;
 3
 4     // 返回i位置开始k(k>j)位置结束的回文串的结束位置k
 5     private int nextLoc(int i, int j) {
 6         int l = -1, m, n;
 7         for (l = j+1; l < s.length; l++) {
 8             for (m = i, n = l; m < n; m++, n--) {
 9                 if (s[m] != s[n])
10                     break;
11             }
12             if (m >= n)
13                 return l;
14         }
15         return (l == s.length) ? -1 : l;
16     }
17
18     private void helper(int cur,List<String> subset, List<List<String>> res){
19         if (cur == s.length) {
20             res.add(new ArrayList<>(subset));
21             return;
22         }
23
24         for (int i = cur, j = cur; j != -1; ) {
25             subset.add(String.valueOf(s, i, j-i+1));
26             helper(j+1,subset, res);
27             subset.remove(subset.size()-1);
28             j = nextLoc(i,j);
29         }
30     }
31
32     public List<List<String>> partition(String s) {
33         this.s = s.toCharArray();
34         List<String> subset = new ArrayList<>();
35         List<List<String>> res = new ArrayList<>();
36         helper(0,subset, res);
37         return res;
38     }
39
40     public static void main(String[] args) {
41         Solution solution = new Solution();
42         List<List<String>> abaaab = solution.partition("abaaab");
43         for (List<String> e : abaaab) {
44             System.out.println(e);
45         }
46     }
47 }

原文地址:https://www.cnblogs.com/yfs123456/p/11617557.html

时间: 2024-08-03 06:41:40

131. 分割回文串的相关文章

LeetCode 131. 分割回文串(Palindrome Partitioning)

131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetCode131. Palindrome Partitioning中等 示例: 输入: "aab" 输出: [ ??["aa","b"], ??["a","a","b"]] Java 实现 略

131. 分割回文串-回溯算法 (leetcode)

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 代码: class Solution: def __init__(self): self.res = [] def partition(self, s: str) -> List[List[str]]: self.helper(s,[]) return self.res def helper(self,part_of_s,answerList): if not part_of_s: self.re

lintcode 容易题:Palindrome Partitioning 分割回文串

题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa","b"], ["a","a","b"] ] 解题: 这个题目不好搞啊,需要动态规划 在这里,没有根据动态规划,也解决了,貌似是暴力解决 从下标pos开始,找到下标i使得 pos到i内是回文字符串,再从i+1开始,找到下一

分割回文串[LintCode]

有问题可以私信我,微博地址:http://weibo.com/1683510813/profile?rightmod=1&wvr=6&mod=personinfo 题目:分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa", "b"], ["a", "a", "b"

131 Palindrome Partitioning 分割回文串

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 所有可能的分割方案.例如,给出 s = "aab",返回[  ["aa","b"],  ["a","a","b"]]详见:https://leetcode.com/problems/palindrome-partitioning/description/ class Solution { public: vect

1、分割回文串——回溯法

题目: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa", "b"], ["a", "a", "b"] ] 标签 Expand 回溯法 深度优先搜索 package unit1; import java.util.ArrayList; import java.util.List; publ

九章算法面试题48 分割回文串

九章算法官网-原文网址 http://www.jiuzhang.com/problem/48/ 题目 对于给定字符串,求最少需要几次划分,能够将字符串划分为若干子串,每个子串都是一个回文串.如abaab,需要至少1次划分,将字符串划分为:a|baab,每个部分均为回文串. 解答 这是一道典型的在字符串上进行分割的动态规划的问题.一般的状态表示方法如下:f[i]表示将前i个字符组成的子串进行划分,能够最少划分为多少个串,每个串都是回文串.那么有状态转移方程:f[i] = MIN(f[j] + 1,

132 Palindrome Partitioning II 分割回文串 II

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分割成 ["aa","b"] 这样两个回文子串.详见:https://leetcode.com/problems/palindrome-partitioning-ii/description/ class Solution { public: int minCut(str

力扣——分割回文串

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa","b"], ["a","a","b"] ] class Solution { //动态规划加上回溯,就可以遍历种类.......回溯就是专门用来列出的. public List<List<String>> part