Approach
# 枚舉各個長度的字串
Time Complexity
O(n * n)
Space Complexity
O(1)
Code
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string p1 = strs[0];
for (int i = 1 ; i < strs.size() ; i++) {
while (strs[i].find(p1) != 0) {
p1 = p1.substr(0 , p1.length() - 1);
if (p1.empty()) {
return "";
}
}
}
return p1;
}
};