zoukankan      html  css  js  c++  java
  • leetcode@ [131/132] Palindrome Partitioning & 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","b"],
        ["a","a","b"]
      ]
    class Solution {
    public:
        void Parlindrome(vector<vector<bool> >& isPal, string& s) {
            for(int i=0;i<isPal.size();++i) {
                for(int j=0;j<=i;++j) {
                    isPal[i][j] = true;
                }
            }
            
            for(int len=2;len<=s.length();++len) {
                for(int i=0;i+len-1<s.length();++i) {
                    int j=i+len-1;
                    if(i==j || (s[i]==s[j] && isPal[i+1][j-1])) isPal[i][j] = true;
                }
            }
            
        }
        void dfs(vector<vector<string> >& res, vector<string>& load, vector<vector<bool> >& isPal, string& s, int idx) {
            if(idx == s.length()) {
                res.push_back(load);
                return;
            }
            
            for(int nx=idx;nx<s.length();++nx) {
                if(isPal[idx][nx]) {
                    load.push_back(s.substr(idx, nx-idx+1));
                    dfs(res, load, isPal, s, nx+1);
                    load.pop_back();
                }
            }
        }
        vector<vector<string>> partition(string s) {
            vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false));
            vector<string> load; load.clear();
            vector<vector<string> > res;
            
            Parlindrome(isPal, s);
            dfs(res, load, isPal, s, 0);
            return res;
        }
    };
    leetcode 131: Palindrome Partitioning

    https://leetcode.com/problems/palindrome-partitioning-ii/

    Given a string s, partition s such that every substring of the partition is a palindrome.

    Return the minimum cuts needed for a palindrome partitioning of s.

    For example, given s = "aab",
    Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

    class Solution {
    public:
        void Parlindrome(vector<vector<bool> >&isPal, string& s) {
            for(int i=1;i<s.length();++i) isPal[i][i-1] = true;
            for(int len=1;len<=s.length();++len) {
                for(int i=0;i+len-1<s.length();++i) {
                    int j=i+len-1;
                    if(i==j || (s[i]==s[j] && isPal[i+1][j-1])) isPal[i][j] = true;
                }
            }
        }
        void minCut(vector<vector<bool> >& isPal, vector<int>& cut, string& s) {
            for(int i=0;i<s.length();++i) {
                if(isPal[0][i] == true) {
                    cut[i] = 0;
                    continue;
                }
                
                for(int pre=0;pre<i;++pre) {
                    if(isPal[pre+1][i]) {
                        cut[i] = min(cut[pre]+1, cut[i]);
                    }
                    else cut[i] = min(cut[pre]+i-pre, cut[i]);
                }
            }
        }
        int minCut(string s) {
            vector<int> cut(s.length(), INT_MAX);
            vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false));
            
            Parlindrome(isPal, s);
            minCut(isPal, cut, s);
            return cut[s.length()-1];
        }
    };
    leetcode 132: Palindrome Partitioning II
  • 相关阅读:
    python的整除,除法和取模对比
    jq禁用双击事件
    jq判断滑动方向
    jq获取下拉框中的value值
    html字符串转换成纯文字
    内层div相对于外层div水平垂直居中以及外层div相对body水平垂直居中
    python获取用户输入
    js判断浏览器是否支持localStorage
    CLR的执行模型
    行人检测2(行人检测的发展历史)
  • 原文地址:https://www.cnblogs.com/fu11211129/p/5003061.html
Copyright © 2011-2022 走看看