【Lintcode】136.Palindrome Partitioning
题目:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example
Given s = "aab", return:
[
["aa","b"],
["a","a","b"]
]
题解:
Solution 1 ()
class Solution {
public:
vector<vector<string>> partition(string s) {
if (s.empty()) {
return {{}};
}
vector<vector<string> > res;
vector<string> v;
dfs(res, v, s, );
return res;
}
void dfs(vector<vector<string> > &res, vector<string> &v, string s, int pos) {
if (pos >= s.size()) {
res.push_back(v);
return;
}
string str;
for (int i = pos; i < s.size(); ++i) {
str += s[i];
if (isValid(str)) {
v.push_back(str);
dfs(res, v, s, i + );
v.pop_back();
}
}
}
bool isValid(string str) {
if (str.empty()) {
return true;
}
int begin = ;
int end = str.size() - ;
for (; begin <= end; ++begin, --end) {
if (str[begin] != str[end]) {
return false;
}
}
return true;
}
};
Solution 1.2 ()
class Solution {
public:
bool isPalindrome(string &s, int i, int j){
while(i < j && s[i] == s[j]){
i++;
j--;
}
if(i >= j) {
return true;
} else {
return false;
}
}
void helper(vector<vector<string>> & res, vector<string> &cur, string &s, int pos){
if(pos == s.size()){
res.push_back(cur);
return;
}
for(int i = pos; i <= s.size() - ; i++){
if(isPalindrome(s, pos, i)){
cur.push_back(s.substr(pos, i - pos + ));
helper(res, cur, s, i+);
cur.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> cur;
helper(res, cur, s, );
return res;
}
};
【Lintcode】136.Palindrome Partitioning的更多相关文章
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 【动态规划】POJ3280- Cheapest Palindrome
[题目大意] 给出一个字符串,可以删除或添加一些字符,它们各自会消耗价值.问最少消耗多少价值,可以使得字符串变成回文的. [思路] 事实上删除或添加字符的价值只需要保持较小的那一个.假设当前要将(j, ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- MP4文件格式的解析,以及MP4文件的分割算法
http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...
- mac上利用minikube搭建kubernetes(k8s)环境
友情提示:对于初次接触k8s的同学,强烈建议先看看本文最后的参考文章. 环境: mac os(Mojave) 前提:先安装好kubectl (brew install kubectl) .docker ...
- MySQL CREATE TRIGGER (1)
CREATE TRIGGER语法 CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW ...
- Android 红色小圆球提示气泡 BadgeView
今天给大家分享两个实用有简单的一个小圆球提示气泡: BadgeView 参考地址: https://github.com/qstumn/BadgeView; 个人地址:http://git ...
- python 基础 7.7 json--上
一. 文件json ...
- 远程访问(HttpClient和HttpResponse的使用) 原型模式
package com.webserver.webservice; import java.io.ByteArrayInputStream; import java.io.FileOutputStre ...
- mysql系列之9.mysql日志&存储引擎
mysqlbinlog 是什么? 数据目录下的如下文件: mysql-bin.xxxxxx 作用? 记录数据库内部增删改查对mysql数据库有更新的内容的记录 三种模式? statement leve ...
- mysql系列之8.mysql高可用 (keepalived)
环境: centos6.5_x64 准备: 两台mysql机器 主1 master: 192.168.32.130 主2 backup: 192.168.32.131 VIP: 192.168.3 ...
- HDU - 1495 非常可乐 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1495 思路 首先 如果可乐的体积 是奇数 那么是无解的 然后 如果能够得到两杯 都是一般容量的可乐 那 ...
- iOS define 宏定义 和 const定义常量区别
const const 是c++中的修饰符. c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1. 对于co ...