|
|
|
@ -1882,7 +1882,8 @@
|
|
|
|
|
|
|
|
|
|
<h2 id="1021">10.2.1. 查找最左一个元素<a class="headerlink" href="#1021" title="Permanent link">¶</a></h2>
|
|
|
|
|
<div class="admonition question">
|
|
|
|
|
<p class="admonition-title">查找并返回元素 <code>target</code> 在有序数组 <code>nums</code> 中首次出现的索引。若数组中不包含该元素,则返回 <span class="arithmatex">\(-1\)</span> 。数组可能包含重复元素。</p>
|
|
|
|
|
<p class="admonition-title">Question</p>
|
|
|
|
|
<p>给定一个长度为 <span class="arithmatex">\(n\)</span> 的有序数组 <code>nums</code> 。请查找并返回元素 <code>target</code> 在该数组中首次出现的索引。若数组中不包含该元素,则返回 <span class="arithmatex">\(-1\)</span> 。数组可能包含重复元素。</p>
|
|
|
|
|
</div>
|
|
|
|
|
<p>实际上,我们可以仅通过二分查找解决以上问题。方法的整体框架不变,先计算中点索引 <code>m</code> ,再判断 <code>target</code> 和 <code>nums[m]</code> 大小关系:</p>
|
|
|
|
|
<ul>
|
|
|
|
@ -1971,11 +1972,11 @@
|
|
|
|
|
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="k">while</span> <span class="n">i</span> <span class="o"><=</span> <span class="n">j</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="n">m</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span> <span class="c1"># 计算中点索引 m</span>
|
|
|
|
|
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="k">if</span> <span class="n">nums</span><span class="p">[</span><span class="n">m</span><span class="p">]</span> <span class="o"><</span> <span class="n">target</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="n">i</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># 此情况说明 target 在区间 [m+1, j] 中</span>
|
|
|
|
|
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="n">i</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># target 在区间 [m+1, j] 中</span>
|
|
|
|
|
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a> <span class="k">elif</span> <span class="n">nums</span><span class="p">[</span><span class="n">m</span><span class="p">]</span> <span class="o">></span> <span class="n">target</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a> <span class="n">j</span> <span class="o">=</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span> <span class="c1"># 此情况说明 target 在区间 [i, m-1] 中</span>
|
|
|
|
|
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a> <span class="n">j</span> <span class="o">=</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span> <span class="c1"># target 在区间 [i, m-1] 中</span>
|
|
|
|
|
<a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a> <span class="k">else</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a> <span class="n">j</span> <span class="o">=</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span> <span class="c1"># 此情况说明首个小于 target 的元素在区间 [i, m-1] 中</span>
|
|
|
|
|
<a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a> <span class="n">j</span> <span class="o">=</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span> <span class="c1"># 首个小于 target 的元素在区间 [i, m-1] 中</span>
|
|
|
|
|
<a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span> <span class="ow">or</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="n">target</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span> <span class="c1"># 未找到目标元素,返回 -1</span>
|
|
|
|
|
<a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a> <span class="k">return</span> <span class="n">i</span>
|
|
|
|
|