Skip to content

Commit f30b25d

Browse files
committed
feat: 🎸 update leetcode
1 parent d71327c commit f30b25d

18 files changed

+778
-83
lines changed

src/algorithm/recursion/dfs-bfs.js

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,86 @@
11
/**
22
* @desc 构造节点
3-
* @param {Any} key
3+
* @param {Any} key
44
*/
5-
function Node (key) {
6-
this.children = []
7-
this.key = key
5+
function Node(key) {
6+
this.children = [];
7+
this.key = key;
88
}
99

1010
// 生成树结构
11-
const n1 = new Node('1')
12-
const n2 = new Node('2')
13-
const n3 = new Node('3')
14-
const n4 = new Node('4')
15-
const n5 = new Node('5')
16-
const n6 = new Node('6')
11+
const n1 = new Node("1");
12+
const n2 = new Node("2");
13+
const n3 = new Node("3");
14+
const n4 = new Node("4");
15+
const n5 = new Node("5");
16+
const n6 = new Node("6");
1717

18-
n1.children.push(n2)
19-
n1.children.push(n5)
20-
n2.children.push(n3)
21-
n2.children.push(n4)
22-
n5.children.push(n6)
18+
n1.children.push(n2);
19+
n1.children.push(n5);
20+
n2.children.push(n3);
21+
n2.children.push(n4);
22+
n5.children.push(n6);
23+
24+
// 1
25+
// 2 5
26+
// 3 4 6
2327

2428
/**
2529
* @desc dfs 深度优先搜索
26-
* @param {Node} node
30+
* @param {Node} node
2731
*/
28-
function dfs (node) {
29-
const stack = [node]
30-
let ret = []
32+
function dfs(node) {
33+
const stack = [node];
34+
let ret = [];
3135
while (stack.length > 0) {
32-
const first = stack.shift()
33-
ret.push(first.key)
34-
first.children.slice().reverse().forEach(child => stack.unshift(child))
36+
const first = stack.shift();
37+
ret.push(first.key);
38+
first.children
39+
.slice()
40+
.reverse()
41+
.forEach((child) => stack.unshift(child));
3542
}
36-
return ret
43+
return ret;
3744
}
3845
// Test
39-
console.log('dfs: ', dfs(n1))
46+
console.log("dfs: ", dfs(n1));
4047

4148
/**
4249
* @desc dfs 深度优先搜索 - 递归实现
43-
* @param {Node} node
50+
* @param {Node} node
4451
*/
45-
function dfsRecursion (node) {
46-
let ret = []
47-
_dfsRecursion(node, ret)
48-
return ret
52+
function dfsRecursion(node) {
53+
let ret = [];
54+
_dfsRecursion(node, ret);
55+
return ret;
4956
}
5057

51-
function _dfsRecursion (node, ret) {
52-
ret.push(node.key)
53-
node.children.forEach(child => _dfsRecursion(child, ret))
58+
function _dfsRecursion(node, ret) {
59+
ret.push(node.key);
60+
node.children.forEach((child) => _dfsRecursion(child, ret));
5461
}
5562
// Test
56-
console.log('dfsRecursion: ', dfsRecursion(n1))
63+
console.log("dfsRecursion: ", dfsRecursion(n1));
5764

5865
/**
5966
* @desc dfs 深度优先搜索
60-
* @param {Node} node
67+
* @param {Node} node
6168
*/
62-
function bfs (node) {
63-
const queue = [node]
64-
let ret = []
69+
function bfs(node) {
70+
const queue = [node];
71+
let ret = [];
6572
while (queue.length > 0) {
66-
const first = queue.shift()
67-
ret.push(first.key)
68-
first.children.forEach(child => queue.push(child))
73+
const first = queue.shift();
74+
ret.push(first.key);
75+
first.children.forEach((child) => queue.push(child));
6976
}
70-
return ret
77+
return ret;
7178
}
7279
// Test
73-
console.log('bfs: ', bfs(n1))
80+
console.log("bfs: ", bfs(n1));
7481

75-
export {
76-
dfs,
77-
dfsRecursion,
78-
bfs
79-
}
82+
// export {
83+
// dfs,
84+
// dfsRecursion,
85+
// bfs
86+
// }

src/leetcode/003-longest-substring-without-reapting-characters.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,48 @@ function lengthOfLongestSubstring(s) {
2626
console.log(lengthOfLongestSubstring("abcabcbb"));
2727
console.log(lengthOfLongestSubstring("bbbbb"));
2828
console.log(lengthOfLongestSubstring("pwwkew"));
29+
30+
// 重新再写一遍
31+
// 找到一个字符串中最长的不重复子串的长度
32+
function lengthOfLongestSubstringNew(s) {
33+
const set = new Set();
34+
const len = s.length;
35+
let rightKey = -1;
36+
let result = 0;
37+
for (let i = 0; i < len; i++) {
38+
if (i !== 0) {
39+
set.delete(s[i - 1]);
40+
}
41+
while (rightKey + 1 < len && !set.has(s[rightKey + 1])) {
42+
set.add(s[rightKey + 1]);
43+
rightKey++;
44+
}
45+
result = Math.max(result, rightKey - i + 1);
46+
}
47+
return result;
48+
}
49+
50+
console.log(lengthOfLongestSubstringNew("abcabcbb")); // abc
51+
console.log(lengthOfLongestSubstringNew("bbbbb"));
52+
console.log(lengthOfLongestSubstringNew("pwwkew"));
53+
54+
function lengthOfLongestSubstringNew1(s) {
55+
let result = 0;
56+
const len = s.length;
57+
const set = new Set();
58+
let left = 0;
59+
for (let right = 0; right < len; right++) {
60+
const value = s[right];
61+
while (set.has(value)) {
62+
set.delete(s[left++]);
63+
}
64+
set.add(value);
65+
console.log("set: ", set);
66+
result = Math.max(result, right - left + 1);
67+
}
68+
return result;
69+
}
70+
71+
console.log(lengthOfLongestSubstringNew1("abcbacbb"));
72+
console.log(lengthOfLongestSubstringNew1("bbbbb"));
73+
console.log(lengthOfLongestSubstringNew1("pwwkew"));
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @desc 59. 删除链表的倒数第 N 个结点
10+
* @leetcode https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/
11+
* @param {ListNode} head
12+
* @param {number} n
13+
* @return {ListNode}
14+
*/
15+
var removeNthFromEndError = function (head, n) {
16+
let current = head;
17+
let layer = 0;
18+
while (current) {
19+
layer++;
20+
current = current.next;
21+
}
22+
let _current = head;
23+
const nLayer = layer - n;
24+
let _layer = 0;
25+
while (_current) {
26+
if (_layer === nLayer) {
27+
if (_current.next) {
28+
_current.val = _current.next.val;
29+
_current.next = _current.next.next;
30+
} else {
31+
_current = null;
32+
}
33+
// break;
34+
}
35+
_layer++;
36+
_current = _current.next;
37+
}
38+
return head;
39+
};
40+
41+
// 两个指针,找到需要删除的前一个节点,然后直接将 next 指向下下个即可
42+
var removeNthFromEnd = function (head, n) {
43+
const dummyNode = ListNode(-1, head);
44+
let left = dummyNode;
45+
let right = dummyNode;
46+
while (n--) {
47+
right = right.next;
48+
}
49+
while (right.next) {
50+
right = right.next;
51+
left = left.next;
52+
}
53+
left.next = left.next.next;
54+
return dummyNode.next;
55+
};
56+
57+
function ListNode(val, next) {
58+
this.val = val === undefined ? 0 : val;
59+
this.next = next === undefined ? null : next;
60+
}
61+
62+
const node1 = new ListNode(1);
63+
const node2 = new ListNode(2);
64+
const node3 = new ListNode(3);
65+
const node4 = new ListNode(4);
66+
const node5 = new ListNode(5);
67+
node1.next = node2;
68+
node2.next = node3;
69+
node3.next = node4;
70+
node4.next = node5;
71+
72+
// 1 2 3 4 5
73+
// 1 2 3 5
74+
console.log(removeNthFromEnd(node1, 2));

src/leetcode/028-implement-strstr.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@
88
*/
99
// 方法一
1010
var strStr = function (haystack, needle) {
11-
return haystack.indexOf(needle)
12-
}
11+
return haystack.indexOf(needle);
12+
};
1313
// Test
14-
console.log(strStr('hello', 'll'))
15-
console.log(strStr('aaaaa', 'bba'))
14+
console.log(strStr("hello", "ll"));
15+
console.log(strStr("aaaaa", "bba"));
1616

1717
// 方法二
1818
var strStr2 = function (haystack, needle) {
1919
for (let i = 0; i < haystack.length; i++) {
2020
if (haystack[i] === needle[0]) {
2121
if (haystack.substr(i, needle.length) === needle) {
22-
return i
22+
return i;
2323
}
2424
}
2525
}
26-
return -1
27-
}
26+
return -1;
27+
};
2828
// Test
29-
console.log(strStr2('hello', 'll'))
30-
console.log(strStr2('aaaaa', 'bba'))
29+
console.log(strStr2("hello", "ll"));
30+
console.log(strStr2("aaaaa", "bba"));
31+
32+
// 方法三 KMP 实现
33+
// 代码随想录 视频讲解 https://www.bilibili.com/video/BV1M5411j7Xx?vd_source=a0beb1b1b69021ba06490959315cf7ef&spm_id_from=333.788.videopod.sections

src/leetcode/056-merge-intervals.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @desc 56. 合并区间
3+
* @leetcode https://leetcode.cn/problems/merge-intervals/description/
4+
* @param {number[][]} intervals
5+
* @return {number[][]}
6+
*/
7+
var merge = function (intervals) {
8+
intervals = intervals.sort((a, b) => a[0] - b[0]);
9+
let answer = [];
10+
for (let value of intervals) {
11+
const last = answer.length - 1;
12+
// 可以合并的条件
13+
if (answer.length && value[0] <= answer[last][1]) {
14+
answer[last][1] = Math.max(answer[last][1], value[1]);
15+
} else {
16+
answer.push(value);
17+
}
18+
}
19+
return answer;
20+
};
21+
22+
// 先根据每一项左区间做排序
23+
// 遍历
24+
// [[1,3]]
25+
console.log(
26+
merge([
27+
[15, 18],
28+
[1, 3],
29+
[8, 10],
30+
[2, 6],
31+
])
32+
);

0 commit comments

Comments
 (0)