标签:efi bre byte 最长公共前缀 += length spl interview card
链接:https://leetcode-cn.com/explore/interview/card/bytedance/242/string/1014/
代码:
class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.size() == 0) return ""; if(strs.size() == 1) return strs[0]; int min_length = strs[0].size(); for(int i = 1; i < strs.size(); i++) { if(min_length > strs[i].size()) min_length = strs[i].size(); } int cur = 0; string res_string = ""; // cout << strs.size() << endl; // return ""; while(cur < min_length) { int i = 1; while(i < strs.size()) { if(strs[i][cur] != strs[0][cur]) break; i++; } if(i == strs.size()) { res_string += strs[0][cur]; cur++; } else break; } return res_string; } };
思路:以 s[0]作为模板,在最近不匹配位置停止就好,要注意边界问题。
标签:efi bre byte 最长公共前缀 += length spl interview card
原文地址:https://www.cnblogs.com/FriskyPuppy/p/12907894.html