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 大家好,我是被算法题虐到泪流满面的老三,只能靠发发文章给自己打气! 这一节,我们来看看回 ...
随机推荐
- UVALive-5095 Transportation (最小费用流+拆边)
题目大意:有n个点,m条单向边.要运k单位货物从1到n,但是每条道路上都有一个参数ai,表示经这条路运送x个单位货物需要花费ai*x*x个单位的钱.求最小费用. 题目分析:拆边.例如:u到v的容量为5 ...
- UVA-11167 Monkeys in the Emei Mountain(区间模型最大流+输出方案)
题目大意:有n只猴子,每只猴子都有一组参数(v,a,b),表示这只猴子在时间段[a,b]之间必须要喝v个单位水,并且每个时间单位只能和一个单位水,每次至少喝一个单位.但是只有一个水池,并且这个水池最多 ...
- 002——数组(二)each() list() implode() explode() in_array()
<?php /** * 数组(二)each() list() implode() explode() in_array() */ /*implode() 把数组拆分成字符串 * explode( ...
- 002——php字符串中的处理函数(一)
<?php /** * 字符串处理函数: * 一.PHP处理字符串的空格: * strlen 显示字符串长度 * * trim 对字符串左右空格删除: * ltrim 对字符串左侧空格删除 * ...
- ViewPager + Fragment 实现主界面底部导航栏
1. 四个类似的Frament布局 tab_main_fragment.xml <LinearLayout xmlns:android="http://schemas.android. ...
- Artix-7 50T FPGA试用笔记之Create a simple MicroBlaze System
前言:之前笔者的试用博文提到安富利这块板子非常适合MicroBlaze开发,同时网上关于MicroBlaze的资料非常少(或含糊不清),没有一篇能完整介绍VIVADO SDK的设计流程,所以笔者带来这 ...
- 汉诺塔的c++实现
void hanNuoTa(int n,int a,int b,int c) { ) return; hanNuoTa(n - , a, c, b); cout << n << ...
- BI系统之统计图表的绘制[后端实现]
因为在开发内部BI系统中需要画出统计图表,我选了Jpgraph 开源绘图工具实现需求. 之前实现过需求,没想到这次又花了很多时间回忆,各种搜索,真的是好记性不如烂笔头, 不会总结的人没有未来啊. 常用 ...
- 《DSP using MATLAB》Problem 2.17
1.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...
- fb远程连接服务器调试,碉堡了
开发中经常碰到本地代码没问题,上传到服务器上就有有问题, 这个时候调试变的很麻烦,放个textField自己保存日志这种方式调试的都是. 今天刚学了远程连接服务器,adobe真是牛逼坏了啊. 新增一个 ...