leetCode 14. Longest Common Prefix 字符串
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
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。