描述
找出所有字符串的最长公共前缀
解决
遍历所有的字符串
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int length = strs.size();
if (length == 0)
return "";
string res = "";
int now_length = strs[0].size();
int pos = now_length;
for (int i = 0; i < now_length; ++i)
{
bool flag = true;
for (int j = 1; j < length; ++j)
{
if (strs[0][i] != strs[j][i])
{
flag = false;
break;
}
}
if (!flag)
{
pos = i;
break;
}
}
return strs[0].substr(0, pos);
}
};