Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)


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

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

示例:

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

分析:给定一个字符串,要求分割,并且要求分割出来的所有的串是回文串。
利用回溯,每次dfs分两个分支 1、不分割继续往下 2、分割后在往下 代码中dfs函数str存放字符串,n存放总长度,step存放当前位置,now是现在已经分割出来的字符串,
remain是字符串剩余还没有分割的,list里装的是分割出来的now 另外加了一个函数来判断,是否为回文串。
class Solution {
List<List<String>> ans = new ArrayList<>(); public List<List<String>> partition(String s) {
if (s.length() == 0 || s == null) {
return ans;
}
dfs(s,s.length(),0,"",s,new ArrayList<String>());
return ans;
} public void dfs(String str, int n, int step, String now, String remain,
ArrayList<String> list) { if (n == step) { if (!now.equals("") && isPalindrome(now)) {
list.add(now);
ans.add(new ArrayList<>(list));
list.remove(list.size() - 1);
}else if(!remain.equals("") && isPalindrome(remain)){
list.add(remain);
ans.add(new ArrayList<>(list));
list.remove(list.size() - 1);
}else if(list.size()!=0 && String.join("",list).equals(str)){ ans.add(new ArrayList<>(list));
} return;
} dfs(str, n, step + 1, now + str.charAt(step),
remain.replaceFirst(str.charAt(step) + "", ""), list); if (!now.equals("") && isPalindrome(now)) {
// System.out.println(step+" "+now + " " + remain);
list.add(now);
dfs(str, n, step + 1, remain.charAt(0)+"", remain.replaceFirst(remain.charAt(0)+"", ""), list);
list.remove(list.size() - 1); } } public boolean isPalindrome(String str) { for (int i = str.length()-1, j = 0; i>=0 && j<=str.length() && i!=j; i--, j++) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
}
return true;
}
}

Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)的更多相关文章

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

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  2. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  3. [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. Java实现 LeetCode 131 分割回文串

    131. 分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa ...

  5. Leetcode 131.分割回文串

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

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

    题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  7. 131. 分割回文串 javascript实现

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

  8. [LeetCode] 132. 分割回文串 II

    题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...

  9. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

随机推荐

  1. Codeforces1144B(B题)Parity Alternated Deletions

    B. Parity Alternated Deletions Polycarp has an array aa consisting of nn integers. He wants to play ...

  2. Android的简述4

    NoteEditor深入分析 首先来弄清楚“日志编辑“的状态转换,通过上篇文章的方法来做下面这样一个实验,首先进入“日志编辑“时会触发onCreate和onResume,然后用户通过Option Me ...

  3. springcloud-provider-consumer-register

    作者:纯洁的微笑出处:http://www.ityouknow.com/ 版权归作者所有,转载请注明出处 上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注 ...

  4. Hadoop自学系列集(四) ---- Hadoop集群

    久等了,近期公司比较忙,学习的时间都没有啊,到今日才有时间呢!!!好了,下面就跟着笔者开始配置Hadoop集群吧. hosts文件和SSH免密码登录配置好了之后,现在进入Hadoop安装目录,修改一些 ...

  5. 在 Windows 上使用 Python 进行 web 开发

    本文由葡萄城技术团队于原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 上一篇我们介绍了在Windows 10下进行初学者入门开发Python的指 ...

  6. 15分钟让你了解如何实现并发中的Barrier

    说到Barrier,很多语言中已经是标准库中自带的概念,一般情况下,只需要直接使用就行了.而最近一些机缘巧合的机会,我需要在c++中使用这么个玩意儿.但是c++标准库里还没有这个概念,只有boost里 ...

  7. 七牛云qshell工具定时备份空间文件到本地

    qshell 是利用七牛文档上公开的 API实现的一个方便开发者测试和使用七牛API服务的命令行工具,使用该工具可以实现很多的功能,今天就分享一下利用qshell定时备份空间文件到本地 1.下载qsh ...

  8. 浅谈Ceph纠删码

    目  录第1章 引言 1.1 文档说明 1.2 参考文档 第2章 纠删码概念和原理 2.1 概念 2.2 原理 第3章 CEPH纠删码介绍 3.1 CEPH纠删码用途 3.2 CEPH纠删码库 3.3 ...

  9. A solution to the never shortened to-do list

    I once told my younger sister my learning system, and the basic five doctrines of my methodology. Bu ...

  10. 原 CNN--卷积神经网络从R-CNN到Faster R-CNN的理解(CIFAR10分类代码)

    1. 什么是CNN 卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(Feedforward Neural Netwo ...