Longest Common Prefix

来源:互联网 发布:mac如何打出罗马数字 编辑:程序博客网 时间:2024/05/18 18:47

Write a function to find the longest common prefix string amongst an array of strings.


class Solution {public:    string longestCommonPrefix(vector<string> &strs)    {        string res;        if (strs.empty()) return res;        for (int i = 0; i < strs[0].length(); i++)        {            for (int j = 1; j < strs.size(); j++)                if (i >= strs[j].length() || strs[0][i] != strs[j][i])                    return res;            res.push_back(strs[0][i]);        }        return res;    }};


0 0