[Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/palindrome-partitioning/description/
Description
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[
["aa","b"],
["a","a","b"]
]
Solution
class Solution {
public:
vector<vector<string>> partition(string s) {
int len = s.length();
vector<vector<string>> res;
vector<string> path;
dfs(0, s, path, res);
return res;
}
void dfs(int idx, string& str, vector<string>& path, vector<vector<string>>& res) {
if (idx == str.length()) {
res.push_back(path);
return;
}
for (int i = idx; i < str.size(); i++) {
if (isPalindrome(str, idx, i)) {
path.push_back(str.substr(idx, i - idx + 1));
dfs(i + 1, str, path, res);
/* pop back every time in recurse stack,
* than all the paces added in dfs can be remove */
path.pop_back();
}
}
}
bool isPalindrome(string& str, int start, int end) {
while (start < end) {
if (str[start] != str[end]) {
return false;
}
start++;
end--;
}
return true;
}
};
解题描述
这道题是目的是找到一个字符串中所有由回文子串组成的集合,算法是对给出的字符串进行遍历,查找所有回文子串,对每个回文子串再进行DFS查找新的回文子串,这样就能找到所有由回文子串切分的子串的集合。
[Leetcode Week13]Palindrome Partitioning的更多相关文章
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [Leetcode][JAVA] Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode】Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- leetcode之 Palindrome Partitioning I&II
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- perf中branch-filter到底是干嘛的?
./arch/x86/events/intel/core.c:2161: data.br_stack = &cpuc->lbr_stack;./arch/x86/e ...
- 在Ubuntu系统下编译arcsim仿真器
首先,用tar zxvf arcsim-0.2.1.tar.gz 将软件包解压 然后,打开里面的INSTALL文件,按照里面的步骤一步一步安装库.Ubuntu13.04下 1.BLAS sudo ap ...
- CentOS7 从查看、启动、停止服务说起systemctl
执行命令“systemctl status 服务名.service”可查看服务的运行状态,其中服务名后的.service 可以省略,这是CenOS7以后采用systemd作为初始化进程后产生的变化. ...
- C++中Set的使用
/* #include <string> // 使用 string 类时须包含这个文件 #include <iostream> // 这个就加上去吧.c++的输入和输出. us ...
- FPGA学习记录_设计一个计数器
此处设计一个数器,使 学习板上 的 LED 状态每 500ms翻转一次. 学习板上晶振为50MHz,也就是说时钟周期为 20ns , 这样可以计算得出 500ms = 500_000_000ns/20 ...
- BZOJ1568:[JSOI2008]Blue Mary开公司——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1568 李超线段树(不会的话去网上搜吧……). 完. #include<map> #in ...
- BZOJ1006 神奇的国度 【弦图染色——最大势算法MCS】
1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 4146 Solved: 1916 [Submit][S ...
- 【神仙题】【P4885】 灭顶之灾
传送门 Description 请将题目名称的首字母连起来读 Scarlet有一张$n*m$的神秘表格.现在Scarlet向表格中填数字,她会从第一行中的某个格子起,按照从左往右,从上往下的顺序依次填 ...
- 解决requests获取源代码时中文乱码问题
用requests获取源代码时,如果是中文网页,就可能会出现乱码,下面我以中关村的网站为例: import requests url = 'http://desk.zol.com.cn/meinv/' ...
- HDU2121:Ice_cream’s world II (虚根+有向图最小生成树)
Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...