Leetcode 14. Longest Common Prefix

Posted ximelon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 14. Longest Common Prefix相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/longest-common-prefix/

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

以上是关于Leetcode 14. Longest Common Prefix的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 14. Longest Common Prefix

LeetCode OJ 14Longest Common Prefix

LeetCode:14. Longest Commen Prefix(Easy)

LeetCode OJ 14Longest Common Prefix

14. Longest Common Prefix

Leetcode 14 Longest Common Prefix