【leetcode】Palindrome Partitioning
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: vector<vector <string> > res; vector<vector<bool> > palindrome;
string s;
int n; vector<vector<string>> partition(string s) { this->s=s;
this->n=s.length(); vector<vector<bool> > palindrome(n,vector<bool>(n));
getPalindrome(palindrome);
this->palindrome=palindrome; vector <string> tmp;
getPartition(,tmp); return res;
} //回溯得到子串
void getPartition(int start,vector<string> tmp)
{ if(start==n)
{
res.push_back(tmp);
return;
} for(int i=start;i<n;i++)
{
if(palindrome[start][i])
{
tmp.push_back(s.substr(start,i-start+));
getPartition(i+,tmp);
tmp.pop_back();
}
}
} void getPalindrome(vector<vector<bool> > &palindrome)
{
int startIndex=;
int endIndex=n-; for(int i=n-;i>=;i--)
{
for(int j=i;j<n;j++)
{
if(i==j)
{
palindrome[i][j]=true;
}
else if(j-i==)
{
palindrome[i][j]=(s[i]==s[j]);
}
else if(j-i>)
{
palindrome[i][j]=(s[i]==s[j]&&palindrome[i+][j-]);
}
}
}
}
};
【leetcode】Palindrome Partitioning的更多相关文章
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
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】【Medium】Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【leetcode】Palindrome Number
题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 【leetcode】Palindrome Number (easy)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
随机推荐
- Could not open Hibernate Session for transaction;
javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Co ...
- js中的with语句
javascript中的with语句是什么? with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象. 看起来 ...
- hdu4547 lca tarjan
比较直接. #include<map> #include<queue> #include<stack> #include<cmath> #include ...
- Java-Vector
package 集合类.list类; import java.util.Vector; public class Vector类 { public static void main(String[] ...
- 12.Android之Tabhost组件学习
TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...
- TCP/IP详解 学习七
静态选路的前提: 1) 网络比较小 2) 网络之间单点连接 3) 网络之间没有多余的路由 动态选路协议,用于路由器之间的通信,有以下几种: 1) ...
- 用word写博客
都知道word的编辑功能强大,那如何用word写博客呢? 以博客园为例 1.写好word文档后,文件->共享->发送至博客,或者新建博客模板 2. 再博客的界面点击管理账户,新建账户,如果 ...
- Centos目录结构详细版
使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...
- MongoDB的安装 转
第1章 MongoDB的安装 (黎明你好原创作品,转载请注明) 1.1 MongoDB简介 MongoDB是一个基于分布式文件存储的数据库开源项目.由C++语言编写,旨在为WEB应用提供可护展的高性能 ...
- 创建gbk编码
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_1803 ...