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

难度系数:

中等

实现

bool isPalindrome(string &s, int ii, int jj)
{
    for (int i = ii, j = jj; i < j; i++,j--) {
        if (s[i] != s[j])
            return false;
    }
    return true;
}

vector<vector<string> > partition(string s) {
    vector<vector<string> > vvs;
    int i = 0;
    int j = 0;
    if (s.size() == 1) {
        vector<string> vs;
        vs.push_back(s);
        vvs.push_back(vs);
        return vvs;
    }
    while (j < s.size()) {
        if (isPalindrome(s, i, j)) {
            if (j == s.size() -1) {
                vector<string> vs;
                vs.push_back(s);
                vvs.push_back(vs);
            } else {
                vector<vector<string> > vvstr = partition(s.substr(j+1));
                for (int k = 0; k < vvstr.size(); ++k) {
                    vvstr[k].insert(vvstr[k].begin(), s.substr(i, j-i+1));
                    vvs.push_back(vvstr[k]);
                }
            }
        }
        j++;
    }
    return vvs;
}
时间: 2024-10-10 10:29:02

LeetCode131——Palindrome Partitioning的相关文章

LeetCode131: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","

LeetCode 131. 分割回文串(Palindrome Partitioning)

131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetCode131. Palindrome Partitioning中等 示例: 输入: "aab" 输出: [ ??["aa","b"], ??["a","a","b"]] Java 实现 略

[LeetCode]132.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&qu

leetcode -- 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

132. Palindrome Partitioning II (String; DP)

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

[LeetCode][Java] 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",&

Palindrome Partitioning -- leetcode

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

LeetCode: Palindrome Partitioning II 解题报告

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 pa

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