/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function (strs) {
let longestCommonPrefix = strs[0]; // 将第一个字符串设置为起始值
for (let i = 1; i < strs.length; i++) { // 从 1 开始遍历字符串数组
const current = strs[i];
if (longestCommonPrefix.length > current.length) { // 如果 longestCommonPrefix > current,直接裁剪 longestCommonPrefix
longestCommonPrefix = longestCommonPrefix.substring(0, current.length);
}
// 遍历当前字符串,与当前 longestCommonPrefix 进行对比,如果字符不匹配,则继续裁剪
for (let j = 0; j < current.length; j++) {
if (longestCommonPrefix[j] != current[j]) {
longestCommonPrefix = longestCommonPrefix.substring(0, j);
break;
}
}
}
return longestCommonPrefix;
};