C++ 6ms easy to understand


  • 0
    L
    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) {
            if (strs.empty())
                return "";
            for(int i = 0 ; i < strs[0].size();++i){
                //每个子串的长度
                for(int j = 0 ;j < strs.size();++j){
                    //多少个子串
                    if( !(i < strs[j].size() && strs[0][i] == strs[j][i])){
                        //如果不满足了,就返回当前i
                        return strs[0].substr(0, i);
                    }
                }
            }
            return strs[0];//否则返回strs[0];
        }
    };
    

Log in to reply
 

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