zoukankan      html  css  js  c++  java
  • [LeetCode]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"]
      ]
    class Solution {
    private:
        vector<vector<string> > res;
    public:
        void DFS(string s, vector<string> &ans)
        {
            if(s.size()<1) 
            {
                res.push_back(ans);
                return;
            }
            for(int i=0;i<s.size();i++)
            {
                int start=0;
                int end=i;
                while(start<end)
                {
                    if(s[start]==s[end])
                    {
                      start++;
                      end--;
                    }
                     else break;
                }
                if(start>=end)
                {
                    ans.push_back(s.substr(0,i+1));
                    DFS(s.substr(i+1),ans);
                    ans.pop_back();
                }
            }
        }
        vector<vector<string>> partition(string s) {
            res.clear();
            vector<string> ans;
            DFS(s,ans);
            return res;
        }
    };
    

      

  • 相关阅读:
    TSQL语句
    约束
    数据库创建
    递归
    函数
    结构体
    集合
    jquery中的select
    正则表达式
    多表单提交
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3597364.html
Copyright © 2011-2022 走看看