leetcode dfs Palindrome Partitioning
2024-08-31 08:47:18
Palindrome Partitioning
Total Accepted: 21056 Total
Submissions: 81036My Submissions
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"]
]
题意:切割字符串,使每一个子串都是回文
思路:dfs
选择一个切割点时,假设从起点到切割点的子串是回文,那么该切割点是合法的。能够选择
按这个规则dfs枚举就能够了
复杂度:时间O(n)。空间O(log n)
vector<vector<string> >res; bool is_palindrome(const string &s, int start, int end){
while(start < end){
if(s[start] != s[end - 1]) return false;
++start; --end;
}
return true;
} void dfs(const string &s, int cur, vector<string>& partitions){
int size = s.size();
if(cur == size){
res.push_back(partitions);
}
for(int end = cur + 1; end <= size; ++end){
if(is_palindrome(s, cur, end)){
partitions.push_back(s.substr(cur, end - cur));
dfs(s, end, partitions);
partitions.pop_back();
}
}
} vector<vector<string>> partition(string s) {
vector<string> tem;
dfs(s, 0, tem);
return res;
}
最新文章
- IE7 浏览器下面设置text-indent属性变成margin属性BUG
- [ZZ] A Proposal For Compiling Direct3D HLSL With LLVM (Written by Michael Larabel )
- 运维利器-ClusterShell集群管理操作记录
- javascrit2.0完全参考手册(第二版) 第2章第4节 基本的数据类型
- JSOI 2008 火星人prefix
- ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)
- Inside Kolla - 01 简介
- Win7 VMWare 串口通信
- 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验
- 恼人的Visual Studio 2010崩溃重启问题
- IO 流—>;>;>;补充
- IC芯片設計
- [国嵌笔记][019][Eclipse集成开发环境]
- Gradle 的Daemon配置
- HTMLConverter使用实例(转)
- 包管理工具之Pipenv
- Web高级 Ajax和跨域CORS
- MySQL数据库对象-索引
- Android百大框架排行榜
- Telnet远程登录