
Write a function to find the longest common prefix string amongst an array of strings.
string longestCommonPrefix(vector<string> &strs) { if(strs.size() == 0) return ""; string res = ""; bool prefix = true; for(int i=0; i<strs[0].size(); i++){ for(int j=1; j<strs.size(); j++){ if(i >= strs[j].size() || strs[j][i] != strs[0][i]){ prefix = false; break; } } if(prefix){//初始值i == 0时 res.append(1,strs[0][i]); } else break; } return res;}




近期评论