标签:
string CommonPrefix(string str1,string str2) { int len_1=str1.length(); int len_2=str2.length(); int min_len=len_1>len_2 ? len_2:len_1; string s=""; for(int i=0;i<min_len;++i) { if (str1[i]==str2[i]) s+=str1[i]; else break; } return s; } string longestCommonPrefix(vector<string>& strs) { if (strs.size() == 0) return ""; string s=strs[0]; for (int i=1;i<strs.size();++i) { s=CommonPrefix(s,strs[i]); if (s=="") break; } return s; }
LeetCode 14: Longest Common Prefix
标签:
原文地址:http://www.cnblogs.com/acode/p/4543522.html