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

Commit2a6e557

Browse files
committed
refine the code
1 parent949dd89 commit2a6e557

File tree

1 file changed

+46
-58
lines changed

1 file changed

+46
-58
lines changed

‎word-break-ii/Solution.java

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,67 @@
11
publicclassSolution {
22

3-
finalintSTACK_SIZE =10;
4-
5-
String[]stack;
6-
ArrayList<String>found;
7-
8-
voidpushWord(intp,Stringword){
9-
while(p >=stack.length){
10-
stack =Arrays.copyOf(stack,stack.length +STACK_SIZE);
3+
Stringjoin(List<String>list){
4+
if(list.isEmpty())return"";
5+
6+
StringBuilders =newStringBuilder(list.get(0));
7+
8+
for(inti =1;i <list.size();i++){
9+
s.append(' ');
10+
s.append(list.get(i));
1111
}
12-
13-
stack[p] =word;
12+
13+
returns.toString();
1414
}
1515

16-
voidwordBreak(Strings,Set<String>dict,finalintp){
17-
if("".equals(s) ||s ==null){
18-
19-
if (p >0){
20-
Stringjoin =stack[0];
21-
for(inti =1;i <p ;i++){
22-
join +=" " +stack[i];
23-
}
24-
25-
found.add(join);
26-
}
16+
ArrayList<Integer>[]P;
17+
char[]S;
18+
ArrayList<String>rt;
19+
20+
voidjoinAll(intoffset,LinkedList<String>parents){
21+
22+
if(P[offset].isEmpty()){
2723

24+
rt.add(join(parents));
2825
return;
2926
}
3027

31-
for(Stringd :dict){
32-
if(s.startsWith(d)){
33-
34-
intl =d.length();
35-
pushWord(p,d);
36-
37-
wordBreak(s.substring(l),dict,p +1);
38-
}
28+
for(Integerp :P[offset]){
29+
30+
parents.push(newString(S,p,offset -p));
31+
32+
joinAll(p,parents);
33+
34+
parents.pop();
3935
}
4036

4137
}
42-
43-
boolean_wordBreak(Strings,Set<String>dict) {
44-
45-
char[]S =s.toCharArray();
46-
47-
boolean[]P =newboolean[S.length +1];
48-
P[0] =true;
49-
38+
39+
publicList<String>wordBreak(Strings,Set<String>dict) {
40+
S =s.toCharArray();
41+
42+
P =newArrayList[S.length +1];
43+
P[0] =newArrayList<Integer>();
44+
5045
for(inti =0;i <S.length;i++){
51-
5246
for(intj =0;j <=i;j++){
53-
if(P[j] &&dict.contains(newString(S,j,i -j +1))){
54-
P[i +1] =true;
55-
continue;
47+
Stringw =newString(S,j,i -j +1);
48+
if(P[j] !=null &&dict.contains(w)){
49+
50+
if(P[i +1] ==null){
51+
P[i +1] =newArrayList<Integer>();
52+
}
53+
54+
P[i +1].add(j);
5655
}
5756
}
5857
}
58+
59+
rt =newArrayList<String>();
5960

60-
returnP[S.length];
61-
62-
63-
}
64-
65-
publicArrayList<String>wordBreak(Strings,Set<String>dict) {
66-
// Note: The Solution object is instantiated only once and is reused by each test case.
67-
68-
found =newArrayList<String>();
69-
70-
if(_wordBreak(s,dict)){
71-
if(dict.size() <=0)returnfound;
72-
stack =newString[STACK_SIZE];
73-
74-
wordBreak(s,dict,0);
61+
if(P[S.length] !=null){
62+
joinAll(S.length,newLinkedList<String>());
7563
}
7664

77-
returnfound;
65+
returnrt;
7866
}
79-
}
67+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp