Skip to content

Commit f85b523

Browse files
committed
update
1 parent 08a7339 commit f85b523

File tree

3 files changed

+99
-64
lines changed

3 files changed

+99
-64
lines changed

MD/Linux.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## 硬链接与软链接的区别
2+
每个文件都是创建了一个指针指向inode(代表物理硬盘的一个区块),可以通过`ls -i`查看。
3+
硬链接是创建另一个文件,也通过创建指针指向inode。当你删除一个文件/硬链接时,它会删除一个到底层inode的指针。当inode的所有指针都被删除时,才会真正删除文件。
4+
软连接是另外一种类型的文件,保存的是它指向文件的全路径,访问时会替换成绝对路径
5+
6+
## 查看某个进程中的线程
7+
`ps -T -p <pid>`
8+
9+
## 查看某个文件夹中每个文件夹的大小
10+
`du --max-depth=1 -h`

MD/在线编程.md

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,69 @@
1818
```
1919

2020
```java
21-
class TreeNode{
21+
import java.util.LinkedList;
22+
23+
class TreeNode {
2224
public TreeNode left;
2325
public TreeNode right;
2426
public int value;
25-
public TreeNode(int value){
27+
28+
public TreeNode(int value) {
2629
this.value = value;
2730
}
28-
31+
2932
public TreeNode invertNode(TreeNode root) {
30-
if(root == null){
33+
if (root == null) {
3134
return null;
3235
}
33-
TreeNode temp = root.left;
34-
root.left = invertNode(root.right);
35-
root.right = invertNode(temp);
36-
return root;
36+
TreeNode temp = root.left;
37+
root.left = invertNode(root.right);
38+
root.right = invertNode(temp);
39+
return root;
40+
}
41+
42+
public void printTreeNode() {
43+
LinkedList<TreeNode> queue = new LinkedList<>();
44+
queue.add(this);
45+
46+
TreeNode currentLineRightestNode = this;
47+
TreeNode nextLineRightestNode = null;
48+
49+
while (!queue.isEmpty()) {
50+
TreeNode currentNode = queue.poll();
51+
52+
if (currentNode.left != null) {
53+
queue.add(currentNode.left);
54+
nextLineRightestNode = currentNode.left;
55+
}
56+
if (currentNode.right != null) {
57+
queue.add(currentNode.right);
58+
nextLineRightestNode = currentNode.right;
59+
}
60+
61+
System.out.print(currentNode.value);
62+
63+
if (currentNode.value == currentLineRightestNode.value) {
64+
System.out.println();
65+
currentLineRightestNode.value = nextLineRightestNode.value;
66+
}
67+
}
68+
}
69+
}
70+
71+
public class ReverseBinaryTree {
72+
public static void main(String[] args) {
73+
TreeNode root = new TreeNode(4);
74+
root.left = new TreeNode(2);
75+
root.right = new TreeNode(7);
76+
root.left.left = new TreeNode(1);
77+
root.left.right = new TreeNode(3);
78+
root.right.left = new TreeNode(6);
79+
root.right.right = new TreeNode(9);
80+
root.printTreeNode();
81+
System.out.println("-------");
82+
root.invertNode(root);
83+
root.printTreeNode();
3784
}
3885
}
3986
```
@@ -43,79 +90,56 @@ LRU,全称Least Recently Used,最近最少使用缓存。
4390

4491
在设计数据结构时,需要能够保持顺序,且是最近使用过的时间顺序被记录,这样每个item的相对位置代表了最近使用的顺序。满足这样考虑的结构可以是链表或者数组,不过链表更有利于Insert和Delete的操纵。
4592

46-
此外,需要记录链表的head和tail,从而方便进行移动到tail或者删除head的操作:
47-
head.next作为最近最少使用的item,tail.prev为最近使用过的item,
48-
49-
在set时,如果超出capacity,则删除head.next,同时将要插入的item放入tail.prev,
50-
在get时,如果存在,只需把item更新到tail.prev即可。
51-
这样set与get均为O(1)时间的操作 (HashMap Get/Set + LinkedList Insert/Delete),空间复杂度为O(n), n为capacity。
52-
5393
```java
54-
public class LRUCache {
55-
private class Node {
56-
Node prev;
57-
Node next;
58-
int key;
59-
int value;
60-
61-
public Node(int key, int value) {
62-
this.key = key;
63-
this.value = value;
64-
this.prev = null;
65-
this.next = null;
66-
}
67-
}
94+
import java.util.HashMap;
95+
import java.util.LinkedList;
6896

97+
public class LRUCache2 {
98+
private HashMap<Integer, Integer> cacheMap = new HashMap<>();
99+
private LinkedList<Integer> recentlyList = new LinkedList<>();
69100
private int capacity;
70-
private HashMap<Integer, Node> hm = new HashMap<Integer, Node>();
71-
private Node head = new Node(-1, -1);
72-
private Node tail = new Node(-1, -1);
73101

74-
// @param capacity, an integer
75-
public LRUCache(int capacity) {
102+
public LRUCache2(int capacity) {
76103
this.capacity = capacity;
77-
this.head.next = this.tail;
78-
this.tail.prev = this.head;
79104
}
80105

81-
// @return an integer
82-
public int get(int key) {
83-
if (!hm.containsKey(key)) {
106+
private int get(int key) {
107+
if (!cacheMap.containsKey(key)) {
84108
return -1;
85109
}
86-
Node current = hm.get(key);
87-
current.prev.next = current.next;
88-
current.next.prev = current.prev;
89110

90-
moveToTail(current);
111+
recentlyList.remove((Integer) key);
112+
recentlyList.add(key);
91113

92-
return hm.get(key).value;
114+
return cacheMap.get(key);
93115
}
94116

95-
// @param key, an integer
96-
// @param value, an integer
97-
// @return nothing
98-
public void set(int key, int value) {
99-
if (get(key) != -1) {
100-
hm.get(key).value = value;
101-
return;
117+
private void put(int key, int value) {
118+
if (cacheMap.containsKey(key)) {
119+
recentlyList.remove((Integer) key);
120+
recentlyList.add(key);
102121
}
103-
if (hm.size() == capacity) {
104-
hm.remove(head.next.key);
105-
head.next = head.next.next;
106-
head.next.prev = head;
122+
123+
if (cacheMap.size() == capacity) {
124+
cacheMap.remove(recentlyList.removeFirst());
107125
}
108126

109-
Node insert = new Node(key, value);
110-
hm.put(key, insert);
111-
moveToTail(insert);
127+
cacheMap.put(key, value);
128+
recentlyList.add(key);
129+
112130
}
113131

114-
private void moveToTail(Node current) {
115-
current.next = tail;
116-
tail.prev.next = current;
117-
current.prev = tail.prev;
118-
tail.prev = current;
132+
public static void main(String[] args) {
133+
LRUCache2 cache = new LRUCache2(2);
134+
cache.put(1, 1);
135+
cache.put(2, 2);
136+
System.out.println(cache.get(1)); // returns 1
137+
cache.put(3, 3); // 驱逐 key 2
138+
System.out.println(cache.get(2)); // returns -1 (not found)
139+
cache.put(4, 4); // 驱逐 key 1
140+
System.out.println(cache.get(1)); // returns -1 (not found)
141+
System.out.println(cache.get(3)); // returns 3
142+
System.out.println(cache.get(4)); // returns 4
119143
}
120144
}
121145
```

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
本项目是本人于2018年参加阿里、京东等其他公司电话、现场面试之后总结出来的针对Java面试的知识点。
1+
本项目是本人于2018年参加阿里、京东等其他公司电话、现场面试之后总结出来的针对Java面试的知识点,每个点都是被实际的面试中问过的
22

33
有疑问欢迎提 Issues 让我们共同解决,有好的想法想加进来的请提 PR ~
44

@@ -19,3 +19,4 @@ PS:除开知识点,一定要准备好1分钟~3分钟的个人介绍,以及
1919
### [搜索引擎](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/搜索引擎.md)
2020
搜索引擎是因为个人项目关系,那个搜索引擎非常简单,但有很多东西可以问到,可选择性学习借鉴,了解到对项目是如何提问的,还有问题的深度
2121
### [在线编程](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/在线编程.md)
22+
### [Linux](https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/Linux.md)

0 commit comments

Comments
 (0)