美文网首页
14. Longest Common Prefix

14. Longest Common Prefix

作者: YellowLayne | 来源:发表于2017-06-14 14:46 被阅读0次

    1.描述

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

    2.分析

    3.代码

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

    相关文章

      网友评论

          本文标题:14. Longest Common Prefix

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