mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-19 06:50:59 +00:00
deploy
This commit is contained in:
@@ -3384,9 +3384,9 @@
|
||||
<ol>
|
||||
<li>计算中点索引 <span class="arithmatex">\(m = \lfloor {(i + j) / 2} \rfloor\)</span> ,其中 <span class="arithmatex">\(\lfloor \space \rfloor\)</span> 表示向下取整操作。</li>
|
||||
<li>判断 <code>nums[m]</code> 和 <code>target</code> 的大小关系,分为三种情况:<ol>
|
||||
<li>当 <code>nums[m] < target</code> 时,说明 <code>target</code> 在区间 <span class="arithmatex">\([m + 1, j]\)</span> 中,因此执行 <span class="arithmatex">\(i = m + 1\)</span> ;</li>
|
||||
<li>当 <code>nums[m] > target</code> 时,说明 <code>target</code> 在区间 <span class="arithmatex">\([i, m - 1]\)</span> 中,因此执行 <span class="arithmatex">\(j = m - 1\)</span> ;</li>
|
||||
<li>当 <code>nums[m] = target</code> 时,说明找到 <code>target</code> ,因此返回索引 <span class="arithmatex">\(m\)</span> ;</li>
|
||||
<li>当 <code>nums[m] < target</code> 时,说明 <code>target</code> 在区间 <span class="arithmatex">\([m + 1, j]\)</span> 中,因此执行 <span class="arithmatex">\(i = m + 1\)</span> 。</li>
|
||||
<li>当 <code>nums[m] > target</code> 时,说明 <code>target</code> 在区间 <span class="arithmatex">\([i, m - 1]\)</span> 中,因此执行 <span class="arithmatex">\(j = m - 1\)</span> 。</li>
|
||||
<li>当 <code>nums[m] = target</code> 时,说明找到 <code>target</code> ,因此返回索引 <span class="arithmatex">\(m\)</span> 。</li>
|
||||
</ol>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
@@ -3396,7 +3396,7 @@
|
||||
<h2 id="1021">10.2.1. 线性方法<a class="headerlink" href="#1021" title="Permanent link">¶</a></h2>
|
||||
<p>为了查找数组中最左边的 <code>target</code> ,我们可以分为两步:</p>
|
||||
<ol>
|
||||
<li>进行二分查找,定位到任意一个 <code>target</code> 的索引,记为 <span class="arithmatex">\(k\)</span> ;</li>
|
||||
<li>进行二分查找,定位到任意一个 <code>target</code> 的索引,记为 <span class="arithmatex">\(k\)</span> 。</li>
|
||||
<li>以索引 <span class="arithmatex">\(k\)</span> 为起始点,向左进行线性遍历,找到最左边的 <code>target</code> 返回即可。</li>
|
||||
</ol>
|
||||
<p><img alt="线性查找最左边的元素" src="../binary_search_edge.assets/binary_search_left_edge_naive.png" /></p>
|
||||
|
||||
@@ -3560,8 +3560,8 @@
|
||||
<h2 id="1032">10.3.2. 哈希查找:以空间换时间<a class="headerlink" href="#1032" title="Permanent link">¶</a></h2>
|
||||
<p>考虑借助一个哈希表,键值对分别为数组元素和元素索引。循环遍历数组,每轮执行:</p>
|
||||
<ol>
|
||||
<li>判断数字 <code>target - nums[i]</code> 是否在哈希表中,若是则直接返回这两个元素的索引;</li>
|
||||
<li>将键值对 <code>nums[i]</code> 和索引 <code>i</code> 添加进哈希表;</li>
|
||||
<li>判断数字 <code>target - nums[i]</code> 是否在哈希表中,若是则直接返回这两个元素的索引。</li>
|
||||
<li>将键值对 <code>nums[i]</code> 和索引 <code>i</code> 添加进哈希表。</li>
|
||||
</ol>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:3"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1"><1></label><label for="__tabbed_2_2"><2></label><label for="__tabbed_2_3"><3></label></div>
|
||||
<div class="tabbed-content">
|
||||
|
||||
Reference in New Issue
Block a user