Longest Common Prefix 字符的最长公共前缀

来源:互联网 发布:电子白板软件功能 编辑:程序博客网 时间:2024/05/17 07:46

Longest Common Prefix

 

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.size()==0)            return "";        else if(strs.size()==1)            return strs[0];        else{            string res="";            int i,k=0;            char c=strs[0][0];            while(1)            {                 for(i=0;i<strs.size();i++)                 {                        if(k<strs[i].size()&&strs[i][k]==c)                        {                            if(i==strs.size()-1)                            {                                res+=c;                                k++;                                c=strs[0][k];                            }                        }                        else                        {                            return res;                        }                 }             }         }        }};

0 0
原创粉丝点击