14. Longest Common Prefix

xiaoxiao2021-02-28  49

14. Longest Common Prefix

class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.empty()) return ""; string res=strs[0]; for(int i=1;i<strs.size();i++){ string temp; for(int j=0;j<res.size() && j<strs[i].size();j++){ if(res[j]==strs[i][j]){ temp+=res[j]; } else break; } res=temp; } return res; } };
转载请注明原文地址: https://www.6miu.com/read-2629571.html

最新回复(0)