题目

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. Python: 列表的基本用法

    列表是可变的,可以改变的序列,它能够保存任何数据类型. >>> list = []        #定义一个空列表>>> list.append(1)        ...

  2. X86架构与ARM架构比较(摘录自网络)

    引言 CPU是怎样运作的? CPU的运作与人脑的运作差不多.先谈一下人这个系统的工作方式.眼镜.耳朵.舌头.皮肤等等感觉器官接收到"触觉",把信息传给大脑,大脑把信息处理后,把处理 ...

  3. 无向图的DFS遍历(方法之一)

    如果看不懂辅助解释在后面第点 1.录入方式: 输入 u - v  表示一边的2个端点 2.存储结构 struct edge { int from; int to; int next; } e[MAXN ...

  4. Using Redis to store php session

    Using Redis to store php session 默认情况下,php将会将session信息存储在文件系统上,在单机情况下没有问题,但是当系统负载增大,或者在对系统可用性要求很高的场景 ...

  5. Portable Basemap Server:多数据源多客户端的底图服务器

    Portable Basemap Server:多数据源多客户端的底图服务器 [poll id=”1″]2014.3.8更新v3.1~在线切片转换为MBTiles时,增加RecreateEmptyCa ...

  6. 企业级监控平台开发之nagios二次开发(七)

    背景: A公司里有很多服务器(>3000台),每台服务器都有不同的用途,如DB Server.WEB Server.ESXI等,每个组使用其中的一批,每个组可能有多个服务器管理员.现在问题出来了 ...

  7. CRM域用户误删恢复

    记录一下: 不小心将CRM用户在域中删除了(CRM中未删除),直接新建一个同样账号的域用户然后尝试在CRM中登录报“invalid user”错误,一番检查发现从2011版本开始CRM中不单记录了用户 ...

  8. 关于前端build工具

    第一.build工具的核心就是帮你安装和帮你做事.安装类工具:Npm.Bower.Yeoman等          做事类工具:Node.Grunt.gulp.Webpack等 安装类工具几乎什么东西 ...

  9. Android_layout 布局(二)

    昨天学习了layout 布局的线性布局和相对布局. 今天我们学习剩余的三个布局,分别是: 一.帧布局(FrameLayout) 在这个布局中,所有的子元素都不能被指定放置的位置,它们通通放于这块区域的 ...

  10. [VM workstation]VM workstation 中的虚拟机连不上网络

    之前一直没有想到虚拟机连不上网络是VM workstationg 自身的原因. 突然在进入虚拟机时看见提示:VM 桥接网桥无法正常工作 于是便进入 编辑→虚拟网络编辑器 中将虚拟网卡都重置了一下就可以 ...