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 ...
随机推荐
- 初尝 JFinal 项目(二)
这里以Roles角色表修改功能做一个例子 RolesController /** * 角色管理控制类 * @author 御手洗红豆 */public class RolesController ex ...
- JavaScript的eval函数
eval() 函数可将字符串转换为代码执行,并返回一个或多个值 函数原型为: 返回值 = eval( codeString ) 函数说明: 如果eval函数在执行时遇到错误,则抛出异常给调用者. 类似 ...
- C#算法之判断一个字符串是否是对称字符串
记得曾经一次面试时,面试官给我电脑,让我现场写个算法,判断一个字符串是不是对称字符串.我当时用了几分钟写了一个很简单的代码. 这里说的对称字符串是指字符串的左边和右边字符顺序相反,如"abb ...
- show status和show variables区别解析
1.show status 查看系统运行的实时状态,便于dba查看mysql当前运行的状态,做出相应优化,动态的,不可认为修改,只能系统自动update. MariaDB [(none)]> ...
- Java for循环的几种用法
J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象.本文介绍使用这种循环的具体方式,说明如何自行定义能被这样遍历的类 ...
- Sharif University CTF 2016 - Smooth As Silk
Category: Crypto Points: 200 Solves: 11 Description: p > q n = p*q = 1146153281852525177586999436 ...
- linux命令(5):rm 命令
linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有 ...
- HTML5 ---localStorage储存实例
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- easyui 中datagrid 点击行的事件
$('#datagrid 的ID').datagrid({ onClickRow:function(index,data) { ...
- (引用)Python 生成随机数小结
转载:http://blog.csdn.net/shuaijiasanshao/article/details/51339438