zoukankan      html  css  js  c++  java
  • LeetCode OJ

    题目:

    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:
        bool isPalindrome(string str) {
            for (int i = 0, j = str.length() - 1; i <= j; i++, j--) {
                if (str[i] != str[j]) {
                    return false;
                }
            }
            return true;
        }
        void search(string s, vector<string> cur_ans, vector<vector<string>> &ans) {
            if (s.empty()) {
                ans.push_back(cur_ans);
            }
            else {
                for (int len = 1; len <= s.length(); len++) {
                    string str = s.substr(0, len);
                    if (isPalindrome(str)) {
                        cur_ans.push_back(str);
                        search(s.substr(len), cur_ans, ans);
                        cur_ans.pop_back();
                    }
                }
                
            }
        }
        vector<vector<string>> partition(string s) {
            vector<string> cur_ans;
            vector<vector<string>> ans;
            search(s, cur_ans, ans);
            return ans;
        }
    };
  • 相关阅读:
    Poj2033
    CodeForces 540
    CodeForces 548
    LeetCode#2 Add Two Numbers
    CodeForces 544A
    POJ 2431Expedition
    HLG1116-选美大赛
    清华学堂 列车调度(Train)
    清华学堂 LightHouse
    清华学堂 Range
  • 原文地址:https://www.cnblogs.com/dongguangqing/p/3727250.html
Copyright © 2011-2022 走看看