leetcode-14. Longest Common Prefix

来源:互联网 发布:tensorflow 关闭会话 编辑:程序博客网 时间:2024/06/07 05:17

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;    }};
0 0
原创粉丝点击