Longest Common Prefix

Total Accepted: 76601 Total Submissions: 288579 Difficulty: Easy

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

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        int strsSize = strs.size();
        if(strsSize==0){
            return "";
        }
        string prefix;
        int i =0;
        while(true){
            bool isEnd =false;
            for(int j=0;j<strsSize;j++){
                int sSize = strs[j].size();
                if(i>=sSize){
                    isEnd = true;
                    break;
                }
                if(j>0 && strs[j][i]!=strs[0][i]){
                    isEnd = true;
                    break;
                }
                if(j+1==strsSize){
                    prefix.push_back(strs[0][i]);
                }
            }
            if(isEnd) break;
            ++i;
        }
        return prefix;
    }
};
原文地址:https://www.cnblogs.com/zengzy/p/5034949.html