ARTS

Algorithm: 每周至少做一道LeetCode算法题
Review: 阅读并点评至少一篇英文技术文章
Tip: 学习至少一个技术技巧
Share: 分享一篇有观点和思考的技术文章

Algorithm

完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

1
2
3
输入: n = 12
输出: 3
解释: 12 = 4 + 4 + 4.

示例 2:

1
2
3
输入: n = 13
输出: 2
解释: 13 = 4 + 9.

使用队列和广度优先搜索(BFS)

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 class NumSquares {
private class Node {
int val;
int step;

public Node(int val, int step) {
this.val = val;
this.step = step;
}
}

public int numSquares(int n) {
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(n, 1));
boolean record[] = new boolean[n];
while (!queue.isEmpty()) {
int val = queue.peek().val;
int step = queue.peek().step;
queue.remove();
// 每一层的广度遍历
for (int i = 1; ; i++) {
int nextVal = val - i * i;
// 说明已到最大平方数
if (nextVal < 0)
break;

// 由于是广度遍历,所以当遍历到0时,肯定是最短路径
if (nextVal == 0)
return step;

// 当再次出现时没有必要加入,因为在该节点的路径长度肯定不小于第一次出现的路径长
if (!record[nextVal]) {
queue.add(new Node(nextVal, step + 1));
record[nextVal] = true;
}
}
}
return -1;
}

}

Review

How does a HashMap work in JAVA

总结

  有点难懂,还需要多次阅读。

Tips

java程序启动参数-D是用来做什么的

去查询了一下官方解释:

Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:

java -Dfoo=”some string” SomeClass
也就是说-D是用来在启动一个java程序时设置系统属性值的。如果该值是一个字符串且包含空格,那么需要包在一对双引号中。

何为系统属性值呢?也就是在System类中通过getProperties()得到的一串系统属性。

如果是在tomcat里使用,则需要在bin/catalina.sh 里加入一条 JAVA_OPTS= “-Dfoo=some thing”

Share

开源的13个Spring Boot 优秀学习项目!超53K星,一网打尽!