博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode-131] Palindrome Partitioning
阅读量:4487 次
发布时间:2019-06-08

本文共 1838 字,大约阅读时间需要 6 分钟。

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

 

历遍的说……

 

1 class Solution { 2     public: 3         vector
> result; 4 5 void search_palin(const string& s, int start, vector
parti, 6 const vector
>& is_palin) 7 { 8 if (start == s.length()) { 9 result.push_back(parti);10 return;11 }12 for (int i = start; i < s.length(); ++i) { 13 if (is_palin.at(start).at(i)) {14 parti.push_back(s.substr(start, i - start + 1)); 15 search_palin(s, i + 1, parti, is_palin); 16 parti.pop_back(); 17 }18 }19 }20 21 vector
> partition(string s) { 22 // Start typing your C/C++ solution below 23 // DO NOT write int main() function 24 25 // if char i to char j palin 26 int slen = s.length(); 27 vector
> is_palin(slen, vector
(slen, false)); 28 for (int j = 0; j < slen; ++j) { 29 for (int i = 0; i <= j; ++i) { 30 if (i == j) { 31 is_palin.at(i).at(j) = true; 32 } else if (i + 1 == j) { 33 is_palin.at(i).at(j) = s[i] == s[j]; 34 } else {35 is_palin.at(i).at(j) = is_palin.at(i + 1).at(j - 1) && s[i] == s[j];36 }37 }38 }39 40 vector
parti;41 result.clear();42 parti.clear();43 search_palin(s, 0, parti, is_palin);44 return result;45 }46 };
View Code

 

转载于:https://www.cnblogs.com/leon-wang/p/3267700.html

你可能感兴趣的文章
《构建之法》(五)
查看>>
创建django项目
查看>>
Linux Bash基本功能
查看>>
一则小脚本(工作中用)
查看>>
软件工程结对作业
查看>>
Keil 4.0 生成bin文件
查看>>
sql语句的进化--hibernate篇
查看>>
python爬虫之cookie
查看>>
2017年5月29号课堂笔记
查看>>
HDU4247【瞎搞】
查看>>
lightoj 1125【背包·从n个选m个】
查看>>
HDU 1243 反恐训练营(最长公共序列)
查看>>
mysql数据库隔离级别
查看>>
(六)buildroot使用详解
查看>>
chrome修改UserAgent,调试
查看>>
Source Insight4.0 试用。。试用。。试用。。
查看>>
python循环for,range,xrange;while
查看>>
hadoop的节点间的通信
查看>>
HashMap
查看>>
mysql 主从 重新同步
查看>>