14. 最长公共前缀
代码
javascript
// 输入:strs = ["flower","flow","flight"]
// 输出:"fl"
export function longestCommonPrefix(strs) {
if (!strs.length) return ''
if (strs.length === 1) return strs[0]
let rI = 0
while (rI < strs[0].length) {
const cur = strs[0][rI]
let flag = true
for (let i = 1; i < strs.length; i++) {
if (strs[i][rI] !== cur) {
flag = false
break
}
}
if (!flag) {
break
} else {
rI++
}
}
return strs[0].slice(0, rI)
}
typescript
// 输入:strs = ["flower","flow","flight"]
// 输出:"fl"
export function longestCommonPrefix(strs: string[]): string {
if (!strs.length) return ''
if (strs.length === 1) return strs[0]
let rI = 0
while (rI < strs[0].length) {
const cur = strs[0][rI]
let flag = true
for (let i = 1; i < strs.length; i++) {
if (strs[i][rI] !== cur) {
flag = false
break
}
}
if (!flag) {
break
} else {
rI++
}
}
return strs[0].slice(0, rI)
}
测试代码
ts
import { expect, test } from 'vitest'
import { longestCommonPrefix } from './typescript.ts'
import { longestCommonPrefix as longestCommonPrefixJs } from './javascript.js'
test(`["flower","flow","flight"] toBe 'fl'`, () => {
expect(longestCommonPrefix(["flower","flow","flight"])).toBe('fl')
})
test(`["flower","flow","flight"] toBe 'fl'`, () => {
expect(longestCommonPrefixJs(["flower","flow","flight"])).toBe('fl')
})