My clear cpp code


  • 0
    C
    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) 
        {
            if(!strs.size()) return "";
            
            int idx = -1;
            string &first_str = strs[0];
            
            bool eq_flag = true;
            while(eq_flag)
            {
                idx++;
                eq_flag = true;
                
                for(const auto& str : strs)
                {
                    if(!str[idx] || str[idx] != first_str[idx])
                    {
                        eq_flag = false;
                        break;
                    }
                }
            }
            
            return string( first_str.begin(), first_str.begin()+idx );
        }
    };

Log in to reply
 

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