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

    Solution: ...

     1 class Solution {
     2 public:
     3     vector<vector<string>> res;
     4     vector<vector<string>> partition(string s) {
     5         res.clear();
     6         vector<string> part;
     7         partitionRe(s, 0, part);
     8         return res;
     9     }
    10 
    11     void partitionRe(const string &s, int start, vector<string> &part) {
    12         if (start == s.size())
    13         {
    14             res.push_back(part);
    15             return;
    16         }
    17         string palindrom;
    18         for (int i = start; i < s.size(); ++i) {
    19             palindrom.push_back(s[i]);
    20             if (!isPalindrome(palindrom)) continue;
    21             part.push_back(palindrom);
    22             partitionRe(s, i + 1, part);
    23             part.pop_back();
    24         }
    25     }
    26 
    27     bool isPalindrome(const string &s) {
    28         int i = 0, j = s.size()-1;
    29         while (i < j) {
    30             if (s[i] != s[j]) return false;
    31             i++; j--;
    32         }
    33         return true;
    34     }
    35 };
  • 相关阅读:
    构造函数+this关键字+super关键字
    封装
    数组+方法
    流程控制语句
    java运算符+引用数据类型
    java的基础安装
    mysql数据库连接
    mysql数据库约束
    mysql数据库
    练习010:按奇偶排序数组
  • 原文地址:https://www.cnblogs.com/zhengjiankang/p/3682307.html
Copyright © 2020-2023  润新知