LeetCode -- Palindrome Partitioning
来源:互联网 发布:idg 知乎 编辑:程序博客网 时间:2024/05/01 05:08
题目描述:
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"]
]
把字符串s进行分割,要求分割后的每个子串(s[i...j],其中i,j∈[0,n))都是回文。
本题算是回溯问题,与Combination Sum的1和2属于同类问题。
使用经典的回溯模型:
BackTracking(start , current, result):
if start is end :
add current to result
i = start to end:
currentAdd(the [i]th element);
BackTracking(i+1, current, result)
currentRemove(the [i]th element)
至于判断回文的过程就不介绍了,判断两头字符是否相等,直到中间元素位置。
实现代码:
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"]
]
把字符串s进行分割,要求分割后的每个子串(s[i...j],其中i,j∈[0,n))都是回文。
本题算是回溯问题,与Combination Sum的1和2属于同类问题。
使用经典的回溯模型:
BackTracking(start , current, result):
if start is end :
add current to result
i = start to end:
currentAdd(the [i]th element);
BackTracking(i+1, current, result)
currentRemove(the [i]th element)
至于判断回文的过程就不介绍了,判断两头字符是否相等,直到中间元素位置。
实现代码:
public class Solution { public IList<IList<string>> Partition(string s) { if(string.IsNullOrEmpty(s)) { return new List<IList<string>>(); } var result = new List<IList<string>>(); Travel(s, 0, new List<string>() , ref result); return result; }private void Travel(string s , int start, IList<string> current, ref List<IList<string>> result){if(start == s.Length){result.Add(new List<string>(current));return;}for(var i = start + 1; i <= s.Length; i++){var x = s.Substring(start, i - start);if(IsP(x)){current.Add(x);Travel(s, i , current, ref result);current.RemoveAt(current.Count - 1);}}}private bool IsP(string s){if(s.Length == 1){return true;}var len = s.Length % 2 == 0 ? s.Length / 2 : (s.Length - 1) / 2;for(var i = 0;i < len; i++){if(s[i] != s[s.Length - 1- i]){return false;}}return true;}}
1 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- LeetCode -- Evaluate Reverse Polish Notation
- Maven自定义上传第三方包到3rd party(第三方无依赖jar和本地扩展类加入maven统一管理)
- mac修改hosts访问google
- UVA - 12166 Equilibrium Mobile
- LeetCode -- Jump Game
- LeetCode -- Palindrome Partitioning
- UVa12100 Printer Queue (打印队列)
- LeetCode -- Pow(x, n)
- LeetCode -- Rectangle Area
- 疯狂Html+CSS+JS 中JS总结
- R语言 分层抽样---分层随机抽样(SRS) strata的使用(一)
- Shiro系列之Shiro+Mysql实现用户认证(Authentication)
- 在retina屏中实现1px border效果
- PhantomJS基础及示例