Compare commits
9 Commits
3115552b87
...
master
Author | SHA1 | Date | |
---|---|---|---|
20f821a921 | |||
2e60e7aa98 | |||
69986d68fd | |||
7f6439ce7c | |||
41aa639602 | |||
146b33a651 | |||
450f1f6b5d | |||
89d8ac5e58 | |||
16ec46e003 |
3
.github/workflows/nodejs.yml
vendored
3
.github/workflows/nodejs.yml
vendored
@ -11,12 +11,11 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
node-version: [10.x, 12.x]
|
node-version: [12.x, 14.x]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
13
.vscode/settings.json
vendored
13
.vscode/settings.json
vendored
@ -36,5 +36,16 @@
|
|||||||
"zhan",
|
"zhan",
|
||||||
"zhong",
|
"zhong",
|
||||||
"zhuan"
|
"zhuan"
|
||||||
]
|
],
|
||||||
|
"standard.options": {
|
||||||
|
"globals": [
|
||||||
|
"$",
|
||||||
|
"jQuery",
|
||||||
|
"fetch"
|
||||||
|
],
|
||||||
|
"usePackageJson": true,
|
||||||
|
"env": {
|
||||||
|
"jest": true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
19
README.md
19
README.md
@ -376,6 +376,16 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
|||||||
- LeetCode 739. 每日温度 <https://leetcode-cn.com/problems/daily-temperatures/>
|
- LeetCode 739. 每日温度 <https://leetcode-cn.com/problems/daily-temperatures/>
|
||||||
- LintCode 1060. 每日温度 <https://www.lintcode.com/problem/daily-temperatures/description>
|
- LintCode 1060. 每日温度 <https://www.lintcode.com/problem/daily-temperatures/description>
|
||||||
|
|
||||||
|
- [最长重复子数组](src/array/maximum-length-of-repeated-subarray.js)
|
||||||
|
|
||||||
|
- LeetCode 718. 最长重复子数组 <https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/>
|
||||||
|
- LintCode 79. 最长公共子串 <https://www.lintcode.com/problem/longest-common-substring/description>
|
||||||
|
|
||||||
|
- [有序矩阵中第 K 小的元素](src/array/kth-smallest-element-in-a-sorted-matrix.js)
|
||||||
|
|
||||||
|
- LeetCode 378. 有序矩阵中第 K 小的元素 <https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/>
|
||||||
|
- LintCode 401. 排序矩阵中的从小到大第 k 个数 <https://www.lintcode.com/problem/kth-smallest-number-in-sorted-matrix/description>
|
||||||
|
|
||||||
## 栈
|
## 栈
|
||||||
|
|
||||||
- [最大矩阵](src/stack/maximal-rectangle.js)
|
- [最大矩阵](src/stack/maximal-rectangle.js)
|
||||||
@ -501,6 +511,15 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
|||||||
- LeetCode 105. 从前序与中序遍历序列构造二叉树 <https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/>
|
- LeetCode 105. 从前序与中序遍历序列构造二叉树 <https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/>
|
||||||
- LintCode 73. 前序遍历和中序遍历树构造二叉树 <https://www.lintcode.com/problem/construct-binary-tree-from-preorder-and-inorder-traversal/description>
|
- LintCode 73. 前序遍历和中序遍历树构造二叉树 <https://www.lintcode.com/problem/construct-binary-tree-from-preorder-and-inorder-traversal/description>
|
||||||
|
|
||||||
|
- [将有序数组转换为二叉搜索树](src/tree/convert-sorted-array-to-binary-search-tree.js)
|
||||||
|
|
||||||
|
- LeetCode 108. 将有序数组转换为二叉搜索树 <https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/>
|
||||||
|
- LintCode 106. 有序链表转换为二叉搜索树 <https://www.lintcode.com/problem/convert-sorted-list-to-binary-search-tree/description>
|
||||||
|
|
||||||
|
- [不同的二叉搜索树](src/tree/unique-binary-search-trees.js)
|
||||||
|
|
||||||
|
- LeetCode 96. 不同的二叉搜索树 <https://leetcode-cn.com/problems/unique-binary-search-trees/>
|
||||||
|
|
||||||
## 链表
|
## 链表
|
||||||
|
|
||||||
- [合并 K 个排序链表](src/list/merge-k-sorted-lists.js)
|
- [合并 K 个排序链表](src/list/merge-k-sorted-lists.js)
|
||||||
|
8
src/array/kth-smallest-element-in-a-sorted-matrix.js
Normal file
8
src/array/kth-smallest-element-in-a-sorted-matrix.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* @param {number[][]} matrix
|
||||||
|
* @param {number} k
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
export const kthSmallest = function (matrix, k) {
|
||||||
|
return matrix.flat().sort((a, b) => a - b)[k - 1]
|
||||||
|
}
|
38
src/array/maximum-length-of-repeated-subarray.js
Normal file
38
src/array/maximum-length-of-repeated-subarray.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// 输入:
|
||||||
|
// A: [1, 2, 3, 2, 1]
|
||||||
|
// B: [3, 2, 1, 4, 7]
|
||||||
|
|
||||||
|
const maxLength = function (A, B, addA, addB, len) {
|
||||||
|
addA = (addA > 0) ? addA : 0
|
||||||
|
addB = (addB > 0) ? addB : 0
|
||||||
|
let result = 0
|
||||||
|
let k = 0
|
||||||
|
for (let i = 0; i < len && (k + len - i > result); i++) {
|
||||||
|
if (A[i + addA] === B[i + addB]) {
|
||||||
|
k++
|
||||||
|
} else {
|
||||||
|
k = 0
|
||||||
|
}
|
||||||
|
result = Math.max(result, k)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number[]} A
|
||||||
|
* @param {number[]} B
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
export const findLength = function (A, B) {
|
||||||
|
const ALen = A.length
|
||||||
|
const BLen = B.length
|
||||||
|
let result = 0
|
||||||
|
for (let i = 1; i < ALen + BLen; i++) {
|
||||||
|
if (result >= (ALen + BLen - i)) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
const len = Math.min(i, ALen, BLen, (ALen + BLen - i))
|
||||||
|
result = Math.max(maxLength(A, B, ALen - i, i - ALen, len), result)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
20
src/tree/convert-sorted-array-to-binary-search-tree.js
Normal file
20
src/tree/convert-sorted-array-to-binary-search-tree.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import TreeNode from '../../test/tree/TreeNode'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums
|
||||||
|
* @return {TreeNode}
|
||||||
|
*/
|
||||||
|
export const sortedArrayToBST = function (nums) {
|
||||||
|
if (!nums.length) return null
|
||||||
|
|
||||||
|
const creatTree = (left, right) => {
|
||||||
|
if (left > right) return null
|
||||||
|
const mid = Math.floor((left + right) / 2)
|
||||||
|
const root = new TreeNode(nums[mid])
|
||||||
|
root.left = creatTree(left, mid - 1)
|
||||||
|
root.right = creatTree(mid + 1, right)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
return creatTree(0, nums.length - 1)
|
||||||
|
}
|
12
src/tree/unique-binary-search-trees.js
Normal file
12
src/tree/unique-binary-search-trees.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// 卡塔兰数
|
||||||
|
/**
|
||||||
|
* @param {number} n
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
export const numTrees = function (n) {
|
||||||
|
let C = 1
|
||||||
|
for (let i = 0; i < n; ++i) {
|
||||||
|
C = C * 2 * (2 * i + 1) / (i + 2)
|
||||||
|
}
|
||||||
|
return C
|
||||||
|
}
|
20
test/array/kth-smallest-element-in-a-sorted-matrix.test.js
Normal file
20
test/array/kth-smallest-element-in-a-sorted-matrix.test.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { kthSmallest } from '../../src/array/kth-smallest-element-in-a-sorted-matrix.js'
|
||||||
|
|
||||||
|
test('有序矩阵中第K小的元素', () => {
|
||||||
|
expect(kthSmallest([
|
||||||
|
[1, 5, 9],
|
||||||
|
[10, 11, 13],
|
||||||
|
[12, 13, 15]
|
||||||
|
], 8)).toBe(13)
|
||||||
|
|
||||||
|
expect(kthSmallest([
|
||||||
|
[1, 5, 7],
|
||||||
|
[3, 7, 8],
|
||||||
|
[4, 8, 9]
|
||||||
|
], 4)).toBe(5)
|
||||||
|
|
||||||
|
expect(kthSmallest([
|
||||||
|
[1, 2],
|
||||||
|
[3, 4]
|
||||||
|
], 3)).toBe(3)
|
||||||
|
})
|
5
test/array/maximum-length-of-repeated-subarray.test.js
Normal file
5
test/array/maximum-length-of-repeated-subarray.test.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { findLength } from '../../src/array/maximum-length-of-repeated-subarray.js'
|
||||||
|
|
||||||
|
test('最长重复子数组', () => {
|
||||||
|
expect(findLength([1, 2, 3, 2, 1], [3, 2, 1, 4, 7])).toBe(3)
|
||||||
|
})
|
@ -0,0 +1,5 @@
|
|||||||
|
import { sortedArrayToBST } from '../../src/tree/convert-sorted-array-to-binary-search-tree.js'
|
||||||
|
|
||||||
|
test('将有序数组转换为二叉搜索树', () => {
|
||||||
|
expect(sortedArrayToBST([-10, -3, 0, 5, 9])).toEqual({ left: { left: null, right: { left: null, right: null, val: -3 }, val: -10 }, right: { left: null, right: { left: null, right: null, val: 9 }, val: 5 }, val: 0 })
|
||||||
|
})
|
5
test/tree/unique-binary-search-trees.test.js
Normal file
5
test/tree/unique-binary-search-trees.test.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { numTrees } from '../../src/tree/unique-binary-search-trees'
|
||||||
|
|
||||||
|
test('不同的二叉搜索树', () => {
|
||||||
|
expect(numTrees(3)).toBe(5)
|
||||||
|
})
|
Reference in New Issue
Block a user