Longest Common Prefix

来源:互联网 发布:矩阵正规化 编辑:程序博客网 时间:2024/05/22 16:40
Write a function to find the longest common prefix string amongst an array of strings.
求最长公共前缀
class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        string prefix = "";        for(int idx=0; strs.size()>0; prefix+=strs[0][idx], idx++)            for(int i=0; i<strs.size(); i++)                if(idx >= strs[i].size() ||(i > 0 && strs[i][idx] != strs[i-1][idx]))                    return prefix;        return prefix;    }};