Longest Common Prefix

来源:互联网 发布:ant design pro 知乎 编辑:程序博客网 时间:2024/05/16 02:42

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

class Solution {public:    string longestCommonPrefix(vector<string> &strs) {        string result;        if(strs.size()==0) return result;        int m=strs.size(),n=strs[0].size();        for(int j=0;j<n;j++){            char temp=strs[0][j];            bool addChar=true;            for(int i=1;i<m;i++){                if(strs[i].size()<=j||strs[i][j]!=temp) {                    addChar=false;                    break;                }            }            if(addChar) result.append(1,temp);            else break;        }        return result;    }};


0 0
原创粉丝点击