标签:
题目描述:
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;
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Palindrome Partitioning
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/49188359