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 66932d6

Browse files
feat: update 4 leetcode about linkedlist
1 parent bde24bf commit 66932d6

File tree

5 files changed

+372
-6
lines changed

5 files changed

+372
-6
lines changed

‎.prettierrc.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// .prettierrc.js
2+
module.exports = {
3+
// 一行最多 120 字符
4+
printWidth: 120,
5+
// 使用 2 个空格缩进
6+
tabWidth: 2,
7+
// 不使用缩进符,而使用空格
8+
useTabs: false,
9+
// 行尾需要有分号
10+
semi: true,
11+
// 使用单引号
12+
singleQuote: true,
13+
// 对象的 key 仅在必要时用引号
14+
quoteProps: 'as-needed',
15+
// jsx 不使用单引号,而使用双引号
16+
jsxSingleQuote: false,
17+
// 末尾需要有逗号
18+
trailingComma: 'all',
19+
// 大括号内的首尾需要空格
20+
bracketSpacing: true,
21+
// jsx 标签的反尖括号需要换行
22+
jsxBracketSameLine: false,
23+
// 箭头函数,只有一个参数的时候,也需要括号
24+
arrowParens: 'always',
25+
// 每个文件格式化的范围是文件的全部内容
26+
rangeStart: 0,
27+
rangeEnd: Infinity,
28+
// 不需要写文件开头的 @prettier
29+
requirePragma: false,
30+
// 不需要自动在文件开头插入 @prettier
31+
insertPragma: false,
32+
// 使用默认的折行标准
33+
proseWrap: 'preserve',
34+
// 根据显示样式决定 html 要不要折行
35+
htmlWhitespaceSensitivity: 'css',
36+
// 换行符使用 lf
37+
endOfLine: 'lf',
38+
// 格式化嵌入的内容
39+
embeddedLanguageFormatting: 'auto',
40+
};

‎leetcode/215.kth-largest-element-in-an-array.ts

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,110 @@
55
*/
66

77
// @lc code=start
8+
/**
9+
* @description: 堆排序的方式
10+
* 时间复杂度 O(nlogk)
11+
* @param {number[]} nums
12+
* @param {number} k
13+
* @return {numer}
14+
*/
15+
function findKthLargest(nums: number[], k: number): number {
16+
/**
17+
* @description: 数组原地交换
18+
* @param {number[]} nums
19+
* @param {number} i
20+
* @param {number} j
21+
* @return {void}
22+
*/
23+
function swap(nums: number[], i: number, j: number): void {
24+
[nums[j], nums[i]] = [nums[i], nums[j]];
25+
}
26+
/**
27+
* @description: 建堆,从后向前
28+
* @param {number[]} nums
29+
* @param {number} heapSize
30+
* @param {number} i
31+
* @return {void}
32+
*/
33+
function buildHeap(nums: number[], heapSize: number) {
34+
// 因为从 0 开始,所以要减去 1
35+
for (let i = Math.floor(heapSize / 2 - 1); i >= 0; i--) {
36+
heapify(nums, heapSize, i);
37+
}
38+
}
39+
/**
40+
* @description: 自上而下堆化
41+
* @param {number[]} nums
42+
* @param {number} heapSize
43+
* @param {number} i
44+
* @return {void}
45+
*/
46+
function heapify(nums: number[], heapSize: number, i: number) {
47+
let maxIndex = i;
48+
while (true) {
49+
if (2 * i + 1 <= heapSize && nums[2 * i + 1] > nums[maxIndex]) maxIndex = 2 * i + 1;
50+
if (2 * i + 2 <= heapSize && nums[2 * i + 2] > nums[maxIndex]) maxIndex = 2 * i + 2;
51+
if (maxIndex === i) break;
52+
swap(nums, i, maxIndex);
53+
i = maxIndex;
54+
}
55+
}
56+
57+
// 建堆
58+
buildHeap(nums, nums.length);
59+
let len = nums.length - 1;
60+
// !取前 k 大的数,也就是判断到 k 为止
61+
for (let index = nums.length - 1; index > nums.length - k; index--) {
62+
swap(nums, len, 0);
63+
len--;
64+
// 从头开始堆化
65+
heapify(nums, len, 0);
66+
}
67+
68+
return nums[0];
69+
}
70+
871
/**
972
* @description: 分区函数
10-
* @param {number} arr
73+
* @param {numberp[]} arr
1174
* @param {number} start
1275
* @param {number} end
1376
* @return {number}
1477
*/
1578
function partition(arr: number[], start: number, end: number): number {
1679
const pivotValue = arr[end];
1780
let pivotIndex = start;
81+
// 从设定的起始值开始循环
1882
for (let i = start; i < end; i++) {
83+
// 如果当前索引的值小于分区值,就交换位置,把当前索引的值交换到起始位置
1984
if (arr[i] < pivotValue) {
85+
// 交换过程,并且如果发生交换,起始位置的索引也会加一
2086
[arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]];
2187
pivotIndex++;
2288
}
2389
}
2490

91+
// 把分区值从末尾交换到中间位置,以此为界的左边应当都都小于分区值,右边则都大于分区值
2592
[arr[pivotIndex], arr[end]] = [arr[end], arr[pivotIndex]];
2693

2794
return pivotIndex;
2895
}
2996

3097
/**
3198
* @description: 使用快排实现寻找第 k 大元素
32-
* 时间复杂度 O(n)
33-
* @param {number} nums
99+
* 时间复杂度 O(nlogn)
100+
* @param {number[]} nums
34101
* @param {number} k
35102
* @return {number}
36103
*/
37-
function findKthLargest(nums: number[], k: number): number {
104+
function findKthLargest2(nums: number[], k: number): number {
38105
let left = 0;
39106
let right = nums.length - 1;
40107
// 数组中第 k 大的数等于排序之后从前往后第 len - k 个数,注意数组从前往后是以 0 起始的,所以可以直接相减
41108
const n = nums.length - k;
42109

43110
while (true) {
44-
// 寻找分区点
111+
// 寻找分区点(实际在这个过程中,数组也是在排序的)
45112
const pivotIndex = partition(nums, left, right);
46113
if (pivotIndex === n) {
47114
return nums[pivotIndex];
@@ -53,5 +120,5 @@ function findKthLargest(nums: number[], k: number): number {
53120
right = pivotIndex - 1;
54121
}
55122
}
56-
};
123+
}
57124
// @lc code=end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode id=237 lang=typescript
3+
*
4+
* [237] Delete Node in a 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+
/**
21+
Do not return anything, modify it in-place instead.
22+
*/
23+
function deleteNode(root: ListNode | null): void {
24+
// 题干保证了节点不是尾结点
25+
root.val = root.next.val;
26+
root.next = root.next.next;
27+
}
28+
// @lc code=end

‎leetcode/707.design-linked-list.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* @lc app=leetcode id=707 lang=typescript
3+
*
4+
* [707] Design Linked List
5+
*/
6+
7+
// @lc code=start
8+
class LinkedNode<T> {
9+
public val: T;
10+
public next: LinkedNode<T>;
11+
12+
constructor(val: T, next: LinkedNode<T> = null) {
13+
this.val = val;
14+
this.next = next;
15+
}
16+
}
17+
18+
/**
19+
* @description: 实现链表
20+
* @return {*}
21+
*/
22+
class MyLinkedList {
23+
public head: LinkedNode<number> = null;
24+
public count: number = 0;
25+
constructor() {}
26+
27+
/**
28+
* @description: 获取对应索引的值
29+
* @param {number} index
30+
* @return {number}
31+
*/
32+
get(index: number): number {
33+
// 注意,等于链表长度的索引也不可以
34+
if (index < 0 || index >= this.count) return -1;
35+
36+
if (index === 0) return this.head.val;
37+
38+
let i = 1;
39+
let current = this.head.next;
40+
while (current !== null && index !== i) {
41+
current = current.next;
42+
i++;
43+
}
44+
return current.val;
45+
}
46+
47+
/**
48+
* @description: 添加头结点
49+
* @param {number} val
50+
* @return {void}
51+
*/
52+
addAtHead(val: number): void {
53+
const linkedNode = new LinkedNode(val);
54+
const oldHead = this.head;
55+
56+
linkedNode.next = oldHead;
57+
this.head = linkedNode;
58+
this.count++;
59+
}
60+
61+
/**
62+
* @description: 添加尾结点
63+
* @param {number} val
64+
* @return {void}
65+
*/
66+
addAtTail(val: number): void {
67+
const linkedNode = new LinkedNode(val);
68+
if (this.head === null) {
69+
this.head = linkedNode;
70+
this.count++;
71+
return;
72+
}
73+
let current = this.head;
74+
while (current.next !== null) {
75+
current = current.next;
76+
}
77+
current.next = linkedNode;
78+
this.count++;
79+
}
80+
81+
/**
82+
* @description: 在对应索引处插入节点
83+
* @param {number} index
84+
* @param {number} val
85+
* @return {void}
86+
*/
87+
addAtIndex(index: number, val: number): void {
88+
// 如果索引超过链表长度就什么都不做
89+
if (index > this.count) return;
90+
91+
const linkedNode = new LinkedNode(val);
92+
// 如果索引小于等于 0 就插入头结点
93+
if (index <= 0) {
94+
this.addAtHead(val);
95+
}
96+
let i = 1;
97+
let prev = this.head;
98+
let current = prev.next;
99+
while (current !== null && i !== index) {
100+
current = current.next;
101+
prev = prev.next;
102+
i++;
103+
}
104+
prev.next = linkedNode;
105+
linkedNode.next = current;
106+
this.count++;
107+
}
108+
109+
/**
110+
* @description: 删除对应索引的结点
111+
* @param {number} index
112+
* @return {void}
113+
*/
114+
deleteAtIndex(index: number): void {
115+
if (index < 0 || index >= this.count) return;
116+
// 删除头结点需特殊处理
117+
if (index === 0) {
118+
this.head = this.head.next;
119+
this.count--;
120+
return;
121+
}
122+
let i = 1;
123+
let prev = this.head;
124+
let current = prev.next;
125+
while (current !== null && i !== index) {
126+
current = current.next;
127+
prev = prev.next;
128+
i++;
129+
}
130+
prev.next = current ? current.next : null;
131+
this.count--;
132+
}
133+
}
134+
135+
/**
136+
* Your MyLinkedList object will be instantiated and called as such:
137+
* var obj = new MyLinkedList()
138+
* var param_1 = obj.get(index)
139+
* obj.addAtHead(val)
140+
* obj.addAtTail(val)
141+
* obj.addAtIndex(index,val)
142+
* obj.deleteAtIndex(index)
143+
*/
144+
// @lc code=end

0 commit comments

Comments
(0)

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