class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int N = strs.size();
string ans = "";
if(N == 0) return ans;
int loc = 0;
while(true){
if(loc >= strs[0].size()) return ans;
char c = strs[0][loc];
for(int i=1;i<N;i++){
if(loc >= strs[i].size() || c != strs[i][loc]) return ans;
}
ans += c;
loc++;
}
return ans;
}
};
No.131 - LeetCode14
最新推荐文章于 2021-09-13 16:49:45 发布
本文介绍了一种使用C++实现的寻找字符串数组中最长公共前缀的算法。通过迭代比较每个字符串的字符,该算法能高效地找出所有字符串共有的最长前缀。
193

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



