| 標(biāo)題: |
Palindrome Partitioning |
| 通過率: | 26.3% |
| 難度: | 中等 |
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"]
]
本題還是一個(gè)遞歸的過程,只是再遞歸的時(shí)候要判斷是否為回文函數(shù),然后就是字符串的截取,具體看代碼
1
public
class
Solution {
2
public
ArrayList<ArrayList<String>>
partition(String s) {
3
ArrayList<ArrayList<String>> res=
new
ArrayList<ArrayList<String>>
();
4
ArrayList<String> tmp=
new
ArrayList<String>
();
5
getpa(res,tmp,s);
6
return
res;
7
}
8
public
void
getpa(ArrayList<ArrayList<String>> res,ArrayList<String>
tmp,String s){
9
if
(s.length()==0||s==
null
){
10
res.add(
new
ArrayList<String>
(tmp));
11
}
12
else
{
13
for
(
int
i=1;i<=s.length();i++
){
14
if
(ispa(s.substring(0
,i))){
15
tmp.add(s.substring(0
,i));
16
getpa(res,tmp,s.substring(i));
17
tmp.remove(tmp.size()-1
);
18
}
19
}
20
}
21
}
22
public
boolean
ispa(String s){
23
for
(
int
i=0,j=s.length()-1;i<j;i++,j--
){
24
if
(s.charAt(i)!=
s.charAt(j))
25
return
false
;
26
}
27
return
true
;
28
}
29
}
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

