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

Leetcode Longest Common Prefix

时间:2014-07-05 18:36:22      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   cti   for   io   

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

class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        if(strs.empty()) return "";
        int minLength = strs[0].length();
        for(int i = 0 ; i < strs.size(); ++ i) minLength = min(minLength,(int)strs[i].length());
        bool flag = false;
        int i = 0;
        for(i = 0 ; i < minLength; ++ i){
            char ch = strs[0][i];
            for(int j = 1; j < strs.size(); ++ j){
                if(strs[j][i]!=ch){flag=true;break;}
            }
            if(flag) break;
        }
        return strs[0].substr(0,i);
    }
};

 

Leetcode Longest Common Prefix,布布扣,bubuko.com

Leetcode Longest Common Prefix

标签:style   blog   color   cti   for   io   

原文地址:http://www.cnblogs.com/xiongqiangcs/p/3822809.html

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