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

14.Longest Common Prefix (String)

时间:2015-07-24 06:55:48      阅读:143      评论: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) {
        int vSize = strs.size();
        string result = "";
        if(vSize == 0) return result;
        else if(vSize == 1) return strs[0];
        
        result = strs[0];
        int i,j;
        for(i = 1; i < vSize; i++){
            for(j = 0; j < result.length() && j < strs[i].length(); j++){
                if(strs[i][j] != result[j]) break;
            }
            result = result.substr(0,j); //截取字符串
        }
        return result;
    }
};

 

14.Longest Common Prefix (String)

标签:

原文地址:http://www.cnblogs.com/qionglouyuyu/p/4672304.html

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