solution.md 4.3 KB
Newer Older
1
# 实现 strStr()
F
fix bug  
feilong 已提交
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<p>实现 <a href="https://baike.baidu.com/item/strstr/811469" target="_blank">strStr()</a> 函数。</p>
<p>给你两个字符串 <code>haystack</code><code>needle</code> ,请你在 <code>haystack</code> 字符串中找出 <code>needle</code>
    字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回  <code>-1</code><strong> </strong></p>
<p> </p>
<p><strong>说明:</strong></p>
<p>当 <code>needle</code> 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。</p>
<p>对于本题而言,当 <code>needle</code> 是空字符串时我们应当返回 0 。这与 C 语言的 <a href="https://baike.baidu.com/item/strstr/811469"
        target="_blank">strstr()</a> 以及 Java 的 <a
        href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)"
        target="_blank">indexOf()</a> 定义相符。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>haystack = "hello", needle = "ll"<strong><br />输出:</strong>2</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>haystack = "aaaaa", needle = "bba"<strong><br />输出:</strong>-1</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>haystack = "", needle = ""<strong><br />输出:</strong>0</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
    <li><code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code></li>
    <li><code>haystack</code><code>needle</code> 仅由小写英文字符组成</li>
</ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
26
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
27

28
## aop
F
fix bug  
feilong 已提交
29

30
### before
F
fix bug  
feilong 已提交
31

32 33 34 35 36
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
37

38 39 40 41 42 43 44 45 46 47 48 49
```cpp
int main()
{
    Solution sol;
    int res;
    res = sol.strStr("hello", "ll");
    cout << res;
    return 0;
}
```

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

51 52 53 54 55 56 57 58 59 60 61 62
```cpp
class Solution
{
public:
    int strStr(string haystack, string needle)
    {
        return haystack.find(needle);
    }
};
```
## 选项

F
fix bug  
feilong 已提交
63

64
### A
F
fix bug  
feilong 已提交
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 93 94 95 96
```cpp
class Solution
{
public:
    int strStr(string haystack, string needle)
    {
        int l1 = haystack.size();
        int l2 = needle.size();
        int i = 0, j = 0;
        while (i < l1 && j < l2)
        {
            if (haystack[i] == needle[j])
            {
                i++;
                j++;
            }
            else
            {
                i = i - j + 1;
                j = 0;
            }
        }
        if (j >= l2)
            return (i - j);
        else
            return -1;
    }
};
```

### B
F
fix bug  
feilong 已提交
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
```cpp
class Solution
{
public:
    vector<int> next(string str)
    {
        vector<int> v;
        v.push_back(-1);
        int i = 0, j = -1;
        while (i < str.size())
        {
            if (j == -1 || str[i] == str[j])
            {
                ++i;
                ++j;
                v.push_back(j);
            }
            else
                j = v[j];
        }
        return v;
    }
    int strStr(string haystack, string needle)
    {
        int i = 0;
        int j = 0;
        int len1 = haystack.size(), len2 = needle.size();
        vector<int> nextptr;
        if (needle.empty())
            return 0;

        nextptr = next(needle);

        while ((i < len1) && (j < len2))
        {

            if ((j == -1) || (haystack[i] == needle[j]))

            {
                i++;
                j++;
            }
            else
            {
                j = nextptr[j];
            }
        }

        if (j == needle.size())
            return i - j;
        else
            return -1;
    }
};
```

### C
F
fix bug  
feilong 已提交
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
```cpp
class Solution
{
public:
    int strStr(string haystack, string needle)
    {
        int flag = 0;
        int i, j;
        int sizeofh = haystack.length();
        int sizeofn = needle.length();
        if (needle == "")
        {
            return 0;
        }
        for (i = 0; i < sizeofh; i++)
        {
            for (j = 0; j < sizeofn; j++)
            {
                if (i + j > sizeofh)
                {
                    break;
                }
                if (haystack[i + j] != needle[j])
                {
                    break;
                }
            }
        }
        if (flag)
        {
            return i;
        }
        else
            return -1;
    }
};
```