solution.md 3.9 KB
Newer Older
1
# 最长公共前缀
F
fix bug  
feilong 已提交
2

3
<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>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13
```cpp
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
14

15
### after
F
fix bug  
feilong 已提交
16

17 18 19 20 21 22 23 24 25 26 27 28 29
```cpp
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;
}
```

## 答案
F
fix bug  
feilong 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
```cpp
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;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
54

55
### A
F
fix bug  
feilong 已提交
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
```cpp
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
F
fix bug  
feilong 已提交
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
```cpp
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
F
fix bug  
feilong 已提交
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
```cpp
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;
    }
};
```