[LeetCode]Longest Common Prefix

题目描述:(链接)

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

解题思路:

两两比较。

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string>& strs) {
 4         int len = strs.size();
 5         if (len == 0) {
 6             return string("");
 7         }
 8         string result{strs[0]};
 9         
10         for (int i = 1; i < len; ++i) {
11             int len_c = min(result.size(), strs[i].size());
12             int j = 0;
13             for (; j < len_c; ++j) {
14                 if (result[j] != strs[i][j]) {
15                     break;
16                 }
17             }
18             
19             result = result.substr(0, j);
20         }
21         
22         return result;
23     }
24 };
原文地址:https://www.cnblogs.com/skycore/p/4958136.html