131. Palindrome Partitioning

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"]
]

典型的backtracking题目。需要一个函数来判断是否为回文序列,然后s从头至尾,如果前面的是回文序列,则recursive到s的后半部分。
 public IList<IList<string>> Partition(string s) {
        var res  = new  List<IList<string>>();
        if(s =="") return res;
        Backtracking(s,res,new List<string>());
        return res;

    }

    private void Backtracking(string s, List<IList<string>> res , List<string> cur)
    {
        if(s == "")
        {
            res.Add(new List<string>(cur));
        }
        else
        {
            for(int i =1;i<= s.Length;i++)
            {
                if(IsPalindrome(s.Substring(0,i)))
                {
                    cur.Add(s.Substring(0,i));
                    Backtracking(s.Substring(i),res,cur);
                    cur. RemoveAt(cur.Count()-1);
                }
            }
        }
    }

    private bool IsPalindrome(string s)
    {
        if(s == null) return false;
        var c = s.ToCharArray();
        Array.Reverse(c);
        return s == new string(c);
    }
时间: 2024-07-29 00:09:01

131. Palindrome Partitioning的相关文章

Java for LeetCode 131 Palindrome Partitioning

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",

[leedcode 131] Palindrome Partitioning

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","

131. Palindrome Partitioning(未完成)

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","

[LC] 131. Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example: Input: "aab" Output: [ ["aa","b"], ["a","a","b"

Leetcode#131 Palindrome Partitioning

原题地址 因为要找所有的解,只能搜索+回溯了 看来数据量比较小,关于回文串的判断没有使用动态规划也可以过 代码: 1 vector<vector<string> > res; 2 3 bool palindromep(string s) { 4 int i = 0; 5 int j = s.length() - 1; 6 while (i < j && s[i] == s[j]) { 7 i++; 8 j--; 9 } 10 return i >= j;

131 Palindrome Partitioning 分割回文串

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 所有可能的分割方案.例如,给出 s = "aab",返回[  ["aa","b"],  ["a","a","b"]]详见:https://leetcode.com/problems/palindrome-partitioning/description/ class Solution { public: vect

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. Palindrome Partitioning 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"], ["

[email&#160;protected] [131/132] Palindrome Partitioning &amp; Palindrome Partitioning II

https://leetcode.com/problems/palindrome-partitioning/ 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&qu

LeetCode 131. 分割回文串(Palindrome Partitioning)

131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetCode131. Palindrome Partitioning中等 示例: 输入: "aab" 输出: [ ??["aa","b"], ??["a","a","b"]] Java 实现 略