Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- class Solution {
- public:
- std::string prefix;
- std::string longestCommonPrefix(std::vector<std::string>& strs) {
- std::string prefix = strs[0]; //flower
- for (int i = 0; i < strs.size(); ++i) { //пробегаем циклом по всему вектору
- while (strs[i].find(prefix) != 0) { //flower.поиск_первого_вхождения(flower) != 0
- prefix = prefix.substr(0, prefix.length() - 1); //уменьшаем на единичку префикс
- if (prefix.empty()) {
- return "";
- }
- }
- }
- return prefix;
- }
- void getdata() {
- std::cout << prefix;
- }
- };
- int main() {
- using namespace std;
- Solution sol;
- vector<string> strs = { "flower", "flow", "flight" };
- sol.longestCommonPrefix(strs);
- sol.getdata();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement