Compare commits
11 Commits
ed223c4b54
...
master
Author | SHA1 | Date | |
---|---|---|---|
20f821a921 | |||
2e60e7aa98 | |||
69986d68fd | |||
7f6439ce7c | |||
41aa639602 | |||
146b33a651 | |||
450f1f6b5d | |||
89d8ac5e58 | |||
16ec46e003 | |||
3115552b87 | |||
cc5208bda0 |
27
.github/workflows/nodejs.yml
vendored
27
.github/workflows/nodejs.yml
vendored
@ -5,27 +5,26 @@ name: Node.js CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [10.x, 12.x]
|
||||
node-version: [12.x, 14.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm install -g yarn
|
||||
- run: yarn install
|
||||
- run: yarn test
|
||||
env:
|
||||
CI: true
|
||||
- uses: actions/checkout@v2
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm install -g yarn
|
||||
- run: yarn install
|
||||
- run: yarn test
|
||||
env:
|
||||
CI: true
|
||||
|
15
.vscode/settings.json
vendored
15
.vscode/settings.json
vendored
@ -19,6 +19,7 @@
|
||||
"lcov",
|
||||
"leetcode",
|
||||
"leetcodeisgreat",
|
||||
"liang",
|
||||
"lrloseumgh",
|
||||
"mincost",
|
||||
"nums",
|
||||
@ -32,7 +33,19 @@
|
||||
"subarrays",
|
||||
"umghlrlose",
|
||||
"xuan",
|
||||
"zhan",
|
||||
"zhong",
|
||||
"zhuan"
|
||||
]
|
||||
],
|
||||
"standard.options": {
|
||||
"globals": [
|
||||
"$",
|
||||
"jQuery",
|
||||
"fetch"
|
||||
],
|
||||
"usePackageJson": true,
|
||||
"env": {
|
||||
"jest": true
|
||||
}
|
||||
}
|
||||
}
|
73
README.md
73
README.md
@ -63,13 +63,13 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 179. 最大数 <https://leetcode-cn.com/problems/largest-number/>
|
||||
- LintCode 184. 最大数 <https://www.lintcode.com/problem/largest-number/description>
|
||||
|
||||
- [实现string2int()函数](src/string/string2int.js)
|
||||
- [实现 string2int()函数](src/string/string2int.js)
|
||||
|
||||
- 面试题参考思路,不严谨实现 廖雪峰 不要使用JavaScript内置的parseInt()函数,利用map和reduce操作实现一个string2int()函数。 <https://www.liaoxuefeng.com/wiki/1022910821149312/1024322552460832>
|
||||
- 面试题参考思路,不严谨实现 廖雪峰 不要使用 JavaScript 内置的 parseInt()函数,利用 map 和 reduce 操作实现一个 string2int()函数。 <https://www.liaoxuefeng.com/wiki/1022910821149312/1024322552460832>
|
||||
|
||||
- [左旋转字符串](src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js)
|
||||
|
||||
- LeetCode 面试题58 - II. 左旋转字符串 <https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/>
|
||||
- LeetCode 面试题 58 - II. 左旋转字符串 <https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/>
|
||||
|
||||
- [最后一个单词的长度](src/string/length-of-last-word.js)
|
||||
|
||||
@ -91,7 +91,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 680. 验证回文字符串 Ⅱ <https://leetcode-cn.com/problems/valid-palindrome-ii/>
|
||||
- LintCode 891. 有效回文 II <https://www.lintcode.com/problem/valid-palindrome-ii/description>
|
||||
|
||||
- [IP地址无效化](src/string/defanging-an-ip-address.js)
|
||||
- [IP 地址无效化](src/string/defanging-an-ip-address.js)
|
||||
|
||||
- LeetCode 1108. IP 地址无效化 <https://leetcode-cn.com/problems/defanging-an-ip-address/>
|
||||
|
||||
@ -268,7 +268,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
|
||||
- [逆序对](src/array/reverse-pairs.js)
|
||||
|
||||
- LeetCode 面试题51. 数组中的逆序对 <https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/>
|
||||
- LeetCode 面试题 51. 数组中的逆序对 <https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/>
|
||||
- LintCode 532. 逆序对 <https://www.lintcode.com/problem/reverse-pairs/description>
|
||||
|
||||
- [搜索旋转排序数组](src/array/search-in-rotated-sorted-array.js)
|
||||
@ -278,7 +278,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
|
||||
- [数组中数字出现的次数](src/array/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof.js)
|
||||
|
||||
- LeetCode 面试题56 - I. 数组中数字出现的次数 <https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/>
|
||||
- LeetCode 面试题 56 - I. 数组中数字出现的次数 <https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/>
|
||||
|
||||
- [只出现一次的数字](src/array/single-number.js)
|
||||
|
||||
@ -298,10 +298,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- [最大子序和](src/array/maximum-subarray.js)
|
||||
|
||||
- LeetCode 53. 最大子序和 <https://leetcode-cn.com/problems/maximum-subarray/>
|
||||
- LeetCode 面试题42. 连续子数组的最大和 <https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/>
|
||||
- LeetCode 面试题 42. 连续子数组的最大和 <https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/>
|
||||
- LintCode 41. 最大子数组 <https://www.lintcode.com/problem/maximum-subarray/description>
|
||||
|
||||
- [跳跃游戏II](src/array/jump-game-ii.js)
|
||||
- [跳跃游戏 II](src/array/jump-game-ii.js)
|
||||
|
||||
- LeetCode 45. 跳跃游戏 II <https://leetcode-cn.com/problems/jump-game-ii/>
|
||||
- LintCode 117. 跳跃游戏 II <https://www.lintcode.com/problem/jump-game-ii/description>
|
||||
@ -325,7 +325,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
|
||||
- [第一个只出现一次的字符](src/array/first-unique-character-in-a-string.js)
|
||||
|
||||
- LeetCode 面试题50. 第一个只出现一次的字符 <https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/>
|
||||
- LeetCode 面试题 50. 第一个只出现一次的字符 <https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/>
|
||||
- LeetCode 387. 字符串中的第一个唯一字符 <https://leetcode-cn.com/problems/first-unique-character-in-a-string/>
|
||||
- LintCode 209. 第一个只出现一次的字符 <https://www.lintcode.com/problem/first-unique-character-in-a-string/description>
|
||||
|
||||
@ -334,26 +334,26 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 152. 乘积最大子数组 <https://leetcode-cn.com/problems/maximum-product-subarray/>
|
||||
- LintCode 191. 乘积最大子序列 <https://www.lintcode.com/problem/maximum-product-subarray/description>
|
||||
|
||||
- [最高频率的IP](src/array/highest-frequency-ip.js)
|
||||
- [最高频率的 IP](src/array/highest-frequency-ip.js)
|
||||
|
||||
- LintCode 1613. 最高频率的IP <https://www.lintcode.com/problem/highest-frequency-ip/description>
|
||||
- LintCode 1613. 最高频率的 IP <https://www.lintcode.com/problem/highest-frequency-ip/description>
|
||||
|
||||
- [柠檬水找零](src/array/lemonade-change.js)
|
||||
|
||||
- LeetCode 860. 柠檬水找零 <https://leetcode-cn.com/problems/lemonade-change/>
|
||||
- LintCode 1509. 柠檬水找零 <https://www.lintcode.com/problem/lemonade-change/description>
|
||||
|
||||
- [LRU缓存机制](src/array/lru-cache.js)
|
||||
- [LRU 缓存机制](src/array/lru-cache.js)
|
||||
|
||||
- LeetCode 146. LRU缓存机制 <https://leetcode-cn.com/problems/lru-cache/>
|
||||
- LintCode 134. LRU缓存策略 <https://www.lintcode.com/problem/lru-cache/description>
|
||||
- LeetCode 146. LRU 缓存机制 <https://leetcode-cn.com/problems/lru-cache/>
|
||||
- LintCode 134. LRU 缓存策略 <https://www.lintcode.com/problem/lru-cache/description>
|
||||
|
||||
- [寻找重复数](src/array/find-the-duplicate-number.js)
|
||||
|
||||
- LeetCode 287. 寻找重复数 <https://leetcode-cn.com/problems/find-the-duplicate-number/>
|
||||
- LintCode 633. 寻找重复的数 <https://www.lintcode.com/problem/find-the-duplicate-number/description>
|
||||
|
||||
- [和可被K整除的子数组](src/array/subarray-sums-divisible-by-k.js)
|
||||
- [和可被 K 整除的子数组](src/array/subarray-sums-divisible-by-k.js)
|
||||
|
||||
- LeetCode 974. 和可被 K 整除的子数组 <https://leetcode-cn.com/problems/subarray-sums-divisible-by-k/>
|
||||
|
||||
@ -376,6 +376,16 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 739. 每日温度 <https://leetcode-cn.com/problems/daily-temperatures/>
|
||||
- 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)
|
||||
@ -388,6 +398,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 155. 最小栈 <https://leetcode-cn.com/problems/min-stack/>
|
||||
- LintCode 12. 带最小值操作的栈 <https://www.lintcode.com/problem/min-stack/description>
|
||||
|
||||
- [用两个栈实现队列](src/stack/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.js)
|
||||
|
||||
- LeetCode 剑指 Offer 09. 用两个栈实现队列 <https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/>
|
||||
|
||||
- [字符串解码](src/stack/decode-string.js)
|
||||
|
||||
- LeetCode 394. 字符串解码 <https://leetcode-cn.com/problems/decode-string/>
|
||||
@ -426,12 +440,12 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- [Pow(x, n)](src/math/powx-n.js)
|
||||
|
||||
- LeetCode 50. Pow(x, n) <https://leetcode-cn.com/problems/powx-n/>
|
||||
- LintCode 428. x的n次幂 <https://www.lintcode.com/problem/powx-n/description>
|
||||
- LintCode 428. x 的 n 次幂 <https://www.lintcode.com/problem/powx-n/description>
|
||||
|
||||
- [和为K的子数组](src/math/subarray-sum-equals-k.js)
|
||||
- [和为 K 的子数组](src/math/subarray-sum-equals-k.js)
|
||||
|
||||
- LeetCode 560. 和为K的子数组 <https://leetcode-cn.com/problems/subarray-sum-equals-k/>
|
||||
- LintCode 838. 子数组和为K <https://www.lintcode.com/problem/subarray-sum-equals-k/description>
|
||||
- LeetCode 560. 和为 K 的子数组 <https://leetcode-cn.com/problems/subarray-sum-equals-k/>
|
||||
- LintCode 838. 子数组和为 K <https://www.lintcode.com/problem/subarray-sum-equals-k/description>
|
||||
|
||||
- [完美数](src/math/perfect-number.js)
|
||||
|
||||
@ -497,17 +511,26 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- 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>
|
||||
|
||||
- [将有序数组转换为二叉搜索树](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)
|
||||
|
||||
- LeetCode 23. 合并K个排序链表 <https://leetcode-cn.com/problems/merge-k-sorted-lists/>
|
||||
- LintCode 104. 合并k个排序链表 <https://www.lintcode.com/problem/merge-k-sorted-lists/description>
|
||||
- LeetCode 23. 合并 K 个排序链表 <https://leetcode-cn.com/problems/merge-k-sorted-lists/>
|
||||
- LintCode 104. 合并 k 个排序链表 <https://www.lintcode.com/problem/merge-k-sorted-lists/description>
|
||||
|
||||
- [合并两个有序链表](src/list/merge-two-sorted-lists.js)
|
||||
|
||||
- LeetCode 21. 合并两个有序链表 <https://leetcode-cn.com/problems/merge-two-sorted-lists/>
|
||||
- LeetCode 面试题25. 合并两个排序的链表 <https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof>
|
||||
- LeetCode 面试题 25. 合并两个排序的链表 <https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof>
|
||||
- LintCode 165. 合并两个排序链表 <https://www.lintcode.com/problem/merge-two-sorted-lists/description>
|
||||
|
||||
- [链表排序](src/list/sort-list.js)
|
||||
@ -525,10 +548,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 876. 链表的中间结点 <https://leetcode-cn.com/problems/middle-of-the-linked-list/>
|
||||
- LintCode 1609. 链表的中间结点 <https://www.lintcode.com/problem/middle-of-the-linked-list/description>
|
||||
|
||||
- [K个一组翻转链表](src/list/reverse-nodes-in-k-group.js)
|
||||
- [K 个一组翻转链表](src/list/reverse-nodes-in-k-group.js)
|
||||
|
||||
- LeetCode 25. K 个一组翻转链表 <https://leetcode-cn.com/problems/reverse-nodes-in-k-group/>
|
||||
- LintCode 450. K组翻转链表 <https://www.lintcode.com/problem/reverse-nodes-in-k-group/description>
|
||||
- LintCode 450. K 组翻转链表 <https://www.lintcode.com/problem/reverse-nodes-in-k-group/description>
|
||||
|
||||
## 图
|
||||
|
||||
|
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
|
||||
}
|
37
src/stack/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.js
Normal file
37
src/stack/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.js
Normal file
@ -0,0 +1,37 @@
|
||||
export var CQueue = function () {
|
||||
this.stackA = []
|
||||
this.stackB = []
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value
|
||||
* @return {void}
|
||||
*/
|
||||
CQueue.prototype.appendTail = function (value) {
|
||||
this.stackA.push(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
CQueue.prototype.deleteHead = function () {
|
||||
const stackBNum = this.stackB.length
|
||||
|
||||
while (this.stackA.length) {
|
||||
this.stackB.push(this.stackA.pop())
|
||||
}
|
||||
|
||||
if (stackBNum !== 0) {
|
||||
this.stackB.push(...this.stackB.splice(0, stackBNum))
|
||||
}
|
||||
|
||||
const tmp = this.stackB.pop()
|
||||
return tmp === undefined ? -1 : tmp
|
||||
}
|
||||
|
||||
/**
|
||||
* Your CQueue object will be instantiated and called as such:
|
||||
* var obj = new CQueue()
|
||||
* obj.appendTail(value)
|
||||
* var param_2 = obj.deleteHead()
|
||||
*/
|
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)
|
||||
})
|
67
test/stack/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.test.js
Normal file
67
test/stack/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.test.js
Normal file
@ -0,0 +1,67 @@
|
||||
import { CQueue } from '../../src/stack/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.js'
|
||||
|
||||
test('用两个栈实现队列 - 1', () => {
|
||||
let obj = null
|
||||
const action = ['CQueue', 'appendTail', 'deleteHead', 'deleteHead']
|
||||
const queue = [[], [3], [], []]
|
||||
const res = []
|
||||
while (queue.length) {
|
||||
const tmp = action.shift()
|
||||
const val = queue.shift()
|
||||
if (tmp === 'CQueue') {
|
||||
obj = new CQueue()
|
||||
res.push(null)
|
||||
} else if (tmp === 'appendTail') {
|
||||
obj.appendTail(val[0])
|
||||
res.push(null)
|
||||
} else if (tmp === 'deleteHead') {
|
||||
res.push(obj.deleteHead())
|
||||
}
|
||||
}
|
||||
|
||||
expect(res).toEqual([null, null, 3, -1])
|
||||
})
|
||||
|
||||
test('用两个栈实现队列 - 2', () => {
|
||||
let obj = null
|
||||
const action = ['CQueue', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead']
|
||||
const queue = [[], [], [5], [2], [], []]
|
||||
const res = []
|
||||
while (queue.length) {
|
||||
const tmp = action.shift()
|
||||
const val = queue.shift()
|
||||
if (tmp === 'CQueue') {
|
||||
obj = new CQueue()
|
||||
res.push(null)
|
||||
} else if (tmp === 'appendTail') {
|
||||
obj.appendTail(val[0])
|
||||
res.push(null)
|
||||
} else if (tmp === 'deleteHead') {
|
||||
res.push(obj.deleteHead())
|
||||
}
|
||||
}
|
||||
|
||||
expect(res).toEqual([null, -1, null, null, 5, 2])
|
||||
})
|
||||
|
||||
test('用两个栈实现队列 - 3', () => {
|
||||
let obj = null
|
||||
const action = ['CQueue', 'deleteHead', 'appendTail', 'deleteHead', 'deleteHead', 'deleteHead', 'deleteHead', 'appendTail', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'deleteHead', 'appendTail', 'deleteHead', 'deleteHead', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'appendTail', 'deleteHead', 'appendTail', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'appendTail', 'deleteHead', 'appendTail', 'appendTail', 'appendTail', 'appendTail', 'deleteHead', 'appendTail', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'deleteHead', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'appendTail', 'deleteHead', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'appendTail', 'deleteHead', 'appendTail', 'appendTail', 'appendTail', 'deleteHead', 'appendTail', 'appendTail', 'appendTail', 'appendTail', 'deleteHead', 'deleteHead', 'deleteHead', 'appendTail', 'deleteHead', 'appendTail', 'deleteHead', 'appendTail', 'appendTail']
|
||||
const queue = [[], [], [97], [], [], [], [], [15], [], [1], [43], [], [], [], [18], [], [], [], [], [36], [69], [21], [91], [], [], [22], [40], [], [], [], [81], [65], [], [77], [], [63], [96], [5], [], [], [35], [90], [], [], [], [], [77], [83], [], [], [52], [], [2], [66], [87], [90], [], [2], [], [], [33], [16], [72], [], [], [14], [78], [8], [], [], [], [], [3], [83], [], [], [13], [], [79], [44], [], [], [33], [], [55], [76], [12], [], [91], [24], [49], [47], [], [], [], [85], [], [69], [], [94], [52]]
|
||||
const res = []
|
||||
while (queue.length) {
|
||||
const tmp = action.shift()
|
||||
const val = queue.shift()
|
||||
if (tmp === 'CQueue') {
|
||||
obj = new CQueue()
|
||||
res.push(null)
|
||||
} else if (tmp === 'appendTail') {
|
||||
obj.appendTail(val[0])
|
||||
res.push(null)
|
||||
} else if (tmp === 'deleteHead') {
|
||||
res.push(obj.deleteHead())
|
||||
}
|
||||
}
|
||||
|
||||
expect(res).toEqual([null, -1, null, 97, -1, -1, -1, null, 15, null, null, 1, 43, -1, null, 18, -1, -1, -1, null, null, null, null, 36, 69, null, null, 21, 91, 22, null, null, 40, null, 81, null, null, null, 65, 77, null, null, 63, 96, 5, 35, null, null, 90, 77, null, 83, null, null, null, null, 52, null, 2, 66, null, null, null, 87, 90, null, null, null, 2, 33, 16, 72, null, null, 14, 78, null, 8, null, null, 3, 83, null, 13, null, null, null, 79, null, null, null, null, 44, 33, 55, null, 76, null, 12, null, null])
|
||||
})
|
@ -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