[leetcode刷题系列]Longest Common Prefix

来源:互联网 发布:mysql 快照备份 编辑:程序博客网 时间:2024/05/03 14:30

模拟题- -

class Solution {public:    string longestCommonPrefix(vector<string> &strs) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if(strs.size() <= 0)            return "";        for(int i = 0; i < strs[0].size(); ++ i){            bool ok = true;            for(int j = 1; j < strs.size() && ok; ++ j)                if(strs[j][i] != strs[0][i])                    ok = false;            if(!ok)                return strs[0].substr(0, i);        }        return strs[0];    }};