1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/找出字符串中第一个匹配项的下标 [find-the-index-of-the-first-occurrence-in-a-string].html
2023-12-09 18:53:53 +08:00

30 lines
1.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你两个字符串&nbsp;<code>haystack</code><code>needle</code> ,请你在 <code>haystack</code> 字符串中找出 <code>needle</code> 字符串的第一个匹配项的下标(下标从 0 开始)。如果&nbsp;<code>needle</code> 不是 <code>haystack</code> 的一部分,则返回&nbsp; <code>-1</code><strong> </strong></p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>haystack = "sadbutsad", needle = "sad"
<strong>输出:</strong>0
<strong>解释:</strong>"sad" 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 ,所以返回 0 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>haystack = "leetcode", needle = "leeto"
<strong>输出:</strong>-1
<strong>解释:</strong>"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= haystack.length, needle.length &lt;= 10<sup>4</sup></code></li>
<li><code>haystack</code><code>needle</code> 仅由小写英文字符组成</li>
</ul>