LeetCode : Longest Common Prefix

来源:互联网 发布:自考还是网络教育 编辑:程序博客网 时间:2024/06/06 09:09

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.empty()) return "";        for (int pos = 0; pos < strs[0].length(); pos++)            for (int i = 1; i < strs.size(); i++)                if (pos >= strs[i].length() || strs[i][pos] != strs[0][pos])                    return strs[0].substr(0, pos);        return strs[0];    }};
0 0
原创粉丝点击