Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 26e8daa

Browse files
committed
feat: LRUCache
1 parent f04e6d6 commit 26e8daa

File tree

4 files changed

+129
-18
lines changed

4 files changed

+129
-18
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"printWidth": 80,
3-
"tabWidth": 2
3+
"tabWidth": 2,
4+
"semi": false
45
}

LRU缓存.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class DoubleNode {
2+
constructor(key, val) {
3+
this.key = key
4+
this.val = val
5+
6+
this.prev = null
7+
this.next = null
8+
}
9+
}
10+
11+
class LRUCache {
12+
constructor(max) {
13+
this.max = max
14+
this.map = new Map()
15+
16+
this.head = null
17+
this.tail = null
18+
}
19+
20+
get(key) {
21+
const node = this.map.get(key)
22+
if (!node) {
23+
return -1
24+
} else {
25+
const res = node.val
26+
this.remove(node)
27+
this.appendHead(node)
28+
return res
29+
}
30+
}
31+
32+
put(key, value) {
33+
let node = this.map.get(key)
34+
// 有这个缓存
35+
if (node) {
36+
node.val = value
37+
// 新加入的 放在最前面
38+
this.remove(node)
39+
this.appendHead(node)
40+
} else {
41+
// 没有这个缓存
42+
node = new DoubleNode(key, value)
43+
// 如果超出容量了 删除最后一个 再放到头部
44+
if (this.map.size >= this.max) {
45+
this.map.delete(this.tail.key)
46+
this.remove(this.tail)
47+
this.appendHead(node)
48+
this.map.set(key, node)
49+
} else {
50+
// 未超出容量 就直接放到头部
51+
this.appendHead(node)
52+
this.map.set(key, node)
53+
}
54+
}
55+
}
56+
57+
/**
58+
* 把头部指针的改变成新的node
59+
* @param {DoubleNode} node
60+
*/
61+
appendHead(node) {
62+
if (this.head === null) {
63+
this.head = this.tail = node
64+
} else {
65+
node.next = this.head
66+
this.head.prev = node
67+
this.head = node
68+
}
69+
}
70+
71+
/**
72+
* 删除某个节点
73+
* @param {DoubleNode} node
74+
*/
75+
remove(node) {
76+
if (this.head === this.tail) {
77+
this.head = this.tail = null
78+
} else {
79+
// 删除头部
80+
if (this.head === node) {
81+
this.head = this.head.next
82+
node.next = null
83+
} else if (this.tail === node) {
84+
this.tail = this.tail.prev
85+
this.tail.next = null
86+
node.prev = null
87+
} else {
88+
node.prev.next = node.next
89+
node.next.prev = node.prev
90+
node.prev = node.next = null
91+
}
92+
}
93+
}
94+
}

排序/选择排序.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const swap = require("../工具/交换");
2+
3+
function selectSort(arr) {
4+
for (let i = 0; i < arr.length; i++) {
5+
let min = i;
6+
for (let j = i + 1; j < arr.length; j++) {
7+
if (arr[j] < arr[min]) {
8+
min = j;
9+
}
10+
}
11+
swap(arr, i, min);
12+
}
13+
return arr;
14+
}
15+
16+
console.log(selectSort([1, 4, 3, 2, 5, 6]));

验证回文字符串2.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22
* @param {string} s
33
* @return {boolean}
44
*/
5-
let validPalindrome = function(s) {
5+
let validPalindrome = function (s) {
66
let i = 0;
7-
let j = s.length - 1
8-
7+
let j = s.length - 1;
8+
99
// 两个指针往中间缩进
1010
while (i < j && s[i] === s[j]) {
11-
i++
12-
j--
11+
i++;
12+
j--;
1313
}
1414

1515
// 遇到相对位置不相等了 判断删除一位后的情况
1616
if (isPalindrome(i + 1, j)) {
17-
return true
17+
return true;
1818
}
1919
if (isPalindrome(i, j - 1)) {
20-
return true
20+
return true;
2121
}
2222

2323
// 工具方法,用于判断字符串是否回文
2424
function isPalindrome(st, ed) {
25-
while(st<ed) {
26-
if(s[st] !== s[ed]) {
27-
return false
28-
}
29-
st++
30-
ed--
31-
}
32-
return true
25+
while (st < ed) {
26+
if (s[st] !== s[ed]) {
27+
return false;
28+
}
29+
st++;
30+
ed--;
31+
}
32+
return true;
3333
}
3434

3535
// 这样都不满足 那就不符合要求了
36-
return false
37-
};
36+
return false;
37+
};

0 commit comments

Comments
 (0)