My 6ms c++ solution easy to understand


  • 0
    H
    public:
        string longestCommonPrefix(vector<string>& strs) {
            string res;
            if (strs.size()==0) return res;
            res=strs[0];
            for (int i=1;i<strs.size();i++) {
                while(strs[i].find(res)!=0) {
                    res.pop_back();
                }
                if (res.size()==0) break;
            }
            return res;
        }
    };

Log in to reply
 

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