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 大家好,我是被算法题虐到泪流满面的老三,只能靠发发文章给自己打气! 这一节,我们来看看回 ...
随机推荐
- UVA-11761-马尔可夫/记忆化搜索
https://vjudge.net/problem/UVA-11762 给出一个整数n,每次随机挑选一个小于等于n的素数,如果是n的因子,n变为n/x ,否则不变,问n变为1的期望挑选次数. f[i ...
- Leetcode 39
//经典回溯法class Solution { public: vector<vector<int>> combinationSum(vector<int>& ...
- spring的懒加载和depends-on
①延迟初始化Bean(惰性初始化Bean)是指不提前初始化Bean,而是只有在真正使用时才创建及初始化Bean. 配置方式很简单只需在<bean>标签上指定 “lazy-init” 属性 ...
- css中的f弹性盒子模型的应用案例
案例1: <!doctype html> <html> <head> <meta charset="utf-8"> <meta ...
- 哈理工OJ 1328
感觉其实可以不水的. //好像是一道特别水的小学数学题.但是我确实看了很久有试了几个样例才懂得.T_T // 先判断是不是素数.如果是素数的话.An-1一定不等于An.否则的话. // 继续找如果有一 ...
- mybatis----Integer = 0 刷选不出来条件原因以及sql改法
Xml写法: POJO: 当status的值为 0时该where SQLand status = 0并未正常拼接,也就是说test内的表达式为false,从而导致查询结果错误.但是,显然该值(Inte ...
- django-pure-pagination使用方法
1.pip install django-pure-pagination 安装包. 2.加入app: 'pure_pagination', 3.在view中写入分布逻辑. try: page = r ...
- REST风格
1)Representational State Transfer,表述性状态转移,是一种软件架构风格 2)实现步骤 第一步:修改URL 例:http://localhost:8090/SMBMS_C ...
- mysql配置调优-开启慢查询日志-slow_query_log
工作中,会遇到需要查看mysql的top 20 慢sql,逐个进行优化,加上必要的索引这种需求,这时就需要开启数据库的慢查询日志的功能 1.查询当前慢查询日志的状态 # 默认为关闭状态 mysql - ...
- Windows10解决无法访问其他机器共享的问题
你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问.这些策略可帮助保护你的电脑免受网络上不安全设备或恶意设备的威胁. 管理员身份执行sc.exe config lanmanwork ...