②
依山傍水房树间,行也安然,住也安然;一条耕牛半顷田,收也凭天,荒也凭天;雨过天晴骂小船,鱼在一边,酒在一边;夜晚妻子话灯前,今也谈谈,古也谈谈;日上三竿犹在眠,不是神仙,胜似神仙。
2022年6月13日
男人一辈子不结婚会怎么样? - 知乎 (zhihu.com)
739. 每日温度
150. 逆波兰表达式求值
2022年6月14日
DFS - 深度优先搜索(Depth First Search)
DFS 用于查找从根结点到目标结点的路径,DFS 代码模板如下:
/*
* Return true if there is a path from cur to target.
*/
boolean DFS(Node cur, Node target, Set<Node> visited) {
return true if cur is target;
for (next : each neighbor of cur) {
if (next is not in visited) {
add next to visted;
return true if DFS(next, target, visited) == true;
}
}
return false;
}
递归解决方案存在一个很大的缺点:如果递归的深度太高,将遭受堆栈溢出。 在这种情况下,可以使用使用显式栈实现 DFS 。
/*
* Return true if there is a path from cur to target.
*/
boolean DFS(int root, int target) {
Set<Node> visited;
Stack<Node> s;
add root to s;
while (s is not empty) {
Node cur = the top element in s;
return true if cur is target;
for (Node next : the neighbors of cur) {
if (next is not in visited) {
add next to s;
add next to visited;
}
}
remove cur from s;
}
return false;
}