/
DeC2018
/
0014
Обзор
Документация
Войти
/
DeC2018
/
0014
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
38 строк
1 KB
Den
create main.cpp
22 ноя 2024, 02:45
22 ноя 2024, 02:45
7627de6
Код
Авторство
О чём код?
#include <iostream> #include <vector> #include <string> using namespace std; class Solution { public: string longestCommonPrefix(vector<string>& strs) { if (strs.empty()) return ""; for (int i = 0; i < strs[0].length(); ++i) for (int j = 1; j < strs.size(); ++j) if (i == strs[j].length() || strs[j][i] != strs[0][i]) return strs[0].substr(0, i); return strs[0]; } }; int main() { Solution solution; // Test case 1 std::vector<std::string> strs1 = {"flower", "flow", "flight"}; std::cout << "Test case 1:" << std::endl; std::cout << "Input: [\"flower\", \"flow\", \"flight\"]" << std::endl; std::cout << "Output: \"" << solution.longestCommonPrefix(strs1) << "\"" << std::endl << std::endl; // Test case 2 std::vector<std::string> strs2 = {"dog", "racecar", "car"}; std::cout << "Test case 2:" << std::endl; std::cout << "Input: [\"dog\", \"racecar\", \"car\"]" << std::endl; std::cout << "Output: \"" << solution.longestCommonPrefix(strs2) << "\"" << std::endl; return 0; }