Longest Common Prefix

来源:互联网 发布:steam游戏推荐软件 编辑:程序博客网 时间:2024/05/18 09:55

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

Subscribe to see which companies asked this question

class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        string s1;        if(strs.size()==0) return s1;        s1=strs[0];        for(int j=1;j<strs.size();++j)        {            int i=0;            for(;i<s1.size();++i)            {                if(s1[i]!=strs[j][i])                break;            }            // s1=string(s1.begin(),s1.begin()+i);            s1=s1.substr(0,i);        }        return s1;            }};

0 0
原创粉丝点击