Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
方法一:
1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4 string res; 5 if(strs.size()==0)return res; 6 process(strs,0,strs.size(),res); 7 return res; 8 } 9 void process(vector<string> &strs,int index, int len, string& prefix) 10 { 11 if(index==len-1) 12 { 13 prefix=strs[index]; 14 return; 15 } 16 17 process(strs,index+1,len,prefix); 18 string s1=strs[index]; 19 int i=0; 20 while(i<s1.size()&&i<prefix.size()) 21 { 22 if(s1[i]!=prefix[i])break; 23 else i++; 24 } 25 prefix=s1.substr(0,i); 26 27 } 28 };
http://fisherlei.blogspot.com/2012/12/leetcode-longest-common-prefix.html