leetcode dfs Palindrome Partitioning
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;
}
leetcode dfs 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 Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
- 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 I&II
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- 【算法】leetcode之 Palindrome Partitioning I&II(转载)
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- 【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 ...
随机推荐
- bzoj4034: [HAOI2015]树上操作(树剖)
4034: [HAOI2015]树上操作 题目:传送门 题解: 树剖裸题: 麻烦一点的就只有子树修改(其实一点也不),因为子树编号连续啊,直接改段(记录编号最小和最大) 开个long long 水模版 ...
- EOJ 2844 排序去重
有 n 个 1 到 1000 之间的整数 (1≤n≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉.然后再按照指定的排序方式把这些数排序. Input 第 1 行为字母 A 或 D,A ...
- struts的工作流程
- 一个请求过来,走前端控制器StrutsPrepareAndExecuteFilter -前端控制器是一个过滤器,过滤器中的核心方法是doFilter(),doFilter方法中首先处 ...
- java8新特性系列:[1]让你的eclipse支持java8
package com.anhui.jdk8; /** * 针对eclipse是否支持java8小测试 * MainClass * @author zhongzh * */ public class ...
- [Java] 总结1.5/1.6/1.7版本的特性
开发过程中接触到了从jdk1.5---jdk1.7的使用,在不同的阶段,都使用过了jdk的一些新特性,操作起来更加方面啦!特此总结了下,以下是测试代码: JDK1.5新特性: 1.自动装箱与拆箱: I ...
- 一步步教会你微信小程序的登录鉴权
前言 为了方便小程序应用使用微信登录态进行授权登录,微信小程序提供了登录授权的开放接口.乍一看文档,感觉文档上讲的非常有道理,但是实现起来又真的是摸不着头脑,不知道如何管理和维护登录态.本文就来手把手 ...
- JDBC基础01
今日知识 1. JDBC基本概念2. 快速入门3. 对JDBC中各个接口和类详解 JDBC: 1. 概念:Java DataBase Connectivity Java 数据库连接, Java语言操作 ...
- MongoDB: The Definitive Guide
第一章 简介 MongoDB是面向文档的数据库,不是关系型数据库.内置对MapReduce的支持,以及对地理空间索引的支持. 丰富的数据模型 容易扩展,它所采用的面向文档的数据模型可以使其在多台服务器 ...
- git window端工具之sourcetree使用
https://www.jianshu.com/p/3478e2a214a1
- rem简单实现移动端适配
rem:移动web开发 默认字体大小是16px 在<html>中设置字体大小 与em的区别: em是在父级设置字体大小受影响 移动端适配 首先获取屏幕的宽度 计算当前屏幕宽度和640的比例 ...