【LeetCode】14

Write a function to find the longest common prefix string amongst an array of strings.

Solution:

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string>& strs) {      //runtime:4ms
 4         string str="";
 5         if(strs.empty())return str;
 6         int index=0;
 7         
 8         while(true){    
 9             if(index>=strs[0].size())return str;
10             char c=strs[0][index];
11             for(int i=1;i<strs.size();i++){
12                 if(index>=strs[i].size()||strs[i][index]!=c)return str;
13             }
14             str+=c;
15             index++;
16         }
17         
18     }
19 };
原文地址:https://www.cnblogs.com/irun/p/4714246.html