Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Shadow-Java
algorithm-journey
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm-journey
/
src
/
class067
/
Code02_WordSearch.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
46 lines (42 loc) · 1.44 KB
main
Breadcrumbs
algorithm-journey
/
src
/
class067
/
Code02_WordSearch.java
Copy path
Top
File metadata and controls
Code
Blame
46 lines (42 loc) · 1.44 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
package
class067
;
// 单词搜索(无法改成动态规划)
// 给定一个 m x n 二维字符网格 board 和一个字符串单词 word
// 如果 word 存在于网格中,返回 true ;否则,返回 false 。
// 单词必须按照字母顺序,通过相邻的单元格内的字母构成
// 其中"相邻"单元格是那些水平相邻或垂直相邻的单元格
// 同一个单元格内的字母不允许被重复使用
// 测试链接 : https://leetcode.cn/problems/word-search/
public
class
Code02_WordSearch
{
public
static
boolean
exist
(
char
[][]
board
,
String
word
) {
char
[]
w
=
word
.
toCharArray
();
for
(
int
i
=
0
;
i
<
board
.
length
;
i
++) {
for
(
int
j
=
0
;
j
<
board
[
0
].
length
;
j
++) {
if
(
f
(
board
,
i
,
j
,
w
,
0
)) {
return
true
;
}
}
}
return
false
;
}
// 因为board会改其中的字符
// 用来标记哪些字符无法再用
// 带路径的递归无法改成动态规划或者说没必要
// 从(i,j)出发,来到w[k],请问后续能不能把word走出来w[k...]
public
static
boolean
f
(
char
[][]
b
,
int
i
,
int
j
,
char
[]
w
,
int
k
) {
if
(
k
==
w
.
length
) {
return
true
;
}
if
(
i
<
0
||
i
==
b
.
length
||
j
<
0
||
j
==
b
[
0
].
length
||
b
[
i
][
j
] !=
w
[
k
]) {
return
false
;
}
// 不越界,b[i][j] == w[k]
char
tmp
=
b
[
i
][
j
];
b
[
i
][
j
] =
0
;
boolean
ans
=
f
(
b
,
i
-
1
,
j
,
w
,
k
+
1
)
||
f
(
b
,
i
+
1
,
j
,
w
,
k
+
1
)
||
f
(
b
,
i
,
j
-
1
,
w
,
k
+
1
)
||
f
(
b
,
i
,
j
+
1
,
w
,
k
+
1
);
b
[
i
][
j
] =
tmp
;
return
ans
;
}
}
You can’t perform that action at this time.