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 大家好,我是被算法题虐到泪流满面的老三,只能靠发发文章给自己打气! 这一节,我们来看看回 ...
随机推荐
- 2-12-配置squid代理服务器加快网站访问速度
本节所讲内容: squid服务器常见概念 squid服务器安装及相关配置文件 实战:配置squid正向代理服务器 实战:配置透明squid代理提升访问速度 实战:配置squid反向代理加速度内网web ...
- 正则,String中用法,Pattern Matcher
package com.正则表达式; import java.util.Scanner; /** * * 校验qq号码 * 1:要求必须是5-15位数字 * 2: 0不能开头 * 分析: * A:键盘 ...
- New Concept English Two 15 37
listening speaking reading writing and translating $课文35 捉贼! 355. Roy Trenton used to drive a taxi. ...
- JQuery和Zepto的差异(部分)
1.width()/height() Zepto.js: 由盒模型(box-sizing)决定 jQuery: 忽略盒模型,始终返回内容区域的宽/高(不包含 padding.border) jQuer ...
- PyalgoTrade 计算权重平滑平均价(三)
本节介绍如何使用收盘价的SMA价格的策略 from pyalgotrade import strategy from pyalgotrade.barfeed import yahoofeed from ...
- BZOJ3436: 小K的农场(差分约束裸题&DFS优化判环)
3436: 小K的农场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2111 Solved: 986[Submit][Status][Discus ...
- Map集合统计字母次数
Map集合练习:"asfefxAAcf34vrfdfse2-2asd--wdd"获取该字符串中,每一个字母出现的次数要求打印的结果是:a(2)c(1)...;思路:对结果分析发现, ...
- python基于协程的网络库gevent、eventlet
python网络库也有了基于协程的实现,比较著名的是 gevent.eventlet 它两之间的关系可以参照 Comparing gevent to eventlet, 本文主要简单介绍一下event ...
- .NET4.0中使用4.5中的 async/await 功能实现异步
在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...
- wiremock docker 运行
使用docker 模式 docker-compose yaml version: '3.3' services: service1: image: rodolpheche/wiremock ports ...