leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/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.
For example, given s = "aab"
,
Return
[
["aa","b"],
["a","a","b"]
]
class Solution {
public:
void Parlindrome(vector<vector<bool> >& isPal, string& s) {
for(int i=;i<isPal.size();++i) {
for(int j=;j<=i;++j) {
isPal[i][j] = true;
}
} for(int len=;len<=s.length();++len) {
for(int i=;i+len-<s.length();++i) {
int j=i+len-;
if(i==j || (s[i]==s[j] && isPal[i+][j-])) isPal[i][j] = true;
}
} }
void dfs(vector<vector<string> >& res, vector<string>& load, vector<vector<bool> >& isPal, string& s, int idx) {
if(idx == s.length()) {
res.push_back(load);
return;
} for(int nx=idx;nx<s.length();++nx) {
if(isPal[idx][nx]) {
load.push_back(s.substr(idx, nx-idx+));
dfs(res, load, isPal, s, nx+);
load.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false));
vector<string> load; load.clear();
vector<vector<string> > res; Parlindrome(isPal, s);
dfs(res, load, isPal, s, );
return res;
}
};
leetcode 131: Palindrome Partitioning
https://leetcode.com/problems/palindrome-partitioning-ii/
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
class Solution {
public:
void Parlindrome(vector<vector<bool> >&isPal, string& s) {
for(int i=;i<s.length();++i) isPal[i][i-] = true;
for(int len=;len<=s.length();++len) {
for(int i=;i+len-<s.length();++i) {
int j=i+len-;
if(i==j || (s[i]==s[j] && isPal[i+][j-])) isPal[i][j] = true;
}
}
}
void minCut(vector<vector<bool> >& isPal, vector<int>& cut, string& s) {
for(int i=;i<s.length();++i) {
if(isPal[][i] == true) {
cut[i] = ;
continue;
} for(int pre=;pre<i;++pre) {
if(isPal[pre+][i]) {
cut[i] = min(cut[pre]+, cut[i]);
}
else cut[i] = min(cut[pre]+i-pre, cut[i]);
}
}
}
int minCut(string s) {
vector<int> cut(s.length(), INT_MAX);
vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false)); Parlindrome(isPal, s);
minCut(isPal, cut, s);
return cut[s.length()-];
}
};
leetcode 132: Palindrome Partitioning II
leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II的更多相关文章
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- Java实现 LeetCode 132 分割回文串 II(二)
132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- Leetcode之二分法专题-275. H指数 II(H-Index II)
Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- 前端与算法 leetcode 350. 两个数组的交集 II
目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
随机推荐
- 搭建 Android 开发环境,初试HelloWorld (win7) (下) (转)
5. 创建AVD 为使Android应用程序可以在模拟器上运行,必须创建AVD. 在Eclipse菜单中,选择 Windows -> Android Virtual Device Manage ...
- Smarty格式化数字为INT数
<? require("setup.php"); define('PAGETITLE','pagtitle'); function insert_top($lid,$sid) ...
- Maven SDK
Maven SDK Details Print Tags: development maven maven2 liferay v6.0 Table of Contents [-] Introdu ...
- C++11多线程教学(一)
本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...
- _CrtIsValidPointer 问题
从微软站点: 检查指针有效性下面的示例使用 _CrtIsValidPointer 验证给定的内存范围对于读或写是否有效. _ASSERTE(_CrtIsValidPointer( address, s ...
- Android 图片缩放
以下演示将一个ImageView的高度设置为两倍: 布局文件main.xml <?xml version="1.0" encoding="utf-8"?& ...
- highcharts 柱形堆叠图
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- SQLite入门与分析(三)---内核概述(2)
写在前面:本节是前一节内容的后续部分,这两节都是从全局的角度SQLite内核各个模块的设计和功能.只有从全局上把握SQLite,才会更容易的理解SQLite的实现.SQLite采用了层次化,模块化的设 ...
- POJ2993——Emag eht htiw Em Pleh(字符串处理+排序)
Emag eht htiw Em Pleh DescriptionThis problem is a reverse case of the problem 2996. You are given t ...
- Oracle命令(一):Oracle登录命令
1.运行SQLPLUS工具 C:\Users\wd-pc>sqlplus 2.直接进入SQLPLUS命令提示符 C:\Users\wd-pc>sqlplus /nolog 3.以OS身份连 ...