贪心法求解
class Solution { public: /** * * @param strs string字符串vector * @return string字符串 */ string longestCommonPrefix(vector<string>& strs) { // write code here if(!strs.size()) return ""; sort(strs.begin(),strs.end()); //按照字典序排序得到所有子串顺序 string a = strs.front(), b = strs.back(); int i; for(i = 0;i<a.size()&&a[i]==b[i];i++); return a.substr(0,i); } };