LeetCode 14 Longest Common Prefix

题目

c++

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        
        if(strs.size()==0)
            return "";
        int i=0;
        string ans="";
        while(true)
        {
            if(i==strs[0].length())
                return ans;
            char s=strs[0][i];
            for(int j=1;j<strs.size();j++)
            {
                if(i==strs[j].length())
                    return ans;
                if(strs[j][i]!=s)
                    return ans;
            }
            ans+=s;
            i++;
        }
        return ans;
    }
};
posted @ 2019-06-22 10:17  Shendu.CC  阅读(85)  评论(0编辑  收藏  举报