leetcode ||131、Palindrome Partitioning

problem:

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

Hide Tags

Backtracking

题意:检查字符串的所有子字符串是否为回文字符串

thinking:

(1)这道题理论上不难,采用回溯法遍历所有的子字符串,再检查子字符串是否为回文字符串即可

(2)但难点在于,输出格式得满足判定程序。这一点我的输出格式没能通过。如:

Input:"ab"Output:[["a"],["b"]]Expected:[["a","b"]]

有 时间再研究怎么改吧

code:

class Solution {
private:
   vector<vector<string> > ret;
   vector<string> tmp;
public:
    vector<vector<string>> partition(string s) {
        ret.clear();
        tmp.clear();
        if(s.size()==0)
           return ret;
        check(s,0,0);
        return ret;                   

    }
protected:
   void check(string s,int start, int end)
    {
        if(start>s.size()-1)
            return;
        string str=s.substr(start,end-start+1);
        if(ispalindrome(str))
           tmp.push_back(str);
        if(end<s.size()-1)
           check(s,start,end+1);
        if(end==s.size()-1)
        {
            if(tmp.size()!=0)
                ret.push_back(tmp);
           tmp.clear();
           check(s,start+1,start+1);
        }
    }
   bool ispalindrome(string s)
    {
        int n=s.size();
        int i=0;
        int j=n-1;
        while(i<=j)
        {
          if(s.at(i)==s.at(j))
          {
            i++;
            j--;
          }
          else
            return false;
        }
        return true;
    }
};
时间: 2024-08-28 10:40:59

leetcode ||131、Palindrome Partitioning的相关文章

leetcode || 132、Palindrome Partitioning II

problem: 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 ["

leetcode题目:Palindrome Partitioning 和Palindrome Partitioning II

题目一: 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",

[email&#160;protected] [131/132] Palindrome Partitioning &amp; 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&qu

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

131. 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"], ["

LeetCode算法编程 - Palindrome Partitioning

1.题目 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 OJ: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 笔记24 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",

【leetcode】1278. Palindrome Partitioning III

题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindro

leetcode -day11 Clone Graph &amp; Palindrome Partitioning I II

 1.Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab