FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_04/id_30/LeetCode_720_30.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_30
/
LeetCode_720_30.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (68 loc) · 2.17 KB
Breadcrumbs
algorithm
/
Week_04
/
id_30
/
LeetCode_720_30.java
Copy path
File metadata and controls
82 lines (68 loc) · 2.17 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
74
75
76
77
78
79
80
81
82
package
com
.
shufeng
.
algorithm4
.
demo
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
/**
* @author gsf
* @date 2019-05-09 14:16
*/
public
class
LeetCode_720_30
{
public
static
void
main
(
String
[]
args
) {
// String[] words = {"w", "wo", "wor", "worl", "world"};
// String[] words = {"a", "banana", "app", "appl", "ap", "apply", "apple"};
String
[]
words
= {
"b"
,
"br"
,
"bre"
,
"brea"
,
"break"
,
"breakf"
,
"breakfa"
,
"breakfas"
,
"breakfast"
,
"l"
,
"lu"
,
"lun"
,
"lunc"
,
"lunch"
,
"d"
,
"di"
,
"din"
,
"dinn"
,
"dinne"
,
"dinner"
};
String
s
=
longestWord
(
words
);
System
.
out
.
println
(
s
);
}
public
static
String
longestWord
(
String
[]
words
) {
Node
node
=
new
Node
(
true
);
for
(
int
i
=
0
;
i
<
words
.
length
;
i
++) {
add
(
node
,
words
[
i
]);
}
String
str
=
""
;
for
(
String
word
:
words
) {
if
(
find
(
node
,
word
) && (
word
.
length
() >
str
.
length
() ||
word
.
length
() ==
str
.
length
() &&
word
.
compareTo
(
str
) <
0
)) {
str
=
word
;
}
}
return
str
;
}
private
static
class
Node
{
// 是单词
private
boolean
isword
;
// 每个字符有多个子字符
private
Map
<
Character
,
Node
>
next
;
public
Node
(
boolean
isword
) {
this
.
isword
=
isword
;
this
.
next
=
new
HashMap
<>();
}
public
Node
() {
this
(
false
);
}
}
/**
* 查询字符串是否每加个字符都有
*/
public
static
boolean
find
(
Node
node
,
String
word
) {
for
(
int
i
=
0
;
i
<
word
.
length
();
i
++) {
char
c
=
word
.
charAt
(
i
);
if
(
node
.
next
.
get
(
c
) ==
null
|| !
node
.
isword
) {
return
false
;
}
node
=
node
.
next
.
get
(
c
);
}
return
true
;
}
public
static
void
add
(
Node
node
,
String
word
) {
for
(
int
i
=
0
;
i
<
word
.
length
();
i
++) {
char
c
=
word
.
charAt
(
i
);
if
(
node
.
next
.
get
(
c
) ==
null
) {
node
.
next
.
put
(
c
,
new
Node
());
}
node
=
node
.
next
.
get
(
c
);
}
if
(!
node
.
isword
) {
node
.
isword
=
true
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL