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

3 4 5
<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>
F
fix bug  
feilong 已提交
6

7 8 9 10 11 12
<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>
F
fix bug  
feilong 已提交
13

14 15 16 17 18 19
<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>
F
fix bug  
feilong 已提交
20

21 22 23 24 25
<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
每日一练社区 已提交
26
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
27

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

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

每日一练社区's avatar
每日一练社区 已提交
32
```c
33 34 35
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
36

37
### after
F
fix bug  
feilong 已提交
38

每日一练社区's avatar
每日一练社区 已提交
39
```c
40 41 42 43 44 45 46 47 48 49 50
int main()
{
    Solution sol;
    int res;
    res = sol.strStr("hello", "ll");
    cout << res;
    return 0;
}
```

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

每日一练社区's avatar
每日一练社区 已提交
52
```c
53 54 55 56 57 58 59 60 61 62 63
class Solution
{
public:
    int strStr(string haystack, string needle)
    {
        return haystack.find(needle);
    }
};
```
## 选项

F
fix bug  
feilong 已提交
64

65
### A
F
fix bug  
feilong 已提交
66

每日一练社区's avatar
每日一练社区 已提交
67
```c
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 97
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 已提交
98

每日一练社区's avatar
每日一练社区 已提交
99
```c
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 155
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 已提交
156

每日一练社区's avatar
每日一练社区 已提交
157
```c
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 193
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;
    }
};
```