# 最长公共前缀

<p>编写一个函数来查找字符串数组中的最长公共前缀。</p><p>如果不存在公共前缀，返回空字符串 <code>""</code>。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>strs = ["flower","flow","flight"]<strong><br />输出：</strong>"fl"</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>strs = ["dog","racecar","car"]<strong><br />输出：</strong>""<strong><br />解释：</strong>输入不存在公共前缀。</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>0 <= strs.length <= 200</code></li>	<li><code>0 <= strs[i].length <= 200</code></li>	<li><code>strs[i]</code> 仅由小写英文字母组成</li></ul>
<p>以下<span style="color:red">错误</span>的选项是？</p>

## aop

### before

```c
#include <bits/stdc++.h>
using namespace std;
```

### after

```c
int main()
{
    Solution sol;
    string arr[] = {"flower", "flow", "flight"};
    int length1 = sizeof(arr) / sizeof(arr[0]);
    vector<string> strs(arr, arr + length1);
    cout << sol.longestCommonPrefix(strs) << endl;
    return 0;
}
```

## 答案

```c
class Solution
{
public:
    string longestCommonPrefix(vector<string> &strs)
    {
        if (strs.empty())
            return "";
        if (strs.size() == 1)
            return strs[0];
        sort(strs.begin(), strs.end());
        string s1 = strs[0], s2 = strs.back();
        for (int i = 0; i < s1.size(); ++i)
        {
            if (i == s2.size())
                return s1.substr(0, i);
        }
        return s1;
    }
};
```
## 选项


### A

```c
class Solution
{
public:
    string longestCommonPrefix(vector<string> &strs)
    {
        string ans;
        int i, j, flag = 0, n = strs.size();
        if (n == 0)
            return "";
        for (i = 0; i < strs[0].length(); i++)
        {
            char ch = strs[0][i];
            for (j = 1; j < n; j++)
            {
                if (i >= strs[j].length() || ch != strs[j][i])
                {
                    flag = 1;
                    break;
                }
            }
            if (flag)
                break;
            if (j == n)
            {
                string x;
                x = ch;
                ans.append(x);
            }
        }
        return ans;
    }
};
```

### B

```c
class Solution
{
public:
    string longestCommonPrefix(vector<string> &strs)
    {
        int len;
        string str, temp;
        if (strs.size() == 0)
            return "";
        temp = strs[0];
        for (int i = 1; i < strs.size(); i++)
        {
            str = strs[i];
            string p = "";
            if (temp.size() < str.size())
                len = temp.size();
            else
                len = str.size();
            for (int j = 0; j < len; j++)
            {
                if (temp[j] == str[j])
                {
                    p = p + str[j];
                    continue;
                }
                else
                    break;
            }
            temp = p;
        }
        return temp;
    }
};
```

### C

```c
class Solution
{
public:
    string longestCommonPrefix(vector<string> &strs)
    {
        int sLen = strs.size();

        if (sLen == 0)
            return "";

        string dst = "";
        int minLen = 100;
        int minIndex = 0;

        for (int i = 0; i < sLen; ++i)
        {
            int len = strs[i].length();
            if (len < minLen)
            {
                minLen = len;
                minIndex = i;
            }
        }

        for (int i = 0; i < strs[minIndex].length(); ++i)
        {
            set<char> ch;

            for (auto iter : strs)
                ch.insert((iter)[i]);

            int setLens = ch.size();

            if (setLens > 1)
                break;
            if (setLens == 1)
                dst += *ch.begin();
        }

        return dst;
    }
};
```
