Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5aaac07

Browse files
feat: update 4 leetcode about stack and linkedlist
1 parent c578819 commit 5aaac07

File tree

5 files changed

+246
-1
lines changed

5 files changed

+246
-1
lines changed

‎.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.detectIndentation": false
3+
}

‎leetcode/146.lru-cache.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* @lc app=leetcode id=146 lang=typescript
3+
*
4+
* [146] LRU Cache
5+
*/
6+
7+
// @lc code=start
8+
interface cache {
9+
key: number;
10+
value: number;
11+
}
12+
/**
13+
* LRU 缓存淘汰算法-数组实现
14+
*/
15+
class LRUCache {
16+
private cacheList: cache[]; // 缓存数组
17+
private maxCacheLength: number; // 缓存容量
18+
19+
constructor(capacity: number) {
20+
this.cacheList = [];
21+
this.maxCacheLength = capacity;
22+
}
23+
24+
/**
25+
* 缓存的读取
26+
* @param key
27+
* @returns
28+
*/
29+
get(key: number): number {
30+
// 查找索引是否存在
31+
const cachedIndex = this.cacheList.findIndex((item: cache): boolean => item.key === key);
32+
if (cachedIndex === -1) {
33+
return cachedIndex;
34+
}
35+
// 修改原缓存数组
36+
const [cached] = this.cacheList.splice(cachedIndex, 1);
37+
// 把最近读取的放在第一位
38+
this.cacheList.unshift(cached);
39+
return cached.value;
40+
}
41+
42+
/**
43+
* 缓存的修改
44+
* @param key
45+
* @param value
46+
* @returns
47+
*/
48+
put(key: number, value: number): void {
49+
// 首先查找是否原先已缓存
50+
const cachedIndex = this.cacheList.findIndex((item: cache): boolean => item.key === key);
51+
// 如果已缓存,就更新值
52+
if (cachedIndex !== -1) {
53+
// 删除原先的缓存内容
54+
this.cacheList.splice(cachedIndex, 1);
55+
// 进行更新,放到最近的位置
56+
this.cacheList.unshift({ key, value });
57+
return;
58+
} else {
59+
this.cacheList.unshift({ key, value });
60+
// 如果超出了,就移除最早缓存的
61+
this.cacheList.length > this.maxCacheLength && this.cacheList.pop();
62+
return;
63+
}
64+
}
65+
}
66+
67+
/**
68+
* Your LRUCache object will be instantiated and called as such:
69+
* var obj = new LRUCache(capacity)
70+
* var param_1 = obj.get(key)
71+
* obj.put(key,value)
72+
*/
73+
// @lc code=end

‎leetcode/155.min-stack.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* @lc app=leetcode id=155 lang=typescript
3+
*
4+
* [155] Min Stack
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 用纯数组的方式解决
10+
* 时间复杂度 O(1)
11+
* 空间复杂度 O(n)
12+
*/
13+
class MinStack {
14+
private stack: number[]; // 数据栈
15+
private minStack: number[]; // 辅助栈:用于保存最小栈,需要把每个值都保存,最小值在栈顶
16+
17+
public constructor() {
18+
this.stack = [];
19+
this.minStack = [Number.MAX_SAFE_INTEGER];
20+
}
21+
22+
/**
23+
* 栈的推入
24+
* @param value
25+
* @returns
26+
*/
27+
public push(value: number): void {
28+
this.stack.push(value);
29+
// 注意这里推入的并不一定是传入的值,如果这个值比当前值大,那么推入会是原先的较小值
30+
// 这么做的目的是为了保存辅助栈和数据栈的同步
31+
this.minStack.push(Math.min(this.minStack[this.minStack.length - 1], value));
32+
}
33+
34+
/**
35+
* 栈的弹出(在非空栈上调用)
36+
* @returns
37+
*/
38+
public pop(): void {
39+
this.stack.pop();
40+
this.minStack.pop();
41+
}
42+
43+
/**
44+
* 获取栈顶元素
45+
* @returns
46+
*/
47+
public top(): number {
48+
return this.stack[this.stack.length - 1];
49+
}
50+
51+
/**
52+
* 获取栈中的最小值
53+
* 时间复杂度 O(1)
54+
* @returns
55+
*/
56+
getMin(): number {
57+
return this.minStack[this.minStack.length - 1];
58+
}
59+
}
60+
61+
class MinStack2 {
62+
private items: number[];
63+
private count: number;
64+
65+
public constructor() {
66+
this.items = [];
67+
this.count = 0;
68+
}
69+
70+
/**
71+
* 栈的推入
72+
* @param value
73+
* @returns
74+
*/
75+
public push(value: number): void {
76+
// if (this.count === this.length) {
77+
// return;
78+
// }
79+
this.items[this.count++] = value;
80+
// this.items.push(value);
81+
}
82+
83+
/**
84+
* 栈的弹出(在非空栈上调用)
85+
* @returns
86+
*/
87+
public pop(): void {
88+
// if (this.count === 0) {
89+
// return null;
90+
// }
91+
this.count--;
92+
const value: number = this.items[this.count]; // 不用数组 API 的方式,需要将 count 先减去多的
93+
this.items.length = this.count; // 注意一定要清除
94+
// return this.items.pop();
95+
}
96+
97+
/**
98+
* 获取栈顶元素
99+
* @returns
100+
*/
101+
public top(): number {
102+
// 题目中不存在非空栈
103+
// if (this.count === 0) {
104+
// return null;
105+
// }
106+
const value: any = this.items[this.count - 1]; // 不用数组 API 的方式,需要将 count 先减去多的
107+
return value;
108+
}
109+
110+
/**
111+
* 获取栈中的最小值
112+
* 时间复杂度 O(n)
113+
* @returns
114+
*/
115+
getMin(): number {
116+
let minValue = Number.MAX_SAFE_INTEGER;
117+
this.items.forEach((value) => {
118+
if (value < minValue) {
119+
minValue = value;
120+
}
121+
});
122+
return minValue;
123+
}
124+
}
125+
126+
/**
127+
* Your MinStack object will be instantiated and called as such:
128+
* var obj = new MinStack()
129+
* obj.push(val)
130+
* obj.pop()
131+
* var param_3 = obj.top()
132+
* var param_4 = obj.getMin()
133+
*/
134+
// @lc code=end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode id=876 lang=typescript
3+
*
4+
* [876] Middle of the Linked List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* class ListNode {
11+
* val: number
12+
* next: ListNode | null
13+
* constructor(val?: number, next?: ListNode | null) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
* }
18+
*/
19+
20+
function middleNode(head: ListNode | null): ListNode | null {
21+
if (head === null || head.next === null) {
22+
return head;
23+
}
24+
25+
let slow = head;
26+
let fast = head;
27+
28+
while (fast && fast.next !== null) {
29+
slow = slow.next;
30+
fast = fast.next.next;
31+
}
32+
33+
return slow;
34+
};
35+
// @lc code=end

‎leetcode/912.sort-an-array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var quickSort = function(arr) {
1616
}
1717
// 如何选择基准值,用中间索引来获取
1818
let baseIndex = Math.floor(arr.length / 2);
19-
// 取出中间值
19+
// 取出中间值,注意也修改了原数组,使得数组去掉中间值进行的遍历
2020
let baseVal = arr.splice(baseIndex, 1)[0];
2121
let left = [];
2222
let right = [];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /