1. 程式人生 > >14. Longest Common Prefix

14. Longest Common Prefix

nbsp vector prefix fin else return col end 公共前綴

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