LeetCode - Longest Common Prefix

来源:互联网 发布:fastdfs nginx module 编辑:程序博客网 时间:2024/05/16 01:55

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

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