leetcode 14 Longest Common Prefix C++实现

leetcode 14 Longest Common Prefix C++实现

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

If there is no common prefix, return an empty string “”.

Example 1:

Input: [“flower”,“flow”,“flight”]
Output: “fl”
Example 2:

Input: [“dog”,“racecar”,“car”]
Output: “”
Explanation: There is no common prefix among the input strings.
Note:

All given inputs are in lowercase letters a-z.

bool cmp(const string& s1, const string& s2)
 {
     return s1.size()& strs) {
        int i,j;
        string result="";
        if(strs.size()==0)
            return "";
        sort(strs.begin(),strs.end(),cmp);//排序,按照字符串的长度大小
        //i代表第i个字符串,j代表字符串中第j个字符,容器中最长的string长度已求得。
        for(j=0;j

你可能感兴趣的:(LeetCode,C++)