算法学习入门(二)
训练环境的设置,编码技巧和Code Style
电脑配置
- 搜索引擎默认Google
- Terminal
- Mac:iTerm2 +zsh
- Windows:Microsoft new terminal
- LeetCode plugin (vscode & IntelliJ)
- http://vscodethemes.com/
- 骚操作
- https://juejin.im/entry/587e0f2f570c352201113e14
- https://juejin.im/post/5ce1365151882525ff28ed47
Code Style
- Google Code Style
LeetCode
- leetcode-cn.com(国内版)和题
- leetcode.com(国外版)和Discuss border
指法和小操作
- home , end (行头,行尾)
- word单词,选单词,选整行
- IDE的自动补全
- Top tips for
养成使用快捷键的习惯,提高编码效率
自顶向下的编程方式
https://markhneedham.com/blog/2008/09/15/clean-code-book-review/
时间复杂度和空间复杂度
Big O notation
- O(1):常数复杂度
- O(log n):对数复杂度
- O(n):线性时间复杂度
- O(n^2):N 平方
- O(n^3):N 立方
- O(2^n):指数O(n!):阶乘
注意:只看最高复杂度的运算。
O(1):
int n = 100;
System.out.println("hello " + n);
O(1):
int n = 1000;
System.out.println("hello " + n);
System.out.println("hi " + n);
System.out.println("helo " + n);
O(N):
for(int i = 1; i<= n; i++) {
System.out.println("hello " + i);
}
O(N^2):
for (int i = 1; i<=n; i++) {
for (int j = 1; j<=n; j++) {
System.out.println("hello " + i + j);
}
}
O(log(n)):
for (int i = 1; i < n; i = i * 2) {
System.out.println("hello " + i);
}
O(k^n):
int fib(int n) {
if(n <=2 ) return n;
return fib(n - 1) + fib(n - 2);
}
Master Theorem(主定理)

思考题
-
二叉树遍历-前序,中序,后序:时间复杂度是多少?
O(n),n 为节点总数
-
图的遍历:时间复杂度是多少?
O(n),n 为节点总数
-
搜索算法:DFS深度优先算法、BFS广度优先算法时间复杂度是多少?
O(n),n 为节点总数
-
二分查找:时间复杂度是多少?
O(log n)