标签:
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 if(strs.size()==0) return ""; 5 6 string prefix=strs[0]; 7 8 for(int i=1;i<strs.size();i++) 9 { 10 if(prefix.size()==0||strs[i].size()==0) return ""; 11 12 13 int len=0; 14 if(prefix.size()<strs[i].size()) 15 len=prefix.size(); 16 else 17 len=strs[i].size(); 18 19 int j; 20 for(j=0;j<len;j++) 21 { 22 if(prefix[j]!=strs[i][j]) 23 break; 24 } 25 26 prefix=prefix.substr(0,j); 27 } 28 29 return prefix; 30 } 31 };
【leetcode】Longest Common Prefix
标签:
原文地址:http://www.cnblogs.com/jawiezhu/p/4521631.html