14. Longest Common Prefix

来源:互联网 发布:windows 批处理 多进程 编辑:程序博客网 时间:2024/06/01 14:41

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

题意:最长公共前缀

class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        int len = strs.size();        if(len == 0) return "";        string lcp = strs[0];        for(int i = 1; i < len; ++i){            int l1 = lcp.length();            int l2 = strs[i].length();            int j;            for(j = 0; j < l1 && j < l2; ++j){                if(strs[i][j] != lcp[j]){                    lcp = strs[i].substr(0, j);                    break;                }               }            if(j == l2) lcp = strs[i];        }        return lcp;    }};
原创粉丝点击