Longest Common Prefix
(原题链接:点击打开链接)
Write a function to find the longest common prefix string amongst an array of strings.
Solution:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string longest = "";
if (strs.size() == 0 || strs[0].length() == 0) return longest;
for (int a = 0; a < strs[0].length(); a++)
{
for (int b = 1; b < strs.size(); b++)
{
if (strs[b].length() <= a || strs[b][a] != strs[0][a]) return longest;
}
longest += strs[0][a];
}
return longest;
}
};
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string longest = "";
if (strs.size() == 0 || strs[0].length() == 0) return longest;
for (int a = 0; a < strs[0].length(); a++)
{
for (int b = 1; b < strs.size(); b++)
{
if (strs[b].length() <= a || strs[b][a] != strs[0][a]) return longest;
}
longest += strs[0][a];
}
return longest;
}
};