FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_04/id_134/LeetCode_720_134.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_04
/
id_134
/
LeetCode_720_134.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (70 loc) · 2.06 KB
Breadcrumbs
algorithm
/
Week_04
/
id_134
/
LeetCode_720_134.java
Copy path
File metadata and controls
73 lines (70 loc) · 2.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class
Solution
{
TrieNode
root
=
new
TrieNode
();
StringBuilder
curWord
=
new
StringBuilder
();
String
ret
=
null
;
public
String
longestWord
(
String
[]
words
) {
if
(
words
==
null
)
return
null
;
for
(
String
word
:
words
) {
insert
(
word
);
}
traverse
(
root
);
return
ret
;
}
public
void
traverse
(
TrieNode
root
) {
if
(
root
.
isLeaf
()) {
if
(
curWord
!=
null
) {
if
(
ret
==
null
||
ret
.
length
() <
curWord
.
length
() ||
(
ret
.
length
() ==
curWord
.
length
() &&
ret
.
compareTo
(
curWord
.
toString
()) >
0
)) {
ret
=
curWord
.
toString
();
}
}
return
;
}
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
root
.
sons
[
i
] !=
null
&&
root
.
sons
[
i
].
isEnd
) {
curWord
.
append
(
root
.
sons
[
i
].
val
);
traverse
(
root
.
sons
[
i
]);
curWord
.
deleteCharAt
(
curWord
.
length
()-
1
);
}
}
}
public
void
insert
(
String
word
) {
TrieNode
node
=
root
;
for
(
int
i
=
0
;
i
<
word
.
length
();
i
++) {
char
ch
=
word
.
charAt
(
i
);
int
pos
=
ch
-
'a'
;
if
(
node
.
sons
[
pos
] ==
null
) {
node
.
sons
[
pos
] =
new
TrieNode
(
ch
);
}
node
=
node
.
sons
[
pos
];
}
node
.
isEnd
=
true
;
}
class
TrieNode
{
char
val
;
boolean
isEnd
;
TrieNode
[]
sons
;
public
TrieNode
() {
isEnd
=
false
;
sons
=
new
TrieNode
[
26
];
}
public
TrieNode
(
char
val
) {
this
.
val
=
val
;
isEnd
=
false
;
sons
=
new
TrieNode
[
26
];
}
public
boolean
isLeaf
() {
boolean
leafFlag
=
true
,
endFlag
=
true
;
for
(
int
i
=
0
;
i
<
sons
.
length
;
i
++) {
if
(
sons
[
i
] !=
null
) {
leafFlag
=
false
;
if
(
sons
[
i
].
isEnd
) {
endFlag
=
false
;
}
}
}
return
leafFlag
||
endFlag
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL