[LeetCode]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”],
[“a”,”a”,”b”]
]

思路

此题可以用回溯法解决。把字符串s分为前后两个字串 str1, str2;如果str1是回文,加入partition,然后递归str2.

代码

    /**------------------------------------
    *   日期:2015-03-02
    *   作者:SJF0115
    *   题目: 131.Palindrome Partitioning
    *   网址:https://oj.leetcode.com/problems/palindrome-partitioning/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        vector<vector<string> > partition(string s) {
            vector<string> path;
            vector<vector<string> > result;
            int size = s.size();
            if(size <= 0){
                return result;
            }//if
            Partition(s,size,0,path,result);
            return result;
        }
    private:
        // s源字符串 size 源字符串长度 start 分割点
        // path中间结果 result 最终结果
        void Partition(string str,int size,int start,vector<string> &path,vector<vector<string> > &result){
            // 终止条件
            if(start == size){
                result.push_back(path);
                return;
            }//if
            string substr;
            // 分割字符串
            for(int i = start;i < size;++i){
                substr = str.substr(start,i-start+1);
                // 判断是否是回文串
                if(IsPalindrome(substr)){
                    path.push_back(substr);
                    Partition(str,size,i+1,path,result);
                    path.pop_back();
                }//if
            }//for
        }
        // 判断字符串是否是回文串
        bool IsPalindrome(string str){
            int size = str.size();
            if(size == 0){
                return false;
            }//if
            int left = 0;
            int right = size - 1;
            while(left < right) {
                if(str[left] != str[right]) {
                    return false;
                }//if
                left++;
                right--;
            }//while
            return true;
        }
    };

    int main(){
        Solution s;
        string str("aaba");
        vector<vector<string> > result = s.partition(str);
        // 输出
        for(int i = 0;i < result.size();++i){
            for(int j = 0;j < result[i].size();++j){
                cout<<result[i][j]<<" ";
            }//for
            cout<<endl;
        }//for
        return 0;
    }

运行时间

时间: 2024-08-12 13:05:18

[LeetCode]131.Palindrome Partitioning的相关文章

[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

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] Valid Palindrome II 验证回文字符串之二

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. N

[LeetCode] Largest Palindrome Product 最大回文串乘积

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example: Input: 2 Output: 987 Explanation: 99 x 91 = 9009, 9009 % 1337 = 987 Note: The

LeetCode 9 Palindrome Number (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

[LeetCode]234.Palindrome Linked List

题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 思路 利用双指针法找到链表中点位置,链表中点以后的的元素(不包括中点元素)翻转,再跟链表中点位置以前的元素一一匹配. 代码 /*--------------------------------------- * 日期:2015-07-18 * 作者:SJF01

[LeetCode]9.Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of usin

leetcode 9 Palindrome Number 回文数

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

[经典面试题]回文串专题

[小米]2015小米校招之回文数判断 [百度]2014百度校园招聘之最长回文串 [网易]字符串回文分割 [创新工场]2014创新工场校园招聘之回文串修复 [算法]Manacher算法之最大回文子串 [LeetCode]9.Palindrome Number [LeetCode]125.Valid Palindrome [LeetCode]131.Palindrome Partitioning [LeetCode]132.Palindrome Partitioning II