Longest Common Prefix

来源:互联网 发布:网欣软件 中标 编辑:程序博客网 时间:2024/06/01 08:31

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

class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        if(strs.size() == 0)            return "";        else if(strs.size() == 1)            return strs[0];        int current = 0;        while(1){            for(int i = 1; i < strs.size(); i++)            {                if(current >= strs[i].size() || current >= strs[0].size() || strs[i][current] != strs[0][current])                    return strs[0].substr(0,current);            }            current++;         }     }};


0 0