FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_04/id_108/LeetCode_720_108.java at master · augfool/algorithm · GitHub
augfool
/
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_108
/
LeetCode_720_108.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
113 lines (96 loc) · 3.26 KB
Breadcrumbs
algorithm
/
Week_04
/
id_108
/
LeetCode_720_108.java
Copy path
File metadata and controls
113 lines (96 loc) · 3.26 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import
java
.
util
.
Arrays
;
import
java
.
util
.
HashSet
;
import
java
.
util
.
Set
;
/**
* @author zhangruihao.zhang
* @version v1.0.0
* @since 2019/05/12
*/
public
class
LeetCode_720_108
{
class
Solution1
{
// 方法一:比较次数较多
public
String
longestWord
(
String
[]
words
) {
Arrays
.
sort
(
words
);
Set
<
String
>
set
=
new
HashSet
<>();
String
longest
=
""
;
for
(
String
word
:
words
) {
if
(
word
.
length
() ==
1
||
set
.
contains
(
word
.
substring
(
0
,
word
.
length
() -
1
))) {
set
.
add
(
word
);
if
(
word
.
length
() >
longest
.
length
()) {
longest
=
word
;
}
}
}
return
longest
;
}
}
class
Solution2
{
//方法二:trie树,比较耗内存
private
TreeNode
root
;
private
char
startChar
=
'a'
;
private
String
longest
=
""
;
public
String
longestWord
(
String
[]
words
) {
for
(
String
word
:
words
) {
insert
(
word
);
}
findLongest
(
root
,
""
);
return
longest
;
}
private
void
insert
(
String
word
) {
if
(
root
==
null
) {
root
=
new
TreeNode
();
}
TreeNode
tmp
=
root
;
for
(
int
i
=
0
;
i
<
word
.
length
();
i
++) {
int
index
=
word
.
charAt
(
i
) -
startChar
;
TreeNode
child
=
tmp
.
getChildren
()[
index
];
if
(
child
==
null
) {
TreeNode
node
=
new
TreeNode
();
node
.
setTimes
(
1
).
setWord
(
i
==
word
.
length
() -
1
);
tmp
.
getChildren
()[
index
] =
node
;
}
else
{
boolean
isWord
=
child
.
isWord
;
child
.
setTimes
(
child
.
getTimes
() +
1
).
setWord
(
isWord
||
i
==
word
.
length
() -
1
);
}
tmp
=
tmp
.
getChildren
()[
index
];
}
}
private
void
findLongest
(
TreeNode
node
,
String
word
) {
for
(
int
index
=
0
;
index
<
26
;
index
++) {
if
(
node
.
getChildren
()[
index
] !=
null
&&
node
.
getChildren
()[
index
].
isWord
) {
String
newWord
=
word
+ (
char
) (
'a'
+
index
);
if
(
newWord
.
length
() >
longest
.
length
()) {
longest
=
newWord
;
}
findLongest
(
node
.
getChildren
()[
index
],
newWord
);
}
}
}
private
class
TreeNode
{
private
boolean
isWord
;
private
TreeNode
[]
children
=
new
TreeNode
[
26
];
private
int
times
;
public
boolean
isWord
() {
return
isWord
;
}
public
TreeNode
setWord
(
boolean
word
) {
isWord
=
word
;
return
this
;
}
public
TreeNode
[]
getChildren
() {
return
children
;
}
public
TreeNode
setChildren
(
TreeNode
[]
children
) {
this
.
children
=
children
;
return
this
;
}
public
int
getTimes
() {
return
times
;
}
public
TreeNode
setTimes
(
int
times
) {
this
.
times
=
times
;
return
this
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL