leetcode_c++:Longest Common Prefix(014)

来源:互联网 发布:南昌神起网络 编辑:程序博客网 时间:2024/05/16 09:56

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

求多个字串的最长前缀


算法

O(n^2)


class Solution {  public:      string longestCommonPrefix(vector<string> &strs) {          // IMPORTANT: Please reset any member data you declared, as          // the same Solution instance will be reused for each test case.          if (strs.size() == 0)            return "";          string prefix = strs[0];          for (int i = 1; i < strs.size(); ++i)          {              if (prefix.length() == 0 || strs[i].length() == 0)                 return "";              int len = prefix.length() < strs[i].length() ? prefix.length() : strs[i].length();              int j;              for (j = 0; j < len; ++j)              {                  if (prefix[j] != strs[i][j])                      break;              }              prefix = prefix.substr(0,j);          }          return prefix;      }  };  
0 0