38. 外观数列
代码
javascript
// 38. 外观数列:https://leetcode.cn/problems/count-and-say/description/
// 输入:n = 4
// 输出:"1211"
export function countAndSay (n) {
const stringToCountStr = (str) => {
const resList = []
let left = 0
let right = 0
while (right < str.length) {
while (right < str.length && str[right] === str[left]) {
right++
}
resList.push(`${right-left}${str[left]}`)
left = right
}
return resList.join('')
}
if (n === 1) {
return '1'
} else {
return stringToCountStr(countAndSay(n - 1))
}
}
typescript
// 38. 外观数列:https://leetcode.cn/problems/count-and-say/description/
// 输入:n = 4
// 输出:"1211"
export function countAndSay (n: number): string {
const stringToCountStr: (str: string) => string = (str) => {
const resList: string[] = []
let left = 0
let right = 0
while (right < str.length) {
while (right < str.length && str[right] === str[left]) {
right++
}
resList.push(`${right-left}${str[left]}`)
left = right
}
return resList.join('')
}
if (n === 1) {
return '1'
} else {
return stringToCountStr(countAndSay(n - 1))
}
}
测试代码
ts
import { expect, test } from 'vitest'
import { countAndSay } from './typescript.ts'
import { countAndSay as countAndSayJs } from './javascript.js'
test(`countAndSay`, () => {
expect(countAndSay(4)).toBe('1211')
})
test(`countAndSayJs`, () => {
expect(countAndSayJs(4)).toBe('1211')
})