feat: 14_longest_common_prefix

This commit is contained in:
SquidSpirit 2025-02-12 01:03:06 +08:00
parent 691ff44f91
commit 593382dc0a

View File

@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) {
return "";
}
string result = "";
int longestLen = 0;
for (auto &str : strs) {
longestLen = max(longestLen, (int)str.length());
}
for (int i = 0; i < longestLen; i++) {
char currentChar = strs[0][i];
for (auto &str : strs) {
if (currentChar != str[i]) {
return result;
}
}
result.push_back(currentChar);
}
return result;
}
};