题目

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。

分析

        给定一个字符串,将其分割为若干个回文子串集合。每个集合中的所有元素均为回文串,且元素连接结果为源串。
        题目类似于之前遇到过的求全集或全排列,考察点均为回溯的思想。对于一个字符串,可以对这个字符串进行遍历,如果前pos个字符串本身是个回文字符,那么只需要求解后面的子字符的回文串即可,于是这个问题被分解成了一个更小的问题。这道题更像一个分治法的题,将问题规模不断缩小,当然的遍历字符串的过程中需要进行回溯。

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;
}
};

GitHub测试程序源码

LeetCode(131)Palindrome Partitioning的更多相关文章

  1. 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 ...

  2. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  3. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  4. MariaDB5.5(mysql)的partitioning设置 for Zabbix3.0

    用zabbix的同学都知道,一台服务器监视几百几千台服务器,一个服务器几十个item,长年下来数据量是很惊人的. 而zabbix自带的housekeeping功能,默认状态下的删除速度完全跟不上数据增 ...

  5. 新概念英语(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 ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. 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 ...

随机推荐

  1. LR常用函数以及调用自定义函数

    2.LR常用函数以及调用自定义函数 2.1.LR常用函数以及对信息的判断 2.1.1. LR内部自定义函数 在LR脚本中定义变量和编写自定义函数,需将变量的声明放在脚本其他内容的上方,否则会提示[il ...

  2. 使用Eclipse上传/下载Git项目

    使用Eclipse上传/下载Git项目 前提: Eclipse已安装EGit插件 已拥有GitLab / GitHub / 其它Git托管服务账号 SSH方式 配置 配置Git信息 配置用户信息 Ec ...

  3. 一步一步学WebSocket(二) 使用SuperWebSocket实现自己的服务端

    上一篇文章,我们了解了客户端如何与服务器创建WebSocket连接.但是一个巴掌拍不响,既然是通信,就必然最少要有两个端.今天我们来看看c#如何用已有的框架实现一个WebSocket服务端. 在.Ne ...

  4. nodejs require

    The rules of where require finds the files can be a little complex, but a simple rule of thumb is th ...

  5. VBA 小知识

    1. 循环 Dim i As Integer 'body Next 'body Wend 2. 键值数据结构 'create dictionary object Set dictMembers = C ...

  6. centos 6.5 msyql5.6安装

    MySQL 安装 安装mysql前需要查询系统中含有的有关mysql的软件. rpm -qa | grep -i mysql   如下 代表有mysql ,则先进行卸载 mysql-libs-5.1. ...

  7. mac上执行sed的编辑 -i命令报错sed: 1: "test.txt": undefined label ‘est.txt’或sed: 1: "2a\test\": extra characters after \ at the end of a command

    问题一 sed编辑命令:[sed -i 's/a/b/g' test.txt]   报错:sed: 1: "test.txt": undefined label 'est.txt' ...

  8. centos7 安装lamp

    1升级gcc4.8以上  yum update gcc2升级openssl     yum update openssl 3安装apache a 安装apr   ./configure --prefi ...

  9. Unity协程截图,WWWForm、WWW配合上传

    先说一下原理.. 截图有两种方法,第一种: Application.CaptureScreenshot(url); 这个API可以截全屏并保存到指定路径 这里我们不采用此方法 下面的代码采用第二种方法 ...

  10. Xcode导航栏不显示模拟器选择框ToolBar

    不显示ToolBar的小伙伴可能就是下面的样子: 全屏后就可以看到ToolBar,像下面这样: 刚开始还以为是模拟器没装,还傻不拉几的去下载模拟器,后来才发现,只要下面的操作即可显示 点击" ...