首页 > 其他 > 详细

Longest Common Prefix

时间:2014-12-04 00:41:26      阅读:313      评论:0      收藏:0      [点我收藏+]
class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        int ind, tail, len, tmp;
        len = strs.size();
        if(len == 0) return "";
        if(len == 1) return strs[0];
        ind = 0;
        for(int i=1;i<len;i++){
            if(strs[i].length() < strs[ind].length()){
                ind = i;
            }
        }
        tmp = tail = strs[ind].length() - 1;
        for(int i=0;i<len;i++){
            int k=0;
            while(k<=tmp){
                if(strs[i][k] == strs[ind][k]){
                    k++;
                }
                else break;
            }
            tmp = k-1;
        }
        return strs[ind].substr(0, tmp+1);
    }
};

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

Longest Common Prefix

原文:http://www.cnblogs.com/code-swan/p/4141311.html

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