首页 > 代码库 > LeetCode 14. Longest Common Prefix

LeetCode 14. Longest Common Prefix

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

让我们在一个字符串中找出所有字符串的共同前缀

 

class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        string result = "";        if (!strs.size())return result;        result = strs[0];        for (auto &e : strs)        {            if (e.size() < result.size())                result = e;        }        for (auto &e : strs)        {            int i = 0;            for (;i < result.size();++i)            {                if (e[i] != result[i])break;            }            result=result.substr(0, i);        }        return result;    }};

 

LeetCode 14. Longest Common Prefix