leetcode 14. 最长公共前缀
题目:14. 最长公共前缀 - 力扣(LeetCode)
又加班,手机上刷一道水题
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ret = strs[0];
for (int i = 1; i < strs.size(); i++) {
int n = 0;
string s = strs[i];
while (n < ret.length() && n < s.length() && s[n] == ret[n]) {
n++;
}
if (n < ret.length()) {
ret = ret.substr(0, n);
}
}
return ret;
}
};