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

      DFS 求解全解神器

    class Solution {
    public:
        bool isPalindrome(const string &s, int start, int end)
        {
            assert(start< len && end < len) ;
            if(start > end) return false ;
            while(start < end)
            {
                if(s[start] != s[end])
                    return false;
                    
                start++;
                end --;    
            }    
            return true;
        }
        void DFS(const string &s,int start, vector<string> p)
        {
            if(start == len) {
                result.push_back(p);
                return ;
            }
            
            for(int i = start; i< len ; i++)
            {
                if(isPalindrome(s,start, i))
                {
                    p.push_back(s.substr(start, i - start +1));
                    DFS(s, i+1, p);
                    p.pop_back();
                }
            }
        }
        vector<vector<string>> partition(string s) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            result.clear();
            len = s.size();
            
            if(len == 0) return result;
            vector<string> p;
            DFS(s, 0, p);
            
            return result ;
        }
    private:
        int len ;
        vector<vector<string>>  result;
    };
  • 相关阅读:
    Django: ModelForm中Meta的fields等成员介绍
    python的random函数
    设置mysql隔离级别
    ubantu 下 修改mysql 默认编码
    jdbc 模板 连接
    sql 注入 与解决
    jdbc 简单连接
    动态代理 例子
    自定义的一个数据输入类
    类加载器 读取配置文件
  • 原文地址:https://www.cnblogs.com/graph/p/3219269.html
Copyright © 2011-2022 走看看