标签:
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 prefix=""; 5 if(strs.size()==0) return prefix; 6 7 /** check char by char, for each char, check all the string word **/ 8 for(int k=0; k<strs[0].size(); k++){ 9 int i=1; 10 for(; i<strs.size() && strs[i].size()>k; i++){ 11 if(strs[i][k]!=strs[0][k]) 12 return prefix; 13 } 14 if(i==strs.size()) prefix+=strs[0][k]; 15 } 16 return prefix; 17 } 18 };
[LeetCode] 14. Longest Common Prefix
标签:
原文地址:http://www.cnblogs.com/amadis/p/5926481.html