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 }}
jayChou112
javascript
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
javascript
/
interview
/
algorithm
/
collection
/
hasStr.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
35 lines (28 loc) · 1.69 KB
master
Breadcrumbs
javascript
/
interview
/
algorithm
/
collection
/
hasStr.js
Copy path
Top
File metadata and controls
Code
Blame
35 lines (28 loc) · 1.69 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
// 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,
// 每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。
// 例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。
// [[“a”,“b”,“c”,“e”],
// [“s”,“f”,“c”,“s”],
// [“a”,“d”,“e”,“e”]]
// 但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。
var
exist
=
function
(
board
,
word
)
{
var
row
=
board
.
length
;
var
col
=
board
[
0
]
.
length
;
var
dfs
=
function
(
i
,
j
,
board
,
word
,
index
)
{
if
(
i
<
0
||
i
>=
row
||
j
<
0
||
j
>
col
||
board
[
i
]
[
j
]
!==
word
[
index
]
)
return
false
;
// 判断不符合条件
if
(
index
===
word
.
length
-
1
)
return
true
;
// word遍历完了
var
tmp
=
board
[
i
]
[
j
]
;
// 记录到board的值
board
[
i
]
[
j
]
=
'-'
// 锁上,因为后续的递归是4个方向上的,无法保证上一个方向的值
var
res
=
dfs
(
i
-
1
,
j
,
board
,
word
,
index
+
1
)
||
dfs
(
i
+
1
,
j
,
board
,
word
,
index
+
1
)
||
dfs
(
i
,
j
-
1
,
board
,
word
,
index
+
1
)
||
dfs
(
i
,
j
+
1
,
board
,
word
,
index
+
1
)
;
board
[
i
]
[
j
]
=
tmp
;
// 恢复现场
return
res
;
}
// 遍历整个board,找到初始位置点
for
(
var
i
=
0
;
i
<
row
;
i
++
)
{
for
(
var
j
=
0
;
j
<
col
;
j
++
)
{
if
(
dfs
(
i
,
j
,
board
,
word
,
0
)
)
return
true
;
}
}
// 没找到
return
false
}
;
You can’t perform that action at this time.