LeetCode 14. Longest Common Prefix

来源:互联网 发布:怎么打开淘宝复制链接 编辑:程序博客网 时间:2024/06/06 08:36
class Solution {public:    string longestCommonPrefix(vector<string>& strs) {    map<string,int> mmap;    for(int i = 0;i<strs.size();++i)    {    for(int j = 1;j<=strs[i].size();j++){    string temp = strs[i].substr(0,j);    if(mmap.find(temp) == mmap.end())    mmap.insert(make_pair(temp,1));    else mmap[temp]++;    }    }    int mmax = numeric_limits<int>::min();    string str;    for(auto item:mmap)    {    if(item.second == strs.size())    {    if(mmax < (int)item.first.size())    {    mmax = item.first.size();    str = item.first;    }    }    }    return str;    }};

0 0
原创粉丝点击