文章目录
原题题目
代码实现(首刷自解)
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;
}
};