70. 爬楼梯
代码
javascript
// 70. 爬楼梯:https://leetcode.cn/problems/climbing-stairs/description/
// 输入:n = 2
// 输出:2
export function climbingStairs (n) {
let resArr = [0, 1]
for (let idx = 0; idx < n; idx++) {
resArr = [resArr[1], resArr[0] + resArr[1]]
}
return resArr[1]
}
typescript
// 70. 爬楼梯:https://leetcode.cn/problems/climbing-stairs/description/
// 输入:n = 2
// 输出:2
export function climbingStairs (n: number): number {
let resArr = [0, 1]
for (let idx = 0; idx < n; idx++) {
resArr = [resArr[1], resArr[0] + resArr[1]]
}
return resArr[1]
}
测试代码
ts
import { expect, test } from 'vitest'
import { climbingStairs } from './typescript.ts'
import { climbingStairs as climbingStairsJs } from './javascript.js'
test(`climbingStairs`, () => {
expect(climbingStairs(1)).toBe(1)
expect(climbingStairs(2)).toBe(2)
expect(climbingStairs(3)).toBe(3)
expect(climbingStairs(4)).toBe(5)
})
test(`climbingStairsJs`, () => {
expect(climbingStairsJs(1)).toBe(1)
expect(climbingStairsJs(2)).toBe(2)
expect(climbingStairsJs(3)).toBe(3)
expect(climbingStairsJs(4)).toBe(5)
})