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; } };
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: