首页 > 其他 > 详细

LeetCode 14. Longest Common Prefix

时间:2016-09-19 01:01:13      阅读:258      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/csudanli/p/5883532.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!