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

LeetCode:Longest Common Prefix

时间:2015-01-06 12:03:24      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:algorithm   leetcode   

题目描述:

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


代码:

string Solution::longestCommonPrefix(vector<string> &strs)
{
    string result = "";
    if(strs.size() == 0)
        return result;
    if(strs.size() == 1)
        return strs[0];
    for(int i = 0;i < strs[0].size();i++)
    {
        char c = strs[0][i];
        for(int j = 1;j < strs.size();j++)
            if(strs[j-1][i] != strs[j][i])
                return result;
        result = result + c;
    }
    return result;
}


LeetCode:Longest Common Prefix

标签:algorithm   leetcode   

原文地址:http://blog.csdn.net/yao_wust/article/details/42454773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!