class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result = "";
if(strs.size() == 0)
{
return result;
}
for (int i = 0 ; i< strs[0].length();i++)
{
char c = strs[0][i];
bool f = true;
for (int j = 1; j < strs.size(); j++)
{
if (i >= strs[j].length() || strs[j][i] != c)
{
f = false;
break;
}
}
if (f)
{
result += c;
}
else
{
break;
}
}
return result;
}
};
LeetCode 14. 最长公共前缀
最新推荐文章于 2025-04-16 16:34:41 发布