Skip to content

Commit e086afc

Browse files
committed
提交作业
1 parent 0fd6bbe commit e086afc

3 files changed

Lines changed: 171 additions & 1 deletion

File tree

Week01/NOTE.md

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,116 @@
1-
学习笔记
1+
## 学习笔记
2+
3+
### 五毒神掌刷题法
4+
1. 刷题第一遍:
5+
- 五分钟:读题 + 思考
6+
- 直接看解法:多解法要比较解法优劣
7+
2. 刷题第二遍
8+
- 马上自己写,LeetCode提交
9+
- 多解法比较、体会 -> 优化
10+
3. 刷题第三遍
11+
- 过了一天后,再重复做题
12+
4. 刷题第四遍
13+
- 过了一周:反复回来练习相同的题目,不熟练的题目反复练习
14+
5. 面试前一周恢复训练
15+
16+
### 数组
17+
18+
#### 特点
19+
1. 内存地址连续
20+
2. 访问时间复杂度 O(1)
21+
3. 插入中间或删除中间需要位移其他元素,修改的时间复杂度为 O(n)
22+
4. 前插 O(1),可采用申请稍大内存空间,数组最开始的地方预留的方式降低复杂度至 O(1),正常是 O(n)
23+
5. 后插 O(1)
24+
25+
### 链表
26+
27+
#### 特点
28+
1. 内存地址不连续
29+
2. 访问时间复杂度 O(n)
30+
3. 插入或删除中间元素的时间复杂度 O(1)
31+
4. 前插(头插)O(1)
32+
5. 后插(尾插)O(1)
33+
34+
#### 标准实现
35+
```java
36+
class LinkedList {
37+
Node head; // head of list
38+
39+
/* Linked list node */
40+
class Node {
41+
int data;
42+
Node next;
43+
44+
// Constructor to create a new node
45+
// Next is by default initialized
46+
// as null
47+
Node(int d) {
48+
data = d;
49+
}
50+
}
51+
}
52+
```
53+
54+
#### 链表插入元素的过程
55+
1. 插入节点的 next 指针指向前驱节点的 next;
56+
2. 前驱节点的 next 指针指向插入节点;
57+
58+
59+
java 中的 LinkedList (双向链表)
60+
61+
62+
63+
64+
### 跳表
65+
66+
#### 特点
67+
1. 只能用于元素有序的情况;
68+
2. 插入/搜索/删除复杂度都是O(log n)
69+
3. 空间复杂度为 O(n),因为累加值收敛
70+
71+
##### 对比平衡树
72+
73+
###### 优势
74+
原理简单,容易实现,方便扩展,效率更高。
75+
76+
###### 劣势
77+
插入删除元素时需要同时更新索引,也是增加删除时间复杂度为 O(log n) 的原因
78+
79+
##### 应用
80+
Redis, LevelDB
81+
82+
##### 实现原理
83+
使用多级索引,通过空间换取时间的方式减少链表查询次数,查询方式类似二分查找。
84+
85+
86+
#### 链接:
87+
- [Java 源码分析(ArrayList)](http://developer.classpath.org/doc/java/util/ArrayList-source.html)
88+
- [Linked List 的标准实现代码](https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/)
89+
- [Linked List 示例代码](http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/code/LinkedList.java)
90+
- [Java 源码分析(LinkedList)](http://developer.classpath.org/doc/java/util/LinkedList-source.html)
91+
- [LRU Cache - Linked list: LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache/)
92+
- [Redis - Skip List:跳跃表](https://redisbook.readthedocs.io/en/latest/internal-datastruct/skiplist.html)[为啥 Redis 使用跳表(Skip List)而不是使用 Red-Black?](https://www.zhihu.com/question/20202931)
93+
94+
95+
96+
###
97+
98+
#### 特点
99+
1. 先入后出
100+
2. 添加、删除皆为 O(1)
101+
3. 查询为 O(n)
102+
103+
104+
### 队列
105+
106+
#### 特点
107+
1. 先入先出
108+
2. 添加、删除皆为 O(1)
109+
3. 查询为 O(n)
110+
111+
112+
### 双端队列
113+
114+
#### 特点
115+
1. 头尾都可以出入
116+
2. 插入和删除都是 O(1)

Week01/加一.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 题目:加一
3+
* 题目地址:https://leetcode-cn.com/problems/plus-one/
4+
* 解法描述:从右往左遍历数组,加一余十存余数,若余数为零,则继续遍历,余数不为零则返回结果。
5+
* 边界值处理:若最后一个值(数组零位)也产生了进位,返回一个长度比原数组大一的新数组,零号位为一,其余填补零。
6+
* 时间复杂度:O(n)
7+
* 空间复杂度:O(n)
8+
* 备注:若产生最高位进位,开辟了一块新的数组内存空间
9+
* 0ms 37.8MB
10+
*/
11+
class Solution2 {
12+
public int[] plusOne(int[] digits) {
13+
int forward = 1;
14+
for (int i = digits.length - 1; i >= 0; i--) {
15+
digits[i]++;
16+
digits[i] = digits[i] % 10;
17+
if (digits[i] != 0) {
18+
return digits;
19+
}
20+
}
21+
digits = new int[digits.length + 1];
22+
digits[0] = 1;
23+
return digits;
24+
}
25+
}

Week01/接雨水.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 题目:接雨水
3+
* 题目地址:https://leetcode-cn.com/problems/trapping-rain-water/submissions/
4+
* 解法描述:利用栈。
5+
* 遍历数组
6+
* 若当前值小于等于栈顶值,则当前索引位入栈
7+
* 若下一个值大于栈顶索引对应的值,则弹出当前栈顶。若此时栈不为空,算出弹出索引位的蓄水量。
8+
* 此时弹出索引位的左边界由弹出后的栈顶索引位对应的值决定,右边界由当前数组遍历的值决定。
9+
* 时间复杂度:O(n)
10+
* 空间复杂度:O(n)
11+
* 3ms 39.4MB
12+
*/
13+
class Solution1 {
14+
public int trap(int[] height) {
15+
int ans = 0, current = 0;
16+
Stack<Integer> st = new Stack<Integer>();
17+
while (current < height.length) {
18+
while (!st.empty() && height[current] > height[st.peek()]) {
19+
int top = st.pop();
20+
if (st.empty())
21+
break;
22+
int distance = current - st.peek() - 1;
23+
int bounded_height = Math.min(height[current], height[st.peek()]) - height[top];
24+
ans += distance * bounded_height;
25+
}
26+
st.push(current++);
27+
}
28+
return ans;
29+
}
30+
}

0 commit comments

Comments
 (0)