Leetcode 14. 最长公共前缀(DAY 141) ---- LeetCode 精选 TOP 面试题
作者:互联网
文章目录
原题题目
代码实现(首刷自解)
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int pos = 0,minpos = INT_MAX;
string ret;
for(const auto& string:strs) minpos = fmin(minpos,string.size());
while(minpos--)
{
char chr = '\0';
for(const auto& str:strs)
{
if(!chr) chr = str[pos];
else
{
if(chr != str[pos]) return ret;
}
}
ret.push_back(chr);
++pos;
}
return ret;
}
};
标签:面试题,minpos,string,141,strs,pos,ret,chr,DAY 来源: https://blog.csdn.net/qq_37500516/article/details/119253942