LeetCode(131)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"]
]
Subscribe to see which companies asked this question。
分析
AC代码
class Solution {
public:
vector<vector<string>> partition(string s) {
if (s.empty())
return vector<vector<string>>();
int len = s.length();
if (len == 1)
return vector<vector<string>>(1, vector<string>(1, s));
else
{
vector<vector<string>> ret;
int pos = 0;
while (pos < len)
{
if (isPalindrome(s, 0, pos))
{
if (pos == len - 1)
{
vector<string> tmp;
tmp.push_back(s.substr(0, pos+1));
ret.push_back(tmp);
}
else{
/*获取剩余子串的所有回文分隔结果*/
vector<vector<string>> subRet = partition(s.substr(pos + 1));
auto iter = subRet.begin();
while (iter != subRet.end())
{
(*iter).insert((*iter).begin(), s.substr(0, pos + 1));
ret.push_back(*iter);
++iter;
}//while
}//else
}//if
++pos;
}//while
return ret;
}
}
/*判断是否为回文串*/
bool isPalindrome(string str, int beg, int end)
{
if (beg <0 || beg > end || end >= str.length())
return false;
while (beg < end)
{
if (str[beg++] != str[end--])
return false;
}//while
return true;
}
};
LeetCode(131)Palindrome Partitioning的更多相关文章
- LeetCode(234) Palindrome Linked List
题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- MariaDB5.5(mysql)的partitioning设置 for Zabbix3.0
用zabbix的同学都知道,一台服务器监视几百几千台服务器,一个服务器几十个item,长年下来数据量是很惊人的. 而zabbix自带的housekeeping功能,默认状态下的删除速度完全跟不上数据增 ...
- 新概念英语(1-31)Where's Sally?
新概念英语(1-31)Where's Sally? Is the cat climbing the tree ? A:Where is Sally, Jack ? B:She is in the ga ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
随机推荐
- Tomcat的SSL证书配置以及Tomcat+Nginx实现SSL配置
把jks上传到java容器在的服务器上,路径只要不是webapps下就可以,然后到conf目录下server.xml里配置 <Connector port=" protocol=&qu ...
- css3新属性
CSS calc()函数来制作响应式网格: calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,你可以使用calc()给元素的border.margin.pading.fo ...
- 锋利的js前端分页之jQuery
大家在作分页时,多数是在后台返回一个导航条的html字符串,其实在前端用js也很好实现. 调用pager方法,输入参数,会返回一个导航条的html字符串.方法的内部比较简单. /** * pageSi ...
- c++多态
#include <cstdio> using namespace std; class Base { public: virtual void A() { puts("Base ...
- 又是一天String
(1) Length of Last Word 解题思路:使用length记录最后一个单词的长度.从最后一个字符开始,如果是空字符,length不变.如果不是空字符,length++,直到再次遇到空字 ...
- Windows服务一:新建Windows服务、安装、卸载服务
Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面 ...
- 驱动开发学习笔记. 0.04 linux 2.6 platform device register 平台设备注册 1/2 共2篇
驱动开发读书笔记. 0.04 linux 2.6 platform device register 平台设备注册 1/2 共2篇下面这段摘自 linux源码里面的文档 : Documentatio ...
- Jquery遍历选中的input标签
$("input[name='chkAgent']:[checked]").each(function () { alert($(this).attr("value&qu ...
- Java调用第三方dll文件的使用方法 System.load()或System.loadLibrary()
Java调用第三方dll文件的使用方法 public class OtherAdapter { static { //System.loadLibrary("Connector") ...
- SQL Server 2008 修改表所有者,架构
ALTER SCHEMA 新架构 TRANSFER 旧架构.对象名称