Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
yaozc
ProjectLeet
Commits
126f2299
Commit
126f2299
authored
Jul 12, 2019
by
yaozc
Browse files
keep going!!!
parent
088b5c22
Changes
1
Hide whitespace changes
Inline
Side-by-side
amazon.cpp
View file @
126f2299
...
...
@@ -615,6 +615,11 @@ public:
};
Word
Ladder
// 本质为BFS广度搜索,BFS一定会给出最短路径,使用一个queue
// 关于BFS和DFS的区别以及图解:https://cuijiahua.com/blog/2018/01/alogrithm_10.html
class
Solution
{
public:
int
ladderLength
(
string
beginWord
,
string
endWord
,
vector
<
string
>&
wordList
)
{
...
...
@@ -650,6 +655,102 @@ public:
Word
Ladder
II
class
Solution
{
public:
vector
<
vector
<
string
>>
findLadders
(
string
beginWord
,
string
endWord
,
vector
<
string
>&
wordList
)
{
}
};
Number
of
Islands
//牢记 DFS 的用途!!!
//每次修改一个点,附近相关的点都推进stack中,因为DFS是不撞南墙不回头的!!!所以特别适合这种找周围符合条件的node的问题!!!
class
Solution
{
public:
int
numIslands
(
vector
<
vector
<
char
>>
&
grid
)
{
int
islands
=
0
;
for
(
int
outer
=
0
;
outer
<
grid
.
size
();
outer
++
)
{
for
(
int
inner
=
0
;
inner
<
grid
[
outer
].
size
();
inner
++
)
{
if
(
grid
[
outer
][
inner
]
==
'1'
)
{
islands
++
;
DFS
(
grid
,
outer
,
inner
);
}
}
}
return
islands
;
}
void
DFS
(
vector
<
vector
<
char
>>
&
grid
,
int
horizontal
,
int
vertical
)
{
stack
<
pair
<
int
,
int
>>
s
;
s
.
push
(
std
::
make_pair
(
horizontal
,
vertical
));
while
(
!
s
.
empty
())
{
pair
<
int
,
int
>
temp
=
s
.
top
();
s
.
pop
();
grid
[
temp
.
first
][
temp
.
second
]
=
'0'
;
if
(
temp
.
second
-
1
>=
0
&&
grid
[
temp
.
first
][
temp
.
second
-
1
]
==
'1'
)
s
.
push
(
std
::
make_pair
(
temp
.
first
,
temp
.
second
-
1
));
if
(
temp
.
second
+
1
<
grid
[
0
].
size
()
&&
grid
[
temp
.
first
][
temp
.
second
+
1
]
==
'1'
)
s
.
push
(
std
::
make_pair
(
temp
.
first
,
temp
.
second
+
1
));
if
(
temp
.
first
-
1
>=
0
&&
grid
[
temp
.
first
-
1
][
temp
.
second
]
==
'1'
)
s
.
push
(
std
::
make_pair
(
temp
.
first
-
1
,
temp
.
second
));
if
(
temp
.
first
+
1
<
grid
.
size
()
&&
grid
[
temp
.
first
+
1
][
temp
.
second
]
==
'1'
)
s
.
push
(
std
::
make_pair
(
temp
.
first
+
1
,
temp
.
second
));
}
}
};
Course
Schedule
// 本质为directed graph的寻找cycle问题
//在这个问题中,使用DFS 应用到 Kahn’s algorithm for Topological Sorting可以寻找graph是否有cycle
// DFS问题不一定要使用stack,很多时候用recursion来解决DFS
// 此题的逻辑为:一个vector (visited),一个stack(visiting),若一个新的点在这两个容器中出现过(先前被访问过),那么此graph有cycle
//因为stack不利于查找,所以答案中用了set替代stack来做visiting。将DFS写成rucursion即可
class
Solution
{
public:
bool
canFinish
(
int
numCourses
,
vector
<
vector
<
int
>>&
prerequisites
)
{
vector
<
bool
>
vect
(
numCourse
,
true
);
}
};
//
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment