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;
    };
  • 相关阅读:
    Spring boot 梳理
    Spring boot 梳理
    Spring boot 梳理
    观察者模式
    设计模式原则
    Spring MVC上传文件
    Spring MVC视图解析器
    Spring MVC中Action使用总结
    Spring MVC控制器
    Java并发 两个线程交替执行和死锁
  • 原文地址:https://www.cnblogs.com/graph/p/3219269.html
Copyright © 2011-2022 走看看