leetcode131
深度优先遍历(DFS),先判断前一个部分是否是回文,如果是,则将其加进集合中,然后继续判断后面的回文串。
在回溯的时候,将之前加入集合的串删除,重新选择回文串。每到达一次叶子节点,得到一组结果。
public class Solution
{
IList<IList<string>> res = new List<IList<string>>();
public IList<IList<string>> Partition(string s)
{
DFS(s, new List<string>());
return res;
} private void DFS(string s, List<string> list)
{
if (s.Length < )
{
res.Add(new List<string>(list));
return;
}
for (int i = ; i <= s.Length; i++)
{
string str = s.Substring(, i);
if (isPalindrom(str))
{
list.Add(str);
DFS(s.Substring(i), list);
list.RemoveAt(list.Count - );
}
else
{
continue;
}
}
}
private bool isPalindrom(String s)
{ //s必须是》=1的字符串
int p1 = ;
int p2 = s.Length - ;
int len = (s.Length + ) / ;
for (int i = ; i < len; i++)
{
if (s[p1++] != s[p2--])
{
return false;
}
}
return true;
}
}
补充一个python的实现:
class Solution:
def isPalindrome(self,s):
n = len(s)
if n == 0:
return False
if n == 1:
return True
mid = n // 2
i,j = mid,mid
if n % 2 == 0:
i -= 1
while i >=0 and j <= n - 1:
if s[i] != s[j]:
return False
i -= 1
j += 1
return True def backTrack(self,s,res,temp):
if len(s) <= 0:
res.append(temp[:])
return for i in range(len(s)):
sub = s[:i+1]
if self.isPalindrome(sub):
temp.append(sub)
self.backTrack(s[i+1:],res,temp)
temp.pop(-1) def partition(self, s: str) -> 'List[List[str]]':
res = []
self.backTrack(s,res,[])
return res
leetcode131的更多相关文章
- LeetCode131:Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- leetcode131分割回文串
class Solution { public: vector<vector<string>> ans; bool isok(string s){ ; ; while(i< ...
- Leetcode131. Palindrome Partitioning分割回文串
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...
- leetcode131:letter-combinations-of-a-phone-number
题目描述 给出一个仅包含数字的字符串,给出所有可能的字母组合. 数字到字母的映射方式如下:(就像电话上数字和字母的映射一样) Input:Digit string "23"Outp ...
- 数组排列组合问题——BACKTRACKING
BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- LeetCode通关:连刷十四题,回溯算法完全攻略
刷题路线:https://github.com/youngyangyang04/leetcode-master 大家好,我是被算法题虐到泪流满面的老三,只能靠发发文章给自己打气! 这一节,我们来看看回 ...
随机推荐
- TTL的具体含义
TTL(Time To Live)生存时间值,在IP数据包从源到目的的整个转发路径上,每经过一个路由器,路由器都会修改这个TTL字段值,具体的做法是把该TTL的值减1,然后再将IP包转发出去.如果在I ...
- torch中的多线程threads学习
torch中的多线程threads学习 torch threads threads 包介绍 threads package的优势点: 程序中线程可以随时创建 Jobs被以回调函数的形式提交给线程系统, ...
- 聊聊一直困扰前端程序员的浏览器兼容-【JavaScript】
上篇已经写过浏览器的兼容发展历史以及主流浏览器,主要的css兼容我知道的已全部写到,这篇这篇专攻javascript的兼容. 1.getYear()方法 var year = new Date().g ...
- 同一sql表,在页面展示多级菜单
2.
- COM 学习
一.COM (Component Object Model) 二.COM+ (Component Services) 三.DCOM (Distributed Component Object Mode ...
- asp.net 导入Excel记录到数据库中
常用到的一个数据库导入功能,这样的话就省了很大一部分时间来处理程序上的问题而不是无休止的重复复制粘贴动作. 其他的废话不多说,直接上代码: 前提条件: 根目录下建立uploadfiles文件夹(用于保 ...
- Nvidia Driver
https://www.if-not-true-then-false.com/2015/fedora-nvidia-guide/ https://javacodes.cn/322.html https ...
- [sqlserver脚本]查看指定SQL语句生成了哪些执行计划
参考SQL技术内幕写了一段脚本,可以通过这段脚本查看执行指定SQL语句后,系统生成了哪些执行计划.使用时注意以下几点: 修改use MyTest,换成自己的数据库名字. 将 exec sp_page_ ...
- kill与kill -9的区别
有时候我们使用kill无法杀掉一个进程,但是用kill -9却可以,why? 首先看一下kill的功能,man手册对kill描述如下: KILL(1) Linux User's Manual KILL ...
- 网络编程I/O函数介绍
read和write #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); ssize_t write(in ...