Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3823914

Browse files
refactor 131
1 parentd7e00b5 commit3823914

File tree

1 file changed

+39
-55
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+39
-55
lines changed

‎src/main/java/com/fishercoder/solutions/_131.java

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,49 @@
33
importjava.util.ArrayList;
44
importjava.util.List;
55

6-
/**
7-
* 131. Palindrome Partitioning
8-
9-
Given a string s, partition s such that every substring of the partition is a palindrome.
10-
11-
Return all possible palindrome partitioning of s.
12-
13-
For example, given s = "aab",
14-
Return
15-
16-
[
17-
["aa","b"],
18-
["a","a","b"]
19-
]
20-
21-
*/
226
publicclass_131 {
237

24-
publicstaticclassSolution1 {
25-
publicList<List<String>>partition(Strings) {
26-
List<List<String>>result =newArrayList();
27-
intn =s.length();
28-
boolean[][]dp =newboolean[n][n];
29-
for (inti =0;i <n;i++) {
30-
for (intj =0;j <=i;j++) {
31-
if (s.charAt(j) ==s.charAt(i) && (j +1 >=i -1 ||dp[j +1][i -1])) {
32-
// j+1 >= i-1 means j and i are adjance to each other or only one char apart from each other
33-
//dp[j+1][i-1] means its inner substring is a palindrome, so as long as s.charAt(j) == s.charAt(i), then dp[j][i] must be a palindrome.
34-
dp[j][i] =true;
35-
}
8+
publicstaticclassSolution1 {
9+
publicList<List<String>>partition(Strings) {
10+
List<List<String>>result =newArrayList();
11+
intn =s.length();
12+
boolean[][]dp =newboolean[n][n];
13+
for (inti =0;i <n;i++) {
14+
for (intj =0;j <=i;j++) {
15+
if (s.charAt(j) ==s.charAt(i) && (j +1 >=i -1 ||dp[j +1][i -1])) {
16+
// j+1 >= i-1 means j and i are adjance to each other or only one char apart from each other
17+
//dp[j+1][i-1] means its inner substring is a palindrome, so as long as s.charAt(j) == s.charAt(i), then dp[j][i] must be a palindrome.
18+
dp[j][i] =true;
19+
}
20+
}
21+
}
22+
23+
for (boolean[]list :dp) {
24+
for (booleanb :list) {
25+
System.out.print(b +", ");
26+
}
27+
System.out.println();
28+
}
29+
System.out.println();
30+
31+
backtracking(s,0,dp,newArrayList(),result);
32+
33+
returnresult;
3634
}
37-
}
38-
39-
for (boolean[]list :dp) {
40-
for (booleanb :list) {
41-
System.out.print(b +", ");
42-
}
43-
System.out.println();
44-
}
45-
System.out.println();
46-
47-
backtracking(s,0,dp,newArrayList(),result);
48-
49-
returnresult;
50-
}
5135

52-
voidbacktracking(Strings,intstart,boolean[][]dp,List<String>temp,
53-
List<List<String>>result) {
54-
if (start ==s.length()) {
55-
List<String>newTemp =newArrayList(temp);
56-
result.add(newTemp);
57-
}
58-
for (inti =start;i <s.length();i++) {
59-
if (dp[start][i]) {
60-
temp.add(s.substring(start,i +1));
61-
backtracking(s,i +1,dp,temp,result);
62-
temp.remove(temp.size() -1);
36+
voidbacktracking(Strings,intstart,boolean[][]dp,List<String>temp,
37+
List<List<String>>result) {
38+
if (start ==s.length()) {
39+
List<String>newTemp =newArrayList(temp);
40+
result.add(newTemp);
41+
}
42+
for (inti =start;i <s.length();i++) {
43+
if (dp[start][i]) {
44+
temp.add(s.substring(start,i +1));
45+
backtracking(s,i +1,dp,temp,result);
46+
temp.remove(temp.size() -1);
47+
}
48+
}
6349
}
64-
}
6550
}
66-
}
6751
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp