码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 14. Longest Common Prefix

时间:2016-09-19 01:03:18      阅读:171      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!