add: 用两个栈实现队列

This commit is contained in:
2020-06-30 18:43:25 +08:00
17 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,5 @@
import { dailyTemperatures } from '../../src/array/daily-temperatures.js'
test('每日温度', () => {
expect(dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73])).toEqual([1, 1, 4, 2, 1, 1, 0, 0])
})

View File

@ -0,0 +1,7 @@
import { rob } from '../../src/array/house-robber'
test('打家劫舍', () => {
expect(rob([1, 2, 3, 1])).toBe(4)
expect(rob([2, 7, 9, 3, 1])).toBe(12)
expect(rob([828, 125, 740, 724, 983, 321, 773, 678, 841, 842, 875, 377, 674, 144, 340, 467, 625, 916, 463, 922, 255, 662, 692, 123, 778, 766, 254, 559, 480, 483, 904, 60, 305, 966, 872, 935, 626, 691, 832, 998, 508, 657, 215, 162, 858, 179, 869, 674, 452, 158, 520, 138, 847, 452, 764, 995, 600, 568, 92, 496, 533, 404, 186, 345, 304, 420, 181, 73, 547, 281, 374, 376, 454, 438, 553, 929, 140, 298, 451, 674, 91, 531, 685, 862, 446, 262, 477, 573, 627, 624, 814, 103, 294, 388])).toBe(29123)
})

View File

@ -0,0 +1,7 @@
import { kidsWithCandies } from '../../src/array/kids-with-the-greatest-number-of-candies'
test('拥有最多糖果的孩子', () => {
expect(kidsWithCandies([2, 3, 5, 1, 3], 3)).toEqual([true, true, true, false, true])
expect(kidsWithCandies([4, 2, 1, 1, 2], 1)).toEqual([true, false, false, false, false])
expect(kidsWithCandies([12, 1, 12], 10)).toEqual([true, false, true])
})

View File

@ -0,0 +1,5 @@
import { productExceptSelf } from '../../src/array/product-of-array-except-self'
test('除自身以外数组的乘积', () => {
expect(productExceptSelf([1, 2, 3, 4])).toEqual([24, 12, 8, 6])
})

View File

@ -0,0 +1,5 @@
import { subarraysDivByK } from '../../src/array/subarray-sums-divisible-by-k'
test('和可被K整除的子数组', () => {
expect(subarraysDivByK([4, 5, 0, -2, -3, 1], 5)).toBe(7)
})

8
test/math/3sum.test.js Normal file
View File

@ -0,0 +1,8 @@
import { threeSum } from '../../src/math/3sum.js'
test('三数之和', () => {
expect(threeSum([-1, 0, 1, 2, -1, -4])).toEqual([
[-1, -1, 2],
[-1, 0, 1]
])
})

View File

@ -0,0 +1,6 @@
import { addBinary } from '../../src/math/add-binary'
test('二进制求和', () => {
expect(addBinary('11', '1')).toEqual('100')
expect(addBinary('1010', '1011')).toEqual('10101')
})

View File

@ -0,0 +1,7 @@
import { decodeString } from '../../src/stack/decode-string'
test('字符串解码', () => {
expect(decodeString('3[a]2[bc]')).toBe('aaabcbc')
expect(decodeString('3[a2[c]]')).toBe('accaccacc')
expect(decodeString('2[abc]3[cd]ef')).toBe('abcabccdcdcdef')
})