ARTS 第62周

ARTS (第62周)

不要高估自己一年内的成果,也不要低估十年后的自己。

Algorithm 算法

矩阵中的路径

1
2
3
题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
https://www.nowcoder.com/practice/c61c6999eecb4b8f88a98f66b273a3cc?tpId=13&&tqId=11218&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

DFS 解法 遍历所有路径

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
47
48
49
50
51
package test;

public class Solution {



public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
if(matrix==null || rows<=0 || cols<=0 || str==null){
return false;
}
boolean[] rem = new boolean[matrix.length];
for(int i = 0; i < rows; i ++){
for(int j = 0; j < cols; j ++){
// (i,j)是起始位置,每个位置当作起始位置跑一遍
if(hasPath_helper(matrix, rows, cols, i, j, str, 0, rem)){
return true;
}
}
}
return false;
}
public static boolean hasPath_helper(char[] matrix, int rows, int cols, int i, int j, char[] str, int k, boolean[] rem){
int idx = i * cols + j;
//边界判断
if(i < 0 || i >= rows || j < 0 || j >= cols) {
return false;
}
//值判断
if( matrix[idx] != str[k] || rem[idx]){
return false;
}
//找到
if(k == str.length - 1){
return true;
}
//把当前点算已遍历过
rem[idx] = true;
if(hasPath_helper(matrix, rows, cols, i - 1, j, str, k + 1, rem)
||hasPath_helper(matrix, rows, cols, i + 1, j, str, k + 1, rem)
||hasPath_helper(matrix, rows, cols, i, j - 1, str, k + 1, rem)
||hasPath_helper(matrix, rows, cols, i , j + 1, str, k + 1, rem)){
return true;
}
//当前节点走不通 将状态记录取消 让其它节点遍历
rem[idx] = false;
return false;
}


}

机器人的运动范围

1
2
3
题目描述
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&&tqId=11219&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

解法 穷举+备忘录

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
public int movingCount(int threshold, int rows, int cols) {
if (rows <= 0 || cols <= 0 || threshold < 0) return 0;
boolean[] rem = new boolean[rows * cols];
return movingCount_help(threshold, rows, cols, 0, 0, rem);
}

private int movingCount_help(int threshold, int rows, int cols, int i, int j, boolean[] rem) {
int count = 0;
if (movingCount_isValid(threshold, rows, cols, i, j, rem)) {
rem[i * cols + j] = true;
count += 1;//当前
count += movingCount_help(threshold, rows, cols, i - 1, j, rem); //往上
count += movingCount_help(threshold, rows, cols, i + 1, j, rem);//往下
count += movingCount_help(threshold, rows, cols, i, j - 1, rem); //往左
count += movingCount_help(threshold, rows, cols, i, j + 1, rem); //往右
}
return count;
}

private boolean movingCount_isValid(int threshold, int rows, int cols, int i, int j, boolean[] rem) {
int idx = i * cols + j;
//边界判断
if(i < 0 || i >= rows || j < 0 || j >= cols) {
return false;
}
//是否记录过
if( rem[idx]){
return false;
}
//位数
if ( movingCount_getSumOfDigits(i) + movingCount_getSumOfDigits(j) > threshold)
return false;
return true;
}

private int movingCount_getSumOfDigits(int number) {
int sum = 0;
while (number != 0) {
sum += number % 10;
number /= 10;
}
return sum;
}

Review 英文文章

https://spring.io/guides/gs/sts/ 用sts创建spring项目

Tip 技巧

code review-代码质量方面

我们可以从以下几个方面来审视代码。

1、目录设置是否合理、模块划分是否清晰、代码结构是否满足“高内聚、松耦合”?

2、是否遵循经典的设计原则和设计思想(SOLID、DRY、KISS、YAGNI、LOD 等)?

3、设计模式是否应用得当?是否有过度设计?代码是否容易扩展?如果要添加新功能,是否容易实现?

4、代码是否可以复用?是否可以复用已有的项目代码或类库?是否有重复造轮子?

5、代码是否容易测试?单元测试是否全面覆盖了各种正常和异常的情况?

6、代码是否易读?是否符合编码规范(比如命名和注释是否恰当、代码风格是否一致等)?

code review-业务方面

1、代码是否实现了预期的业务需求?

2、逻辑是否正确?是否处理了各种异常情况?

3、日志打印是否得当?是否方便 debug 排查问题?

4、接口是否易用?是否支持幂等、事务等?

5、代码是否存在并发问题?是否线程安全?

6、性能是否有优化空间,比如,SQL、算法是否可以优化?

7、是否有安全漏洞?比如输入输出校验是否全面?

动态规划

一般,动态规划有以下几种分类:

最值型动态规划,比如求最大,最小值是多少
计数型动态规划,比如换硬币,有多少种换法
坐标型动态规划,比如在m*n矩阵求最值型,计数型,一般是二维矩阵
区间型动态规划,比如在区间中求最值
其实,根据此题的启发,我们可以换种想法,就是什么样的题适合用暴力递归?
显然就是,可能的情况很多,需要枚举所有种情况。只不过动态规划,只记录子结构中最优的解。

链接:https://www.nowcoder.com/questionTerminal/57d85990ba5b440ab888fc72b0751bf8?answerType=1&f=discussion
来源:牛客网

Share 分享

https://blog.csdn.net/u011635492/article/details/81058153 高并发下接口幂等性解决方案

https://www.mooc.cn/ 大学MOOC

https://www.icourse163.org/ 大学MOOC

https://www.jianshu.com/p/b9384fc7b04b 微信小程序登录时序图:授权与 Oauth2.0

https://blog.csdn.net/qing_gee/article/details/105086259 优秀网站汇总