首页 > 代码库 > 14. Longest Common Prefix

14. Longest Common Prefix

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

 

找出,给出的一堆字符串的公共前缀。两个两个比较

class Solution {public:    string getcom(string &a,string &b) {        int m = min(a.size(), b.size());        string t = "";        for (int i = 0; i < m; ++i) {            if (a[i] != b[i]) return t;             else t += a[i];        }        return t;    }    string longestCommonPrefix(vector<string>& strs) {        if (strs.size() == 0) return "";        string s = strs[0];        for (int i = 1; i < strs.size(); ++i) {            s = getcom(s, strs[i]);         }        //cout<<s<<endl;        return s;    }};

 

14. Longest Common Prefix