14. Longest Common Prefix.cpp

来源:互联网 发布:java 获取日期 编辑:程序博客网 时间:2024/06/03 18:15
/*
Write a function to find the longest common prefix string amongst an array of strings.
*/
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string commo_str;
        if(strs.size()==0) return commo_str;
        
        for(int i = 0; i < strs[0].size() ;i++)
        {      
            for(int j = 0 ; j < strs.size() ; j++)
            {
                if(strs[0][i] != strs[j][i])
                    return commo_str;
            }
            commo_str.push_back(strs[0][i]) ;
        }
        return commo_str;
    }
};