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 大家好,我是被算法题虐到泪流满面的老三,只能靠发发文章给自己打气! 这一节,我们来看看回 ...
随机推荐
- RoR unobtrusive scripting adapter--UJS(一些Javascript的语法糖)
Learn how the new Rails UJS library works and compares with the old version of jquery_ujs that it re ...
- Confluence 6 使用一个页面为站点的默认页面
如果你希望有更多的控制,你可以选择一个 Confluence 的原始页面为你的站点载入页面来替换掉将用户发到主面板中.请查 Configuring the Site Home Page 页面来查看更多 ...
- poj3436网络流之最大流拆点
这题看了半天看不懂题意...还是看的网上题意写的 加一个源点一个汇点,把每个点拆成两个,这两个点的流量是v,其他联通的边都设为无穷大 输入没有1的点就与源点连接,输出只有1的点就与汇点连接 还有这个输 ...
- IOS-网络(大文件下载)
一.不合理方式 // // ViewController.m // IOS_0131_大文件下载 // // Created by ma c on 16/1/31. // Copyright © 20 ...
- CentOS6下源码安装mysql-5.6.25
1.1.系统环境检查 1)检查系统版本 mkdir -p /server/tools/ cd /server/tools/ cat /etc/redhat-release 2)配置域名解析 vim / ...
- I.MX6 PWM buzzer driver hacking with Demo test
/***************************************************************************** * I.MX6 PWM buzzer dr ...
- rancher下的kubernetes之三:在linux上安装kubectl工具
本章是<rancher下的kubernetes>系列之三,前面两章我们完成了racher下搭建kubernetes环境的实战,本章我们来安装kubectl工具: 系列文章地址 <ra ...
- RabbitMQ引入
引入MQ话题 可能很多人有疑惑:MQ到底是什么?哪些场景下要使用MQ? 前段时间安装了RabbitMQ,现在就记录下自己的学习心得吧.首先看段程序: class Program { static vo ...
- 【Beanstalkd】Beanstalkd消息队列的安装与使用
一.Beanstalkd是什么? Beanstalkd是一个高性能,轻量级的分布式内存队列 二.Beanstalkd特性 1.支持优先级(支持任务插队)2.延迟(实现定时任务)3.持久化(定时把内存中 ...
- 0302 IT行业就业与软件工程
阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html http://www.shzhidao.cn/system/2015/09/22/0102610 ...