14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

题目大意:求一组字符串的最长前缀。

代码如下:

classSolution{public:stringlongestCommonPrefix(vector<string>&strs){if(strs.size()==0)return"";intminStrLen=strs[0].size();stringresult;for(inti=0;i<strs.size();i++){if(strs[i].size()<minStrLen){minStrLen=strs[i].size();}}inti=0;for(;i<minStrLen;i++){chara=strs[0][i];intj=0;for(;j<strs.size();j++){if(strs[j][i]!=a)break;}if(j<strs.size())break;}result=strs[0].substr(0,i);returnresult;}};


2016-08-10 17:44:00