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"]
      ]
    
    输出总共2^n种,硬做就可以了
    class Solution {
    public:
        void sub(vector<vector<string> >&ret,vector<vector<bool> >&ISP,vector<string>& one,int start,string& s){
            if(start==s.length()){
                ret.push_back(one);
                return;
            }
            int size=one.size();
            int i=1;
            while(i+start<=s.length()){
                if(ISP[start][i]){
                    one.push_back(s.substr(start,i));
                    sub(ret,ISP,one,start+i,s);
                    one.resize(size);
                }
                i++;
    
            }
        }
        bool isPalindrome(string&s,int start,int len){
            for(int i=0;i<len/2;i++){
                if(s[start+i]!=s[start+len-i-1])return false;
            }
            return true;
        }
        vector<vector<string> > partition(string s) {
            vector<vector<string> > ret;
            if(s=="")return ret;
            vector<vector<bool> > ISP;
            ISP.resize(s.length());
            for(int i=0;i<s.length();i++){
                ISP[i].resize(s.length()-i+1);
                for(int j=1;j<ISP[i].size();j++){
                    if(isPalindrome(s,i,j))ISP[i][j]=true;
                }
            }
            vector<string> one;
            sub(ret,ISP,one,0,s);
            return ret;
        }
    };
    View Code
  • 相关阅读:
    hdu 2680:Choose the best route(Dijkstra , SPFA)
    Gym
    Gym
    Gym
    常用的相似度计算
    kafka 配置
    Kafka-broker配置说明
    kafka 安装以及测试
    adaboost算法
    solr scheme配置简介
  • 原文地址:https://www.cnblogs.com/superzrx/p/3356312.html
Copyright © 2011-2022 走看看