C++ 4ms accepted


  • 0
    D
    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) {
        string ans;
        if(strs.empty())
            return ans;
        int length = strs.size();
        int minLength = strs[0].size();
        for(int i = 0; i < length; i++){
            if(strs[i].size() <= minLength)
                minLength = strs[i].size();
        }
        char temp = strs[0][0];
        int i = 0;
        int j = 0;
        for(i = 0; i < minLength; ++i){
            temp = strs[0][i];
            for(j = 0; j < length; ++j){
                if(strs[j][i] != temp){
                    return ans;
                }
            }
    
            ans.push_back(strs[0][i]);
        }
        return ans;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.