leetcode-14. Longest Common Prefix

    xiaoxiao2021-03-25  189

    leetcode-14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings.

    最长的子序列一定是一个序列的子集,不可能比这个array中任何一个string要长

    class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.empty()) return ""; //IMPORTANT!!! string res = ""; for(int i = 0; i < strs[0].size(); ++i){ char c = strs[0][i]; for(int j = 1; j < strs.size(); ++j){ if(i > strs[j].size() || strs[j][i] != c){ return res; } } res.push_back(c); } return res; } };
    转载请注明原文地址: https://ju.6miu.com/read-1287.html

    最新回复(0)