Easy understand C++ code


  • 0
    W
    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) {
            string res;
            int i = strs.size() - 1;
            int j = 0;
            sort (strs.begin(), strs.end());
            
            if (strs.size() == 0) {
                return res = "";
            } 
            
            while (j < strs[i].size() && strs[0][j] == strs[i][j]) {
                j++;
            }
    
            return res = strs[0].substr(0, j);
        }
    };
    

Log in to reply
 

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