add: 只出现一次的数字等

This commit is contained in:
2019-03-10 16:43:04 +08:00
committed by yi-ge
parent 930ff1be11
commit 8239e7afd5
106 changed files with 7978 additions and 0 deletions

View File

@ -0,0 +1,51 @@
import { updateMatrix } from '../../src/array/01-matrix'
test('01 矩阵', () => {
expect(updateMatrix([
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
])).toEqual([
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
])
expect(updateMatrix([
[0, 0, 0],
[0, 1, 0],
[1, 1, 1]
])).toEqual([
[0, 0, 0],
[0, 1, 0],
[1, 2, 1]
])
expect(updateMatrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
])).toEqual([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
])
expect(updateMatrix([
[0, 1, 0, 1, 1],
[1, 1, 0, 0, 1],
[0, 0, 0, 1, 0],
[1, 0, 1, 1, 1],
[1, 0, 0, 0, 1]
])).toEqual([
[0, 1, 0, 1, 2],
[1, 1, 0, 0, 1],
[0, 0, 0, 1, 0],
[1, 0, 1, 1, 1],
[1, 0, 0, 0, 1]
])
})

View File

@ -0,0 +1,9 @@
import maxProfit from '../../src/array/best-time-to-buy-and-sell-stock-ii'
test('maxProfit', () => {
expect(maxProfit([7, 1, 5, 3, 6, 4])).toEqual(7)
expect(maxProfit([1, 2, 3, 4, 5])).toEqual(4)
expect(maxProfit([7, 6, 4, 3, 1])).toEqual(0)
expect(maxProfit([])).toEqual(0)
expect(maxProfit([2, 1, 2, 0, 1])).toEqual(2)
})

View File

@ -0,0 +1,9 @@
import { search } from '../../src/array/binary-search'
test('BinarySearch', () => {
expect(search([-1, 0, 3, 5, 9, 12], 9)).toEqual(4)
expect(search([-1, 0, 3, 5, 9, 12], 2)).toEqual(-1)
expect(search([1, 4, 4, 5, 7, 7, 8, 9, 9, 10], 1)).toEqual(0)
expect(search([1, 2, 3, 3, 4, 5, 10], 3)).toEqual(2)
expect(search([1, 2, 3, 3, 4, 5, 10], 6)).toEqual(-1)
})

View File

@ -0,0 +1,11 @@
import bubbleSort from '../../src/array/bubble-sort'
test('bubbleSort', () => {
expect(bubbleSort([1, 2, 3])).toEqual([1, 2, 3])
expect(bubbleSort([3, 2, 1])).toEqual([1, 2, 3])
expect(bubbleSort([])).toEqual([])
expect(bubbleSort([3, 6, 4, 5, 6, 8])).toEqual([3, 4, 5, 6, 6, 8])
expect(bubbleSort([1, 1, 1])).toEqual([1, 1, 1])
expect(bubbleSort([-1, -10, 3])).toEqual([-10, -1, 3])
expect(bubbleSort([7, 2, 8, 3, 6, 3])).toEqual([2, 3, 3, 6, 7, 8])
})

View File

@ -0,0 +1,7 @@
import canPlaceFlowers from '../../src/array/can-place-flowers'
test('canPlaceFlowers', () => {
expect(canPlaceFlowers([1, 0, 0, 0, 1], 1)).toEqual(true)
expect(canPlaceFlowers([1, 0, 0, 0, 1], 2)).toEqual(false)
expect(canPlaceFlowers([1, 0, 0, 0, 1, 0, 0], 2)).toEqual(true)
})

View File

@ -0,0 +1,7 @@
import circularArrayLoop from '../../src/array/circular-array-loop'
test('circularArrayLoop', () => {
expect(circularArrayLoop([2, -1, 1, 2, 2])).toEqual(true)
expect(circularArrayLoop([-1, 2])).toEqual(false)
expect(circularArrayLoop([-2, 1, -1, -2, -2])).toEqual(false)
})

View File

@ -0,0 +1,7 @@
import { maxArea } from '../../src/array/container-with-most-water'
test('盛最多水的容器', () => {
expect(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])).toBe(49)
expect(maxArea([1, 3, 2])).toBe(2)
expect(maxArea([1, 3, 2, 2])).toBe(4)
})

View File

@ -0,0 +1,7 @@
import { numberOfSubarrays } from '../../src/array/count-number-of-nice-subarrays'
test('统计「优美子数组」', () => {
expect(numberOfSubarrays([1, 1, 2, 1, 1], 3)).toBe(2)
expect(numberOfSubarrays([2, 4, 6], 1)).toBe(0)
expect(numberOfSubarrays([2, 2, 2, 1, 2, 2, 1, 2, 2, 2], 2)).toBe(16)
})

View File

@ -0,0 +1,24 @@
import { includesInStr, getStrCopyByNum, getMaxRepetitions } from '../../src/array/count-the-repetitions'
test('判断从 s2 中删除某些字符是否可以变为 s1', () => {
expect(includesInStr('abc', 'ab')).toBe(1)
expect(includesInStr('acb', 'ab')).toBe(1)
expect(includesInStr('ab', 'ab')).toBe(1)
expect(includesInStr('ac', 'ab')).toBe(0)
expect(includesInStr('aa', 'a')).toBe(2)
expect(includesInStr('aaa', 'a')).toBe(3)
expect(includesInStr('abcabc', 'abb')).toBe(1)
})
test('获取重复字符串', () => {
expect(getStrCopyByNum('abc', 2)).toBe('abcabc')
expect(getStrCopyByNum('abc', 3)).toBe('abcabcabc')
})
test('统计重复个数', () => {
expect(getMaxRepetitions('abc', 4, 'ab', 2)).toBe(2)
expect(getMaxRepetitions('acb', 4, 'ab', 2)).toBe(2)
expect(getMaxRepetitions('abc', 4, 'abb', 2)).toBe(1)
expect(getMaxRepetitions('aaa', 3, 'aa', 1)).toBe(4)
expect(getMaxRepetitions('abccab', 4, 'abc', 2)).toBe(2)
})

View File

@ -0,0 +1,9 @@
import { commonChars } from '../../src/array/find-common-characters'
describe('查找常用字符', () => {
test('查找常用字符', () => {
expect(commonChars(['bella', 'label', 'roller']).sort()).toEqual(['e', 'l', 'l'].sort())
expect(commonChars(['cool', 'lock', 'cook']).sort()).toEqual(['c', 'o'].sort())
expect(commonChars(['acabcddd', 'bcbdbcbd', 'baddbadb', 'cbdddcac', 'aacbcccd', 'ccccddda', 'cababaab', 'addcaccd']).sort()).toEqual([].sort())
})
})

View File

@ -0,0 +1,11 @@
import firstMissingPositive from '../../src/array/first-missing-positive'
test('缺失的第一个正数', () => {
expect(firstMissingPositive([-1])).toBe(1)
expect(firstMissingPositive([0])).toBe(1)
expect(firstMissingPositive([])).toBe(1)
expect(firstMissingPositive([3, 2, -1])).toBe(1)
expect(firstMissingPositive([3, 4, -1, 1])).toBe(2)
expect(firstMissingPositive([1, 2, 0])).toBe(3)
expect(firstMissingPositive([7, 8, 9, 11, 12])).toBe(1)
})

View File

@ -0,0 +1,15 @@
import firstMissingPrime, { isPrinme } from '../../src/array/first-missing-prime-number'
test('是否是素数', () => {
expect(isPrinme(0)).toBe(false)
expect(isPrinme(1)).toBe(false)
expect(isPrinme(3)).toBe(true)
expect(isPrinme(5)).toBe(true)
expect(isPrinme(7)).toBe(true)
expect(isPrinme(6)).toBe(false)
})
test('缺失的第一个素数', () => {
expect(firstMissingPrime([3, 5, 7])).toBe(2)
expect(firstMissingPrime([2, 3, 5, 7, 11, 13, 17, 23, 29])).toBe(19)
})

View File

@ -0,0 +1,7 @@
import grayCode from '../../src/array/gray-code'
test('grayCode', () => {
expect(grayCode(1)).toEqual([0, 1])
expect(grayCode(2)).toEqual([0, 1, 3, 2])
expect(grayCode(0)).toEqual([0])
})

View File

@ -0,0 +1,6 @@
import { canJump } from '../../src/array/jump-game'
test('跳跃游戏', () => {
expect(canJump([2, 3, 1, 1, 4])).toBe(true)
expect(canJump([3, 2, 1, 0, 4])).toBe(false)
})

View File

@ -0,0 +1,6 @@
import findKthLargest from '../../src/array/kth-largest-element-in-an-array'
test('findKthLargest', () => {
expect(findKthLargest([3, 2, 1, 5, 6, 4], 2)).toEqual(5)
expect(findKthLargest([3, 2, 3, 1, 2, 4, 5, 5, 6], 4)).toEqual(4)
})

View File

@ -0,0 +1,5 @@
import letterCombinations from '../../src/array/letter-combinations-of-a-phone-number'
test('letterCombinations', () => {
expect(letterCombinations('23')).toEqual(['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'])
})

View File

@ -0,0 +1,20 @@
import loopAscArray from '../../src/array/loop-asc-array'
test('loopAscArray', () => {
expect(loopAscArray([0, 3, 4, 6, 7], 0)).toEqual(true)
expect(loopAscArray([6, 7, 0, 3, 4], 0)).toEqual(true)
expect(loopAscArray([6, 7, 0, 3, 4], 5)).toEqual(false)
expect(loopAscArray([1, 2, 3, 4, 5], 6)).toEqual(false)
expect(loopAscArray([1, 2, 3, 4, 5], 1)).toEqual(true)
expect(loopAscArray([3, 4, 5, 1, 1, 2], 1)).toEqual(true)
expect(loopAscArray([3, 4, 5, 1, 1, 2], 6)).toEqual(false)
expect(loopAscArray([3, 4, 5, 1, 1, 2], 0)).toEqual(false)
expect(loopAscArray([1, 0, 1, 1, 1], 0)).toEqual(true)
expect(loopAscArray([1, 0, 1, 1, 1], 2)).toEqual(false)
expect(loopAscArray([3], 2)).toEqual(false)
expect(loopAscArray([2], 2)).toEqual(true)
expect(loopAscArray([1, 1, 1], 2)).toEqual(false)
expect(loopAscArray([1, 1, 1], 1)).toEqual(true)
expect(loopAscArray([1, 1, 1, 0], 1)).toEqual(true)
expect(loopAscArray([1, 1, 1, 0], 2)).toEqual(false)
})

View File

@ -0,0 +1,11 @@
import { majorityElement } from '../../src/array/majority-element'
test('主元素', () => {
expect(majorityElement([1, 2, 5, 9, 5, 9, 5, 5, 5])).toBe(5)
expect(majorityElement([1, 1, 1, 1, 2, 2, 2])).toBe(1)
expect(majorityElement([1, 1, 1, 2, 2, 2, 2])).toBe(2)
expect(majorityElement([3, 2])).toBe(-1)
expect(majorityElement([2, 2, 1, 1, 1, 2, 2])).toBe(2)
expect(majorityElement([1])).toBe(1)
expect(majorityElement([2, 2])).toBe(2)
})

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,10 @@
import maximumGap from '../../src/array/maximum-gap'
import inData from './maximum-gap.test.data'
test('最大间距', () => {
expect(maximumGap([3, 6, 9, 1])).toBe(3)
expect(maximumGap([10])).toBe(0)
expect(maximumGap([13, 16, 19, 1])).toBe(12)
expect(maximumGap([1, 3, 100])).toBe(97)
expect(maximumGap(inData)).toBe(2147428092)
})

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,17 @@
import { numIslands } from '../../src/array/number-of-islands'
test('岛屿的个数', () => {
expect(numIslands([
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
])).toBe(3)
expect(numIslands([
[1, 1]
])).toBe(1)
expect(numIslands([])).toBe(0)
})

View File

@ -0,0 +1,6 @@
import partitionArray from '../../src/array/partition-array'
test('loopAscArray', () => {
expect(partitionArray([], 9)).toEqual(0)
expect(partitionArray([3, 2, 2, 1], 2)).toEqual(1)
})

View File

@ -0,0 +1,15 @@
import removeDuplicates from '../../src/array/remove-duplicates-from-sorted-array'
test('removeDuplicates', () => {
const nums = [1, 1, 2]
expect(removeDuplicates(nums)).toBe(2)
expect(nums).toEqual([1, 2])
})
test('removeDuplicates2', () => {
const nums = [1, 2]
expect(removeDuplicates(nums)).toBe(2)
expect(nums).toEqual([1, 2])
})

View File

@ -0,0 +1,29 @@
import {
reversePairs,
merge,
mergeSort,
resetCount
} from '../../src/array/reverse-pairs'
describe('逆序对', () => {
beforeEach(() => {
resetCount()
})
test('归并排序 - 合并左右', () => {
expect(merge([7], [5])).toEqual([5, 7])
expect(merge([2], [3])).toEqual([2, 3])
expect(merge([2, 3], [4, 5])).toEqual([2, 3, 4, 5])
})
test('归并排序', () => {
expect(mergeSort([2, 4, 3, 5])).toEqual([2, 3, 4, 5])
expect(mergeSort([2, 4, 3, 0])).toEqual([0, 2, 3, 4])
})
test('逆序对', () => {
expect(reversePairs([7, 5, 6, 4])).toBe(5)
expect(reversePairs([2, 4, 1, 3, 5])).toBe(3)
expect(reversePairs([1, 2, 3, 4])).toBe(0)
})
})

View File

@ -0,0 +1,6 @@
import { search } from '../../src/array/search-in-rotated-sorted-array'
test('搜索旋转排序数组', () => {
expect(search([4, 5, 6, 7, 0, 1, 2], 0)).toBe(4)
expect(search([4, 5, 6, 7, 0, 1, 2], 3)).toBe(-1)
})

View File

@ -0,0 +1,8 @@
import searchInsert from '../../src/array/search-insert-position'
test('searchInsert', () => {
expect(searchInsert([1, 3, 5, 6], 5)).toEqual(2)
expect(searchInsert([1, 3, 5, 6], 2)).toEqual(1)
expect(searchInsert([1, 3, 5, 6], 7)).toEqual(4)
expect(searchInsert([1, 3, 5, 6], 0)).toEqual(0)
})

View File

@ -0,0 +1,11 @@
import selectSort from '../../src/array/select-sort'
test('selectSort', () => {
expect(selectSort([1, 2, 3])).toEqual([1, 2, 3])
expect(selectSort([3, 2, 1])).toEqual([1, 2, 3])
expect(selectSort([])).toEqual([])
expect(selectSort([3, 6, 4, 5, 6, 8])).toEqual([3, 4, 5, 6, 6, 8])
expect(selectSort([1, 1, 1])).toEqual([1, 1, 1])
expect(selectSort([-1, -10, 3])).toEqual([-10, -1, 3])
expect(selectSort([7, 2, 8, 3, 6, 3])).toEqual([2, 3, 3, 6, 7, 8])
})

View File

@ -0,0 +1,6 @@
import { singleNumbers } from '../../src/array/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof'
test('面试题56 - I. 数组中数字出现的次数', () => {
expect(singleNumbers([4, 1, 4, 6]).some(i => [1, 6].includes(i))).toBe(true)
expect(singleNumbers([1, 2, 10, 4, 1, 4, 3, 3]).some(i => [2, 10].includes(i))).toBe(true)
})

View File

@ -0,0 +1,6 @@
import { singleNumber } from '../../src/array/single-number'
test('只出现一次的数字', () => {
expect(singleNumber([2, 2, 1])).toBe(1)
expect(singleNumber([4, 1, 2, 1, 2])).toBe(4)
})

View File

@ -0,0 +1,5 @@
import sortArrayByParity from '../../src/array/sort-array-by-parity'
test('sortArrayByParity', () => {
expect(sortArrayByParity([4, 2, 5, 7])).toEqual([2, 5, 4, 7])
})

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,10 @@
import hasGroupsSizeX from '../../src/array/x-of-a-kind-in-a-deck-of-cards'
test('hasGroupsSizeX', () => {
expect(hasGroupsSizeX([1, 2, 3, 4, 4, 3, 2, 1])).toEqual(true)
expect(hasGroupsSizeX([1, 1, 1, 2, 2, 2, 3, 3])).toEqual(false)
expect(hasGroupsSizeX([1, 1, 2, 2, 2, 2])).toEqual(true)
expect(hasGroupsSizeX([1])).toEqual(false)
expect(hasGroupsSizeX([1, 1])).toEqual(true)
expect(hasGroupsSizeX([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10])).toEqual(true)
})