class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size() == 0)
return "";
int ans = 0;
for(;;ans++){
if(strs[0].size() == ans)
break;
for(int i = 1;i < strs.size(); i++){
if(strs[i].length() == ans) goto aaa;
if(strs[i][ans] != strs[0][ans])
goto aaa;
}
}
aaa:;
return strs[0].substr(0,ans);
}
};leetcode 14. Longest Common Prefix
最新推荐文章于 2025-12-04 23:36:16 发布
本文介绍了一种寻找字符串数组中最长公共前缀的算法实现。通过比较每个字符串的字符来确定公共前缀的长度,并返回该前缀。此方法适用于多个字符串的前缀查找。
281

被折叠的 条评论
为什么被折叠?



