mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			30 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你两个字符串 <code>haystack</code> 和 <code>needle</code> ,请你在 <code>haystack</code> 字符串中找出 <code>needle</code> 字符串的第一个匹配项的下标(下标从 0 开始)。如果 <code>needle</code> 不是 <code>haystack</code> 的一部分,则返回  <code>-1</code><strong> </strong>。</p>
 | 
						||
 | 
						||
<p> </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> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= haystack.length, needle.length <= 10<sup>4</sup></code></li>
 | 
						||
	<li><code>haystack</code> 和 <code>needle</code> 仅由小写英文字符组成</li>
 | 
						||
</ul>
 |