美文网首页Leetcode
Leetcode 14. Longest Common Pref

Leetcode 14. Longest Common Pref

作者: SnailTyan | 来源:发表于2018-09-10 19:29 被阅读4次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Longest Common Prefix

2. Solution

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string result = "";
        if(strs.size() == 0) {
            return result;
        }
        string pattern = strs[0];
        for(int i = 0; i < pattern.length(); i++) {
            char ch = pattern[i];
            for(string s : strs) {
                if(s[i] != ch) {
                    return result;
                }
            }
            result += ch;
        }
        return result;
    }
};

Reference

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

相关文章

网友评论

    本文标题:Leetcode 14. Longest Common Pref

    本文链接:https://www.haomeiwen.com/subject/kdlegftx.html