标签:
题目:
Write a function to find the longest common prefix string amongst an array of strings.
题目解答:
求一组字符串的最长公共前缀。使用迭代器数组实现。思路异常的简单。
代码如下:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string res;
int size = strs.size();
if(size <= 0)
{
return res;
}
string::iterator sptr[size];
for(int i = 0;i < size;i++)
{
sptr[i] = strs[i].begin();
}
bool flag = true;
while(*(sptr[0]) != ‘\0‘)
{
for(int i = 1; i < size;i++)
{
if(*(sptr[i]) == *(sptr[i - 1]))
{
continue;
}
else
{
flag = false;
break;
}
}
if(flag)
{
res += *(sptr[0]);
for(int i = 0;i < size;i++)
{
(sptr[i])++;
}
}
else
{
break;
}
}
return res;
}
};
Leetcode题目:Longest Common Prefix
标签:
原文地址:http://www.cnblogs.com/CodingGirl121/p/5477335.html