131. Palindrome Partitioning
来源:互联网 发布:淘宝一元捡漏 编辑:程序博客网 时间:2024/06/03 15:20
题目
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”] ]
思路
利用 深度优先搜索去找一个字符串中的回文字符串
如果采用以下算法,找到的子字符串依次为:
[“a”,”a”,”b”]
[“aa”,”b”]
再看一个例子 s = “aacc”
[a, a, c, c]
[a, a, cc]
[aa, c, c]
[aa, cc]
实现代码
public class Solution { public IList<IList<string>> Partition(string s) { IList<IList<string>> rtn = new List<IList<string>>(); subPartition(rtn, new List<string>(), s, 0); return rtn; } void subPartition(IList<IList<string>> rslt, IList<string> curItem, string s, int start) { if (start == s.Length)//搜索的起始位置如果到了终点,说明找到了一组解 { rslt.Add(new List<string>(curItem)); return; } for (int i = start; i < s.Length; i++) //广度方向 { string str = s.Substring(start, i -start + 1); //get substring from start index of s if (isPalindrome(str)) { curItem.Add(str); //深度方向递归 subPartition(rslt, curItem, s, i + 1); curItem.RemoveAt(curItem.Count - 1); } } } bool isPalindrome(string s) { int lo = 0, hi = s.Length - 1; while (lo < hi) { if (s[lo++] != s[hi--]) return false; } return true; } }
阅读全文
1 0
- 131. Palindrome Partitioning && 132. Palindrome Partitioning II
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- 131. Palindrome Partitioning
- 131. Palindrome Partitioning
- 131. Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- 131. Palindrome Partitioning
- Leetcode 131. Palindrome Partitioning
- 131. Palindrome Partitioning
- 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode *** 131. Palindrome Partitioning
- 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- 个人网站升级改版——在上次的那个稳定运行的阿里云服务器上(PhpStorm的Deployment)
- 设置select下拉菜单不弹出列表
- AfxBeginThread的介绍/基本用法(界面/工作)
- $.ajax({});的各个参数的理解
- openssl老版本编译
- 131. Palindrome Partitioning
- AMC美国数学竞赛考试时间
- ajax
- 【SSM开发】MyBatis Generator踩过的坑
- 删除链表中的元素-LintCode
- numpy.savez
- 【Android】这效果,我没法描述
- 图像和流媒体 -- 帧率、分辨率、码流的概念和关系
- 获取Java生成二维码所需要的Jar包(zxing方式)