题目:
Write a function to find the longest common prefix string amongst an array of strings.
思路:
1.假定第一个是,然后比较、缩减
2.同时比较所有的第i位,直到存在不同或到某字符串末
代码 思路1 c++:
class Solution { public: string longestCommonPrefix(vector<string>& strs) { if (!strs.size()) return ""; string prefix =strs[0]; for (int i = 1; i < strs.size(); i++) { int len = 0; while (prefix[len] == strs[i][len] && len < prefix.size()) len++; prefix = prefix.substr(0,len); } return prefix; } };