首页 > 其他 > 详细

LeetCode:Longest Common Prefix

时间:2015-07-11 20:05:49      阅读:252      评论:0      收藏:0      [点我收藏+]

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

 

Solution:题意要求求取字符串数组的最长公共前缀子串。从位置0开始,对每一个位置比较所有的字符串,直到遇到不匹配的字符串位置

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

 

LeetCode:Longest Common Prefix

原文:http://www.cnblogs.com/xiaoying1245970347/p/4639240.html

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