颜林林的“左耳听风ARTS”打卡记录

ARTS第三十三周(2020年5月4日~10日)

2020-05-04

Algorithm

LeetCode题库

编号 难度 题目 我的解答 执行用时 内存消耗 排名 备注
230 中等 二叉搜索树中第K小的元素 200504-1.cpp 32 ms 24 MB 43.57%
231 简单 2的幂 200504-1.cpp 0 ms 5.7 MB 100.00%
232 简单 用栈实现队列 200510-1.cpp 0 ms 6.9 MB 100.00%

Review

1. C++中实现“else-before-if”

分享链接:Else Before If

这篇文章介绍了C++语法中Lambda表达式的一个应用示例。

通常我们写多重条件判断时,采取如下形式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if (edgeCase1)
{
    // deal with edge case 1
}
else if (edgeCase2)
{
    // deal with edge case 2
}
else
{
    // this is the main case
}

而另一种(可能)更清晰的理解方式,可以采取类似如下的形式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
normally
{
    // this is the main case
}
unless (edgeCase1)
{
    // deal with edge case 1
}
unless (edgeCase2)
{
    // deal with edge case 2
}

这种似乎需要语言底层语法支持才能实现的功能,在现代C++里其实可以通过用户自定义一些类和模板来实现,这正是现代C++语言的灵活美妙之处。通过Lambda表达式,能够让整个语句块作为参数传递给某个类成员函数(或是模板函数),从而实现在运行时才根据具体数值确定是否执行的“延迟计算”或“动态计算”。

该实现的完整代码见:http://coliru.stacked-crooked.com/a/100db35a6b2800fc

Tip

1. Linux下如何重复执行命令直至成功

分享链接:How to repeat a Linux command until it succeeds

可以使用whileuntil两个命令来构造循环:

1
2
3
4
5
$ while ! cat missingfile
do
    echo waiting for missingfile
    sleep 10
done
1
2
3
4
5
$ until cat missingfile
do
    echo waiting for missingfile
    sleep 10
done

Share

1. COBOL语言课程

分享链接:

COBOL是一门古老的计算机语言,而且在很多大公司里被使用。没想到,时至今日,还有这样一门课程,教授该语言。

2. 将照片处理成为3D效果

分享链接:

人工智能在处理图像上,做出来的神奇效果。