mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 03:06:29 +08:00
deploy
This commit is contained in:
parent
f4bc77378f
commit
703378cd91
9 changed files with 458 additions and 230 deletions
|
@ -4828,7 +4828,24 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">recursion.dart</span><pre><span></span><code><a id="__codelineno-92-1" name="__codelineno-92-1" href="#__codelineno-92-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">forLoopRecur</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">recursion.dart</span><pre><span></span><code><a id="__codelineno-92-1" name="__codelineno-92-1" href="#__codelineno-92-1"></a><span class="cm">/* 使用迭代模拟递归 */</span>
|
||||
<a id="__codelineno-92-2" name="__codelineno-92-2" href="#__codelineno-92-2"></a><span class="kt">int</span><span class="w"> </span><span class="n">forLoopRecur</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-92-3" name="__codelineno-92-3" href="#__codelineno-92-3"></a><span class="w"> </span><span class="c1">// 使用一个显式的栈来模拟系统调用栈</span>
|
||||
<a id="__codelineno-92-4" name="__codelineno-92-4" href="#__codelineno-92-4"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">stack</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-92-5" name="__codelineno-92-5" href="#__codelineno-92-5"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-92-6" name="__codelineno-92-6" href="#__codelineno-92-6"></a><span class="w"> </span><span class="c1">// 递:递归调用</span>
|
||||
<a id="__codelineno-92-7" name="__codelineno-92-7" href="#__codelineno-92-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">--</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-92-8" name="__codelineno-92-8" href="#__codelineno-92-8"></a><span class="w"> </span><span class="c1">// 通过“入栈操作”模拟“递”</span>
|
||||
<a id="__codelineno-92-9" name="__codelineno-92-9" href="#__codelineno-92-9"></a><span class="w"> </span><span class="n">stack</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
|
||||
<a id="__codelineno-92-10" name="__codelineno-92-10" href="#__codelineno-92-10"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-92-11" name="__codelineno-92-11" href="#__codelineno-92-11"></a><span class="w"> </span><span class="c1">// 归:返回结果</span>
|
||||
<a id="__codelineno-92-12" name="__codelineno-92-12" href="#__codelineno-92-12"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">stack</span><span class="p">.</span><span class="n">isEmpty</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-92-13" name="__codelineno-92-13" href="#__codelineno-92-13"></a><span class="w"> </span><span class="c1">// 通过“出栈操作”模拟“归”</span>
|
||||
<a id="__codelineno-92-14" name="__codelineno-92-14" href="#__codelineno-92-14"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">stack</span><span class="p">.</span><span class="n">removeLast</span><span class="p">();</span>
|
||||
<a id="__codelineno-92-15" name="__codelineno-92-15" href="#__codelineno-92-15"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-92-16" name="__codelineno-92-16" href="#__codelineno-92-16"></a><span class="w"> </span><span class="c1">// res = 1+2+3+...+n</span>
|
||||
<a id="__codelineno-92-17" name="__codelineno-92-17" href="#__codelineno-92-17"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-92-18" name="__codelineno-92-18" href="#__codelineno-92-18"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -3630,9 +3630,35 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">climbing_stairs_backtrack.c</span><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">backtrack</span><span class="p">}</span>
|
||||
<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a>
|
||||
<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">climbingStairsBacktrack</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">climbing_stairs_backtrack.c</span><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="cm">/* 回溯 */</span>
|
||||
<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">backtrack</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">choices</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">state</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">len</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="w"> </span><span class="c1">// 当爬到第 n 阶时,方案数量加 1</span>
|
||||
<a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">state</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
|
||||
<a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a><span class="w"> </span><span class="n">res</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-10-6" name="__codelineno-10-6" href="#__codelineno-10-6"></a><span class="w"> </span><span class="c1">// 遍历所有选择</span>
|
||||
<a id="__codelineno-10-7" name="__codelineno-10-7" href="#__codelineno-10-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-10-8" name="__codelineno-10-8" href="#__codelineno-10-8"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">choice</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
|
||||
<a id="__codelineno-10-9" name="__codelineno-10-9" href="#__codelineno-10-9"></a><span class="w"> </span><span class="c1">// 剪枝:不允许越过第 n 阶</span>
|
||||
<a id="__codelineno-10-10" name="__codelineno-10-10" href="#__codelineno-10-10"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">state</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">choice</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
|
||||
<a id="__codelineno-10-11" name="__codelineno-10-11" href="#__codelineno-10-11"></a><span class="w"> </span><span class="k">break</span><span class="p">;</span>
|
||||
<a id="__codelineno-10-12" name="__codelineno-10-12" href="#__codelineno-10-12"></a><span class="w"> </span><span class="c1">// 尝试:做出选择,更新状态</span>
|
||||
<a id="__codelineno-10-13" name="__codelineno-10-13" href="#__codelineno-10-13"></a><span class="w"> </span><span class="n">backtrack</span><span class="p">(</span><span class="n">choices</span><span class="p">,</span><span class="w"> </span><span class="n">state</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">choice</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">len</span><span class="p">);</span>
|
||||
<a id="__codelineno-10-14" name="__codelineno-10-14" href="#__codelineno-10-14"></a><span class="w"> </span><span class="c1">// 回退</span>
|
||||
<a id="__codelineno-10-15" name="__codelineno-10-15" href="#__codelineno-10-15"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-10-16" name="__codelineno-10-16" href="#__codelineno-10-16"></a><span class="p">}</span>
|
||||
<a id="__codelineno-10-17" name="__codelineno-10-17" href="#__codelineno-10-17"></a>
|
||||
<a id="__codelineno-10-18" name="__codelineno-10-18" href="#__codelineno-10-18"></a><span class="cm">/* 爬楼梯:回溯 */</span>
|
||||
<a id="__codelineno-10-19" name="__codelineno-10-19" href="#__codelineno-10-19"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsBacktrack</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-10-20" name="__codelineno-10-20" href="#__codelineno-10-20"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">choices</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">};</span><span class="w"> </span><span class="c1">// 可选择向上爬 1 或 2 阶</span>
|
||||
<a id="__codelineno-10-21" name="__codelineno-10-21" href="#__codelineno-10-21"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">state</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// 从第 0 阶开始爬</span>
|
||||
<a id="__codelineno-10-22" name="__codelineno-10-22" href="#__codelineno-10-22"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
||||
<a id="__codelineno-10-23" name="__codelineno-10-23" href="#__codelineno-10-23"></a><span class="w"> </span><span class="o">*</span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// 使用 res[0] 记录方案数量</span>
|
||||
<a id="__codelineno-10-24" name="__codelineno-10-24" href="#__codelineno-10-24"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">choices</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">);</span>
|
||||
<a id="__codelineno-10-25" name="__codelineno-10-25" href="#__codelineno-10-25"></a><span class="w"> </span><span class="n">backtrack</span><span class="p">(</span><span class="n">choices</span><span class="p">,</span><span class="w"> </span><span class="n">state</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">len</span><span class="p">);</span>
|
||||
<a id="__codelineno-10-26" name="__codelineno-10-26" href="#__codelineno-10-26"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-10-27" name="__codelineno-10-27" href="#__codelineno-10-27"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">res</span><span class="p">);</span>
|
||||
<a id="__codelineno-10-28" name="__codelineno-10-28" href="#__codelineno-10-28"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">result</span><span class="p">;</span>
|
||||
<a id="__codelineno-10-29" name="__codelineno-10-29" href="#__codelineno-10-29"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -3854,9 +3880,20 @@ dp[i] = dp[i-1] + dp[i-2]
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dfs.c</span><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">dfs</span><span class="p">}</span>
|
||||
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a>
|
||||
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">climbingStairsDFS</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dfs.c</span><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="cm">/* 搜索 */</span>
|
||||
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">dfs</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a><span class="w"> </span><span class="c1">// 已知 dp[1] 和 dp[2] ,返回之</span>
|
||||
<a id="__codelineno-22-4" name="__codelineno-22-4" href="#__codelineno-22-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
|
||||
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
||||
<a id="__codelineno-22-6" name="__codelineno-22-6" href="#__codelineno-22-6"></a><span class="w"> </span><span class="c1">// dp[i] = dp[i-1] + dp[i-2]</span>
|
||||
<a id="__codelineno-22-7" name="__codelineno-22-7" href="#__codelineno-22-7"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>
|
||||
<a id="__codelineno-22-8" name="__codelineno-22-8" href="#__codelineno-22-8"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">count</span><span class="p">;</span>
|
||||
<a id="__codelineno-22-9" name="__codelineno-22-9" href="#__codelineno-22-9"></a><span class="p">}</span>
|
||||
<a id="__codelineno-22-10" name="__codelineno-22-10" href="#__codelineno-22-10"></a>
|
||||
<a id="__codelineno-22-11" name="__codelineno-22-11" href="#__codelineno-22-11"></a><span class="cm">/* 爬楼梯:搜索 */</span>
|
||||
<a id="__codelineno-22-12" name="__codelineno-22-12" href="#__codelineno-22-12"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsDFS</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-22-13" name="__codelineno-22-13" href="#__codelineno-22-13"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
|
||||
<a id="__codelineno-22-14" name="__codelineno-22-14" href="#__codelineno-22-14"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4133,9 +4170,32 @@ dp[i] = dp[i-1] + dp[i-2]
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dfs_mem.c</span><pre><span></span><code><a id="__codelineno-34-1" name="__codelineno-34-1" href="#__codelineno-34-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">dfs</span><span class="p">}</span>
|
||||
<a id="__codelineno-34-2" name="__codelineno-34-2" href="#__codelineno-34-2"></a>
|
||||
<a id="__codelineno-34-3" name="__codelineno-34-3" href="#__codelineno-34-3"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">climbingStairsDFSMem</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dfs_mem.c</span><pre><span></span><code><a id="__codelineno-34-1" name="__codelineno-34-1" href="#__codelineno-34-1"></a><span class="cm">/* 记忆化搜索 */</span>
|
||||
<a id="__codelineno-34-2" name="__codelineno-34-2" href="#__codelineno-34-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">dfs</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">mem</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-34-3" name="__codelineno-34-3" href="#__codelineno-34-3"></a><span class="w"> </span><span class="c1">// 已知 dp[1] 和 dp[2] ,返回之</span>
|
||||
<a id="__codelineno-34-4" name="__codelineno-34-4" href="#__codelineno-34-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
|
||||
<a id="__codelineno-34-5" name="__codelineno-34-5" href="#__codelineno-34-5"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
||||
<a id="__codelineno-34-6" name="__codelineno-34-6" href="#__codelineno-34-6"></a><span class="w"> </span><span class="c1">// 若存在记录 dp[i] ,则直接返回之</span>
|
||||
<a id="__codelineno-34-7" name="__codelineno-34-7" href="#__codelineno-34-7"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">mem</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">-1</span><span class="p">)</span>
|
||||
<a id="__codelineno-34-8" name="__codelineno-34-8" href="#__codelineno-34-8"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">mem</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
|
||||
<a id="__codelineno-34-9" name="__codelineno-34-9" href="#__codelineno-34-9"></a><span class="w"> </span><span class="c1">// dp[i] = dp[i-1] + dp[i-2]</span>
|
||||
<a id="__codelineno-34-10" name="__codelineno-34-10" href="#__codelineno-34-10"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">mem</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">mem</span><span class="p">);</span>
|
||||
<a id="__codelineno-34-11" name="__codelineno-34-11" href="#__codelineno-34-11"></a><span class="w"> </span><span class="c1">// 记录 dp[i]</span>
|
||||
<a id="__codelineno-34-12" name="__codelineno-34-12" href="#__codelineno-34-12"></a><span class="w"> </span><span class="n">mem</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">count</span><span class="p">;</span>
|
||||
<a id="__codelineno-34-13" name="__codelineno-34-13" href="#__codelineno-34-13"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">count</span><span class="p">;</span>
|
||||
<a id="__codelineno-34-14" name="__codelineno-34-14" href="#__codelineno-34-14"></a><span class="p">}</span>
|
||||
<a id="__codelineno-34-15" name="__codelineno-34-15" href="#__codelineno-34-15"></a>
|
||||
<a id="__codelineno-34-16" name="__codelineno-34-16" href="#__codelineno-34-16"></a><span class="cm">/* 爬楼梯:记忆化搜索 */</span>
|
||||
<a id="__codelineno-34-17" name="__codelineno-34-17" href="#__codelineno-34-17"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsDFSMem</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-34-18" name="__codelineno-34-18" href="#__codelineno-34-18"></a><span class="w"> </span><span class="c1">// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录</span>
|
||||
<a id="__codelineno-34-19" name="__codelineno-34-19" href="#__codelineno-34-19"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">mem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">((</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
||||
<a id="__codelineno-34-20" name="__codelineno-34-20" href="#__codelineno-34-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-34-21" name="__codelineno-34-21" href="#__codelineno-34-21"></a><span class="w"> </span><span class="n">mem</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
|
||||
<a id="__codelineno-34-22" name="__codelineno-34-22" href="#__codelineno-34-22"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-34-23" name="__codelineno-34-23" href="#__codelineno-34-23"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">mem</span><span class="p">);</span>
|
||||
<a id="__codelineno-34-24" name="__codelineno-34-24" href="#__codelineno-34-24"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">mem</span><span class="p">);</span>
|
||||
<a id="__codelineno-34-25" name="__codelineno-34-25" href="#__codelineno-34-25"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">result</span><span class="p">;</span>
|
||||
<a id="__codelineno-34-26" name="__codelineno-34-26" href="#__codelineno-34-26"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4353,7 +4413,23 @@ dp[i] = dp[i-1] + dp[i-2]
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dp.c</span><pre><span></span><code><a id="__codelineno-46-1" name="__codelineno-46-1" href="#__codelineno-46-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">climbingStairsDP</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dp.c</span><pre><span></span><code><a id="__codelineno-46-1" name="__codelineno-46-1" href="#__codelineno-46-1"></a><span class="cm">/* 爬楼梯:动态规划 */</span>
|
||||
<a id="__codelineno-46-2" name="__codelineno-46-2" href="#__codelineno-46-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsDP</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-46-3" name="__codelineno-46-3" href="#__codelineno-46-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
|
||||
<a id="__codelineno-46-4" name="__codelineno-46-4" href="#__codelineno-46-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
|
||||
<a id="__codelineno-46-5" name="__codelineno-46-5" href="#__codelineno-46-5"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
|
||||
<a id="__codelineno-46-6" name="__codelineno-46-6" href="#__codelineno-46-6"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">dp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">((</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
||||
<a id="__codelineno-46-7" name="__codelineno-46-7" href="#__codelineno-46-7"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
|
||||
<a id="__codelineno-46-8" name="__codelineno-46-8" href="#__codelineno-46-8"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||
<a id="__codelineno-46-9" name="__codelineno-46-9" href="#__codelineno-46-9"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
|
||||
<a id="__codelineno-46-10" name="__codelineno-46-10" href="#__codelineno-46-10"></a><span class="w"> </span><span class="c1">// 状态转移:从较小子问题逐步求解较大子问题</span>
|
||||
<a id="__codelineno-46-11" name="__codelineno-46-11" href="#__codelineno-46-11"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-46-12" name="__codelineno-46-12" href="#__codelineno-46-12"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">2</span><span class="p">];</span>
|
||||
<a id="__codelineno-46-13" name="__codelineno-46-13" href="#__codelineno-46-13"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-46-14" name="__codelineno-46-14" href="#__codelineno-46-14"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="n">n</span><span class="p">];</span>
|
||||
<a id="__codelineno-46-15" name="__codelineno-46-15" href="#__codelineno-46-15"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">dp</span><span class="p">);</span>
|
||||
<a id="__codelineno-46-16" name="__codelineno-46-16" href="#__codelineno-46-16"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">result</span><span class="p">;</span>
|
||||
<a id="__codelineno-46-17" name="__codelineno-46-17" href="#__codelineno-46-17"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4538,7 +4614,18 @@ dp[i] = dp[i-1] + dp[i-2]
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dp.c</span><pre><span></span><code><a id="__codelineno-58-1" name="__codelineno-58-1" href="#__codelineno-58-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">climbingStairsDPComp</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">climbing_stairs_dp.c</span><pre><span></span><code><a id="__codelineno-58-1" name="__codelineno-58-1" href="#__codelineno-58-1"></a><span class="cm">/* 爬楼梯:空间优化后的动态规划 */</span>
|
||||
<a id="__codelineno-58-2" name="__codelineno-58-2" href="#__codelineno-58-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsDPComp</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-58-3" name="__codelineno-58-3" href="#__codelineno-58-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
|
||||
<a id="__codelineno-58-4" name="__codelineno-58-4" href="#__codelineno-58-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
|
||||
<a id="__codelineno-58-5" name="__codelineno-58-5" href="#__codelineno-58-5"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
|
||||
<a id="__codelineno-58-6" name="__codelineno-58-6" href="#__codelineno-58-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-58-7" name="__codelineno-58-7" href="#__codelineno-58-7"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
|
||||
<a id="__codelineno-58-8" name="__codelineno-58-8" href="#__codelineno-58-8"></a><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
|
||||
<a id="__codelineno-58-9" name="__codelineno-58-9" href="#__codelineno-58-9"></a><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="p">;</span>
|
||||
<a id="__codelineno-58-10" name="__codelineno-58-10" href="#__codelineno-58-10"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-58-11" name="__codelineno-58-11" href="#__codelineno-58-11"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
|
||||
<a id="__codelineno-58-12" name="__codelineno-58-12" href="#__codelineno-58-12"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -3391,7 +3391,7 @@
|
|||
<!-- Page content -->
|
||||
<h1 id="93">9.3 图的遍历<a class="headerlink" href="#93" title="Permanent link">¶</a></h1>
|
||||
<p>树代表的是“一对多”的关系,而图则具有更高的自由度,可以表示任意的“多对多”关系。因此,我们可以把树看作是图的一种特例。显然,<strong>树的遍历操作也是图的遍历操作的一种特例</strong>。</p>
|
||||
<p>图和树都都需要应用搜索算法来实现遍历操作。图的遍历方式可分为两种:「广度优先遍历 breadth-first traversal」和「深度优先遍历 depth-first traversal」。它们也常被称为「广度优先搜索 breadth-first search」和「深度优先搜索 depth-first search」,简称 BFS 和 DFS 。</p>
|
||||
<p>图和树都需要应用搜索算法来实现遍历操作。图的遍历方式可分为两种:「广度优先遍历 breadth-first traversal」和「深度优先遍历 depth-first traversal」。它们也常被称为「广度优先搜索 breadth-first search」和「深度优先搜索 depth-first search」,简称 BFS 和 DFS 。</p>
|
||||
<h2 id="931">9.3.1 广度优先遍历<a class="headerlink" href="#931" title="Permanent link">¶</a></h2>
|
||||
<p><strong>广度优先遍历是一种由近及远的遍历方式,从某个节点出发,始终优先访问距离最近的顶点,并一层层向外扩张</strong>。如图 9-9 所示,从左上角顶点出发,先遍历该顶点的所有邻接顶点,然后遍历下一个顶点的所有邻接顶点,以此类推,直至所有顶点访问完毕。</p>
|
||||
<p><a class="glightbox" href="../graph_traversal.assets/graph_bfs.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="图的广度优先遍历" src="../graph_traversal.assets/graph_bfs.png" /></a></p>
|
||||
|
|
|
@ -5711,120 +5711,125 @@
|
|||
<div class="highlight"><span class="filename">hash_map_open_addressing.dart</span><pre><span></span><code><a id="__codelineno-20-1" name="__codelineno-20-1" href="#__codelineno-20-1"></a><span class="cm">/* 开放寻址哈希表 */</span>
|
||||
<a id="__codelineno-20-2" name="__codelineno-20-2" href="#__codelineno-20-2"></a><span class="kd">class</span><span class="w"> </span><span class="nc">HashMapOpenAddressing</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-3" name="__codelineno-20-3" href="#__codelineno-20-3"></a><span class="w"> </span><span class="kd">late</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_size</span><span class="p">;</span><span class="w"> </span><span class="c1">// 键值对数量</span>
|
||||
<a id="__codelineno-20-4" name="__codelineno-20-4" href="#__codelineno-20-4"></a><span class="w"> </span><span class="kd">late</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span><span class="w"> </span><span class="c1">// 哈希表容量</span>
|
||||
<a id="__codelineno-20-5" name="__codelineno-20-5" href="#__codelineno-20-5"></a><span class="w"> </span><span class="kd">late</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">_loadThres</span><span class="p">;</span><span class="w"> </span><span class="c1">// 触发扩容的负载因子阈值</span>
|
||||
<a id="__codelineno-20-6" name="__codelineno-20-6" href="#__codelineno-20-6"></a><span class="w"> </span><span class="kd">late</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_extendRatio</span><span class="p">;</span><span class="w"> </span><span class="c1">// 扩容倍数</span>
|
||||
<a id="__codelineno-20-4" name="__codelineno-20-4" href="#__codelineno-20-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_capacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span><span class="p">;</span><span class="w"> </span><span class="c1">// 哈希表容量</span>
|
||||
<a id="__codelineno-20-5" name="__codelineno-20-5" href="#__codelineno-20-5"></a><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">_loadThres</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2.0</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="m">3.0</span><span class="p">;</span><span class="w"> </span><span class="c1">// 触发扩容的负载因子阈值</span>
|
||||
<a id="__codelineno-20-6" name="__codelineno-20-6" href="#__codelineno-20-6"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_extendRatio</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 扩容倍数</span>
|
||||
<a id="__codelineno-20-7" name="__codelineno-20-7" href="#__codelineno-20-7"></a><span class="w"> </span><span class="kd">late</span><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Pair</span><span class="o">?></span><span class="w"> </span><span class="n">_buckets</span><span class="p">;</span><span class="w"> </span><span class="c1">// 桶数组</span>
|
||||
<a id="__codelineno-20-8" name="__codelineno-20-8" href="#__codelineno-20-8"></a><span class="w"> </span><span class="kd">late</span><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="n">_removed</span><span class="p">;</span><span class="w"> </span><span class="c1">// 删除标记</span>
|
||||
<a id="__codelineno-20-8" name="__codelineno-20-8" href="#__codelineno-20-8"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">(</span><span class="o">-</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"-1"</span><span class="p">);</span><span class="w"> </span><span class="c1">// 删除标记</span>
|
||||
<a id="__codelineno-20-9" name="__codelineno-20-9" href="#__codelineno-20-9"></a>
|
||||
<a id="__codelineno-20-10" name="__codelineno-20-10" href="#__codelineno-20-10"></a><span class="w"> </span><span class="cm">/* 构造方法 */</span>
|
||||
<a id="__codelineno-20-11" name="__codelineno-20-11" href="#__codelineno-20-11"></a><span class="w"> </span><span class="n">HashMapOpenAddressing</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-12" name="__codelineno-20-12" href="#__codelineno-20-12"></a><span class="w"> </span><span class="n">_size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-13" name="__codelineno-20-13" href="#__codelineno-20-13"></a><span class="w"> </span><span class="n">_capacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-14" name="__codelineno-20-14" href="#__codelineno-20-14"></a><span class="w"> </span><span class="n">_loadThres</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2.0</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="m">3.0</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-15" name="__codelineno-20-15" href="#__codelineno-20-15"></a><span class="w"> </span><span class="n">_extendRatio</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-16" name="__codelineno-20-16" href="#__codelineno-20-16"></a><span class="w"> </span><span class="n">_buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">generate</span><span class="p">(</span><span class="n">_capacity</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="kc">null</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-17" name="__codelineno-20-17" href="#__codelineno-20-17"></a><span class="w"> </span><span class="n">_removed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Pair</span><span class="p">(</span><span class="o">-</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"-1"</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-18" name="__codelineno-20-18" href="#__codelineno-20-18"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-19" name="__codelineno-20-19" href="#__codelineno-20-19"></a>
|
||||
<a id="__codelineno-20-20" name="__codelineno-20-20" href="#__codelineno-20-20"></a><span class="w"> </span><span class="cm">/* 哈希函数 */</span>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-22" name="__codelineno-20-22" href="#__codelineno-20-22"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-23" name="__codelineno-20-23" href="#__codelineno-20-23"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-24" name="__codelineno-20-24" href="#__codelineno-20-24"></a>
|
||||
<a id="__codelineno-20-25" name="__codelineno-20-25" href="#__codelineno-20-25"></a><span class="w"> </span><span class="cm">/* 负载因子 */</span>
|
||||
<a id="__codelineno-20-26" name="__codelineno-20-26" href="#__codelineno-20-26"></a><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">loadFactor</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-27" name="__codelineno-20-27" href="#__codelineno-20-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">_size</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-28" name="__codelineno-20-28" href="#__codelineno-20-28"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-29" name="__codelineno-20-29" href="#__codelineno-20-29"></a>
|
||||
<a id="__codelineno-20-30" name="__codelineno-20-30" href="#__codelineno-20-30"></a><span class="w"> </span><span class="cm">/* 查询操作 */</span>
|
||||
<a id="__codelineno-20-31" name="__codelineno-20-31" href="#__codelineno-20-31"></a><span class="w"> </span><span class="kt">String</span><span class="o">?</span><span class="w"> </span><span class="kd">get</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-32" name="__codelineno-20-32" href="#__codelineno-20-32"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-33" name="__codelineno-20-33" href="#__codelineno-20-33"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span>
|
||||
<a id="__codelineno-20-34" name="__codelineno-20-34" href="#__codelineno-20-34"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-35" name="__codelineno-20-35" href="#__codelineno-20-35"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span>
|
||||
<a id="__codelineno-20-36" name="__codelineno-20-36" href="#__codelineno-20-36"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-37" name="__codelineno-20-37" href="#__codelineno-20-37"></a><span class="w"> </span><span class="c1">// 若遇到空桶,说明无此 key ,则返回 null</span>
|
||||
<a id="__codelineno-20-38" name="__codelineno-20-38" href="#__codelineno-20-38"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-39" name="__codelineno-20-39" href="#__codelineno-20-39"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则返回对应 val</span>
|
||||
<a id="__codelineno-20-40" name="__codelineno-20-40" href="#__codelineno-20-40"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">_removed</span><span class="p">)</span>
|
||||
<a id="__codelineno-20-41" name="__codelineno-20-41" href="#__codelineno-20-41"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">val</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-42" name="__codelineno-20-42" href="#__codelineno-20-42"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-43" name="__codelineno-20-43" href="#__codelineno-20-43"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-44" name="__codelineno-20-44" href="#__codelineno-20-44"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-45" name="__codelineno-20-45" href="#__codelineno-20-45"></a>
|
||||
<a id="__codelineno-20-46" name="__codelineno-20-46" href="#__codelineno-20-46"></a><span class="w"> </span><span class="cm">/* 添加操作 */</span>
|
||||
<a id="__codelineno-20-47" name="__codelineno-20-47" href="#__codelineno-20-47"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">put</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="kt">String</span><span class="w"> </span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-48" name="__codelineno-20-48" href="#__codelineno-20-48"></a><span class="w"> </span><span class="c1">// 当负载因子超过阈值时,执行扩容</span>
|
||||
<a id="__codelineno-20-49" name="__codelineno-20-49" href="#__codelineno-20-49"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">loadFactor</span><span class="p">()</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">_loadThres</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-50" name="__codelineno-20-50" href="#__codelineno-20-50"></a><span class="w"> </span><span class="n">extend</span><span class="p">();</span>
|
||||
<a id="__codelineno-20-13" name="__codelineno-20-13" href="#__codelineno-20-13"></a><span class="w"> </span><span class="n">_buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">generate</span><span class="p">(</span><span class="n">_capacity</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="kc">null</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-14" name="__codelineno-20-14" href="#__codelineno-20-14"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-15" name="__codelineno-20-15" href="#__codelineno-20-15"></a>
|
||||
<a id="__codelineno-20-16" name="__codelineno-20-16" href="#__codelineno-20-16"></a><span class="w"> </span><span class="cm">/* 哈希函数 */</span>
|
||||
<a id="__codelineno-20-17" name="__codelineno-20-17" href="#__codelineno-20-17"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-18" name="__codelineno-20-18" href="#__codelineno-20-18"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-19" name="__codelineno-20-19" href="#__codelineno-20-19"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-20" name="__codelineno-20-20" href="#__codelineno-20-20"></a>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="cm">/* 负载因子 */</span>
|
||||
<a id="__codelineno-20-22" name="__codelineno-20-22" href="#__codelineno-20-22"></a><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">loadFactor</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-23" name="__codelineno-20-23" href="#__codelineno-20-23"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">_size</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-24" name="__codelineno-20-24" href="#__codelineno-20-24"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-25" name="__codelineno-20-25" href="#__codelineno-20-25"></a>
|
||||
<a id="__codelineno-20-26" name="__codelineno-20-26" href="#__codelineno-20-26"></a><span class="w"> </span><span class="cm">/* 搜索 key 对应的桶索引 */</span>
|
||||
<a id="__codelineno-20-27" name="__codelineno-20-27" href="#__codelineno-20-27"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">findBucket</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-28" name="__codelineno-20-28" href="#__codelineno-20-28"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-29" name="__codelineno-20-29" href="#__codelineno-20-29"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">firstTombstone</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="m">1</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-30" name="__codelineno-20-30" href="#__codelineno-20-30"></a><span class="w"> </span><span class="c1">// 线性探测,当遇到空桶时跳出</span>
|
||||
<a id="__codelineno-20-31" name="__codelineno-20-31" href="#__codelineno-20-31"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-32" name="__codelineno-20-32" href="#__codelineno-20-32"></a><span class="w"> </span><span class="c1">// 若遇到 key ,返回对应桶索引</span>
|
||||
<a id="__codelineno-20-33" name="__codelineno-20-33" href="#__codelineno-20-33"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-34" name="__codelineno-20-34" href="#__codelineno-20-34"></a><span class="w"> </span><span class="c1">// 若之前遇到了删除标记,则将键值对移动至该索引</span>
|
||||
<a id="__codelineno-20-35" name="__codelineno-20-35" href="#__codelineno-20-35"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">firstTombstone</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="o">-</span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-36" name="__codelineno-20-36" href="#__codelineno-20-36"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">firstTombstone</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">];</span>
|
||||
<a id="__codelineno-20-37" name="__codelineno-20-37" href="#__codelineno-20-37"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-38" name="__codelineno-20-38" href="#__codelineno-20-38"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">firstTombstone</span><span class="p">;</span><span class="w"> </span><span class="c1">// 返回移动后的桶索引</span>
|
||||
<a id="__codelineno-20-39" name="__codelineno-20-39" href="#__codelineno-20-39"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-40" name="__codelineno-20-40" href="#__codelineno-20-40"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">index</span><span class="p">;</span><span class="w"> </span><span class="c1">// 返回桶索引</span>
|
||||
<a id="__codelineno-20-41" name="__codelineno-20-41" href="#__codelineno-20-41"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-42" name="__codelineno-20-42" href="#__codelineno-20-42"></a><span class="w"> </span><span class="c1">// 记录遇到的首个删除标记</span>
|
||||
<a id="__codelineno-20-43" name="__codelineno-20-43" href="#__codelineno-20-43"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">firstTombstone</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">-</span><span class="m">1</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-44" name="__codelineno-20-44" href="#__codelineno-20-44"></a><span class="w"> </span><span class="n">firstTombstone</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">index</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-45" name="__codelineno-20-45" href="#__codelineno-20-45"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-46" name="__codelineno-20-46" href="#__codelineno-20-46"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span>
|
||||
<a id="__codelineno-20-47" name="__codelineno-20-47" href="#__codelineno-20-47"></a><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-48" name="__codelineno-20-48" href="#__codelineno-20-48"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-49" name="__codelineno-20-49" href="#__codelineno-20-49"></a><span class="w"> </span><span class="c1">// 若 key 不存在,则返回添加点的索引</span>
|
||||
<a id="__codelineno-20-50" name="__codelineno-20-50" href="#__codelineno-20-50"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">firstTombstone</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">-</span><span class="m">1</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">firstTombstone</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-51" name="__codelineno-20-51" href="#__codelineno-20-51"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-52" name="__codelineno-20-52" href="#__codelineno-20-52"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-53" name="__codelineno-20-53" href="#__codelineno-20-53"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span>
|
||||
<a id="__codelineno-20-54" name="__codelineno-20-54" href="#__codelineno-20-54"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-55" name="__codelineno-20-55" href="#__codelineno-20-55"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span>
|
||||
<a id="__codelineno-20-56" name="__codelineno-20-56" href="#__codelineno-20-56"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-57" name="__codelineno-20-57" href="#__codelineno-20-57"></a><span class="w"> </span><span class="c1">// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶</span>
|
||||
<a id="__codelineno-20-58" name="__codelineno-20-58" href="#__codelineno-20-58"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">_removed</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-59" name="__codelineno-20-59" href="#__codelineno-20-59"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Pair</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-60" name="__codelineno-20-60" href="#__codelineno-20-60"></a><span class="w"> </span><span class="n">_size</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-61" name="__codelineno-20-61" href="#__codelineno-20-61"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-62" name="__codelineno-20-62" href="#__codelineno-20-62"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-63" name="__codelineno-20-63" href="#__codelineno-20-63"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则更新对应 val</span>
|
||||
<a id="__codelineno-20-64" name="__codelineno-20-64" href="#__codelineno-20-64"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-65" name="__codelineno-20-65" href="#__codelineno-20-65"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">val</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-66" name="__codelineno-20-66" href="#__codelineno-20-66"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-67" name="__codelineno-20-67" href="#__codelineno-20-67"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-68" name="__codelineno-20-68" href="#__codelineno-20-68"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-69" name="__codelineno-20-69" href="#__codelineno-20-69"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-70" name="__codelineno-20-70" href="#__codelineno-20-70"></a>
|
||||
<a id="__codelineno-20-71" name="__codelineno-20-71" href="#__codelineno-20-71"></a><span class="w"> </span><span class="cm">/* 删除操作 */</span>
|
||||
<a id="__codelineno-20-72" name="__codelineno-20-72" href="#__codelineno-20-72"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">remove</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-73" name="__codelineno-20-73" href="#__codelineno-20-73"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-74" name="__codelineno-20-74" href="#__codelineno-20-74"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span>
|
||||
<a id="__codelineno-20-75" name="__codelineno-20-75" href="#__codelineno-20-75"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-76" name="__codelineno-20-76" href="#__codelineno-20-76"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span>
|
||||
<a id="__codelineno-20-77" name="__codelineno-20-77" href="#__codelineno-20-77"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">_capacity</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-78" name="__codelineno-20-78" href="#__codelineno-20-78"></a><span class="w"> </span><span class="c1">// 若遇到空桶,说明无此 key ,则直接返回</span>
|
||||
<a id="__codelineno-20-79" name="__codelineno-20-79" href="#__codelineno-20-79"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-80" name="__codelineno-20-80" href="#__codelineno-20-80"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-52" name="__codelineno-20-52" href="#__codelineno-20-52"></a>
|
||||
<a id="__codelineno-20-53" name="__codelineno-20-53" href="#__codelineno-20-53"></a><span class="w"> </span><span class="cm">/* 查询操作 */</span>
|
||||
<a id="__codelineno-20-54" name="__codelineno-20-54" href="#__codelineno-20-54"></a><span class="w"> </span><span class="kt">String</span><span class="o">?</span><span class="w"> </span><span class="kd">get</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-55" name="__codelineno-20-55" href="#__codelineno-20-55"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
|
||||
<a id="__codelineno-20-56" name="__codelineno-20-56" href="#__codelineno-20-56"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">findBucket</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-57" name="__codelineno-20-57" href="#__codelineno-20-57"></a><span class="w"> </span><span class="c1">// 若找到键值对,则返回对应 val</span>
|
||||
<a id="__codelineno-20-58" name="__codelineno-20-58" href="#__codelineno-20-58"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-59" name="__codelineno-20-59" href="#__codelineno-20-59"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">val</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-60" name="__codelineno-20-60" href="#__codelineno-20-60"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-61" name="__codelineno-20-61" href="#__codelineno-20-61"></a><span class="w"> </span><span class="c1">// 若键值对不存在,则返回 null</span>
|
||||
<a id="__codelineno-20-62" name="__codelineno-20-62" href="#__codelineno-20-62"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-63" name="__codelineno-20-63" href="#__codelineno-20-63"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-64" name="__codelineno-20-64" href="#__codelineno-20-64"></a>
|
||||
<a id="__codelineno-20-65" name="__codelineno-20-65" href="#__codelineno-20-65"></a><span class="w"> </span><span class="cm">/* 添加操作 */</span>
|
||||
<a id="__codelineno-20-66" name="__codelineno-20-66" href="#__codelineno-20-66"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">put</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="kt">String</span><span class="w"> </span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-67" name="__codelineno-20-67" href="#__codelineno-20-67"></a><span class="w"> </span><span class="c1">// 当负载因子超过阈值时,执行扩容</span>
|
||||
<a id="__codelineno-20-68" name="__codelineno-20-68" href="#__codelineno-20-68"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">loadFactor</span><span class="p">()</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">_loadThres</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-69" name="__codelineno-20-69" href="#__codelineno-20-69"></a><span class="w"> </span><span class="n">extend</span><span class="p">();</span>
|
||||
<a id="__codelineno-20-70" name="__codelineno-20-70" href="#__codelineno-20-70"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-71" name="__codelineno-20-71" href="#__codelineno-20-71"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
|
||||
<a id="__codelineno-20-72" name="__codelineno-20-72" href="#__codelineno-20-72"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">findBucket</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-73" name="__codelineno-20-73" href="#__codelineno-20-73"></a><span class="w"> </span><span class="c1">// 若找到键值对,则覆盖 val 并返回</span>
|
||||
<a id="__codelineno-20-74" name="__codelineno-20-74" href="#__codelineno-20-74"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-75" name="__codelineno-20-75" href="#__codelineno-20-75"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">val</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-76" name="__codelineno-20-76" href="#__codelineno-20-76"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-77" name="__codelineno-20-77" href="#__codelineno-20-77"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-78" name="__codelineno-20-78" href="#__codelineno-20-78"></a><span class="w"> </span><span class="c1">// 若键值对不存在,则添加该键值对</span>
|
||||
<a id="__codelineno-20-79" name="__codelineno-20-79" href="#__codelineno-20-79"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Pair</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-80" name="__codelineno-20-80" href="#__codelineno-20-80"></a><span class="w"> </span><span class="n">_size</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-81" name="__codelineno-20-81" href="#__codelineno-20-81"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-82" name="__codelineno-20-82" href="#__codelineno-20-82"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则标记删除并返回</span>
|
||||
<a id="__codelineno-20-83" name="__codelineno-20-83" href="#__codelineno-20-83"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">!</span><span class="p">.</span><span class="n">key</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-84" name="__codelineno-20-84" href="#__codelineno-20-84"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_removed</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-85" name="__codelineno-20-85" href="#__codelineno-20-85"></a><span class="w"> </span><span class="n">_size</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-86" name="__codelineno-20-86" href="#__codelineno-20-86"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-87" name="__codelineno-20-87" href="#__codelineno-20-87"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-88" name="__codelineno-20-88" href="#__codelineno-20-88"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-89" name="__codelineno-20-89" href="#__codelineno-20-89"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-90" name="__codelineno-20-90" href="#__codelineno-20-90"></a>
|
||||
<a id="__codelineno-20-91" name="__codelineno-20-91" href="#__codelineno-20-91"></a><span class="w"> </span><span class="cm">/* 扩容哈希表 */</span>
|
||||
<a id="__codelineno-20-92" name="__codelineno-20-92" href="#__codelineno-20-92"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">extend</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-93" name="__codelineno-20-93" href="#__codelineno-20-93"></a><span class="w"> </span><span class="c1">// 暂存原哈希表</span>
|
||||
<a id="__codelineno-20-94" name="__codelineno-20-94" href="#__codelineno-20-94"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Pair</span><span class="o">?></span><span class="w"> </span><span class="n">bucketsTmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_buckets</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-95" name="__codelineno-20-95" href="#__codelineno-20-95"></a><span class="w"> </span><span class="c1">// 初始化扩容后的新哈希表</span>
|
||||
<a id="__codelineno-20-96" name="__codelineno-20-96" href="#__codelineno-20-96"></a><span class="w"> </span><span class="n">_capacity</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="n">_extendRatio</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-97" name="__codelineno-20-97" href="#__codelineno-20-97"></a><span class="w"> </span><span class="n">_buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">generate</span><span class="p">(</span><span class="n">_capacity</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="kc">null</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-98" name="__codelineno-20-98" href="#__codelineno-20-98"></a><span class="w"> </span><span class="n">_size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-99" name="__codelineno-20-99" href="#__codelineno-20-99"></a><span class="w"> </span><span class="c1">// 将键值对从原哈希表搬运至新哈希表</span>
|
||||
<a id="__codelineno-20-100" name="__codelineno-20-100" href="#__codelineno-20-100"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="o">?</span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">bucketsTmp</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-101" name="__codelineno-20-101" href="#__codelineno-20-101"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pair</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">_removed</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-102" name="__codelineno-20-102" href="#__codelineno-20-102"></a><span class="w"> </span><span class="n">put</span><span class="p">(</span><span class="n">pair</span><span class="p">.</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">pair</span><span class="p">.</span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-103" name="__codelineno-20-103" href="#__codelineno-20-103"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-104" name="__codelineno-20-104" href="#__codelineno-20-104"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-105" name="__codelineno-20-105" href="#__codelineno-20-105"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-106" name="__codelineno-20-106" href="#__codelineno-20-106"></a>
|
||||
<a id="__codelineno-20-107" name="__codelineno-20-107" href="#__codelineno-20-107"></a><span class="w"> </span><span class="cm">/* 打印哈希表 */</span>
|
||||
<a id="__codelineno-20-108" name="__codelineno-20-108" href="#__codelineno-20-108"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">printHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-109" name="__codelineno-20-109" href="#__codelineno-20-109"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="o">?</span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">_buckets</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-110" name="__codelineno-20-110" href="#__codelineno-20-110"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pair</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-111" name="__codelineno-20-111" href="#__codelineno-20-111"></a><span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s2">"</span><span class="si">${</span><span class="n">pair</span><span class="p">.</span><span class="n">key</span><span class="si">}</span><span class="s2"> -> </span><span class="si">${</span><span class="n">pair</span><span class="p">.</span><span class="n">val</span><span class="si">}</span><span class="s2">"</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-112" name="__codelineno-20-112" href="#__codelineno-20-112"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-113" name="__codelineno-20-113" href="#__codelineno-20-113"></a><span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-114" name="__codelineno-20-114" href="#__codelineno-20-114"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-115" name="__codelineno-20-115" href="#__codelineno-20-115"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-116" name="__codelineno-20-116" href="#__codelineno-20-116"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-117" name="__codelineno-20-117" href="#__codelineno-20-117"></a><span class="p">}</span>
|
||||
<a id="__codelineno-20-82" name="__codelineno-20-82" href="#__codelineno-20-82"></a>
|
||||
<a id="__codelineno-20-83" name="__codelineno-20-83" href="#__codelineno-20-83"></a><span class="w"> </span><span class="cm">/* 删除操作 */</span>
|
||||
<a id="__codelineno-20-84" name="__codelineno-20-84" href="#__codelineno-20-84"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">remove</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-85" name="__codelineno-20-85" href="#__codelineno-20-85"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
|
||||
<a id="__codelineno-20-86" name="__codelineno-20-86" href="#__codelineno-20-86"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">findBucket</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-87" name="__codelineno-20-87" href="#__codelineno-20-87"></a><span class="w"> </span><span class="c1">// 若找到键值对,则用删除标记覆盖它</span>
|
||||
<a id="__codelineno-20-88" name="__codelineno-20-88" href="#__codelineno-20-88"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-89" name="__codelineno-20-89" href="#__codelineno-20-89"></a><span class="w"> </span><span class="n">_buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-90" name="__codelineno-20-90" href="#__codelineno-20-90"></a><span class="w"> </span><span class="n">_size</span><span class="o">--</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-91" name="__codelineno-20-91" href="#__codelineno-20-91"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-92" name="__codelineno-20-92" href="#__codelineno-20-92"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-93" name="__codelineno-20-93" href="#__codelineno-20-93"></a>
|
||||
<a id="__codelineno-20-94" name="__codelineno-20-94" href="#__codelineno-20-94"></a><span class="w"> </span><span class="cm">/* 扩容哈希表 */</span>
|
||||
<a id="__codelineno-20-95" name="__codelineno-20-95" href="#__codelineno-20-95"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">extend</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-96" name="__codelineno-20-96" href="#__codelineno-20-96"></a><span class="w"> </span><span class="c1">// 暂存原哈希表</span>
|
||||
<a id="__codelineno-20-97" name="__codelineno-20-97" href="#__codelineno-20-97"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Pair</span><span class="o">?></span><span class="w"> </span><span class="n">bucketsTmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_buckets</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-98" name="__codelineno-20-98" href="#__codelineno-20-98"></a><span class="w"> </span><span class="c1">// 初始化扩容后的新哈希表</span>
|
||||
<a id="__codelineno-20-99" name="__codelineno-20-99" href="#__codelineno-20-99"></a><span class="w"> </span><span class="n">_capacity</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="n">_extendRatio</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-100" name="__codelineno-20-100" href="#__codelineno-20-100"></a><span class="w"> </span><span class="n">_buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">generate</span><span class="p">(</span><span class="n">_capacity</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="kc">null</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-101" name="__codelineno-20-101" href="#__codelineno-20-101"></a><span class="w"> </span><span class="n">_size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-20-102" name="__codelineno-20-102" href="#__codelineno-20-102"></a><span class="w"> </span><span class="c1">// 将键值对从原哈希表搬运至新哈希表</span>
|
||||
<a id="__codelineno-20-103" name="__codelineno-20-103" href="#__codelineno-20-103"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="o">?</span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">bucketsTmp</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-104" name="__codelineno-20-104" href="#__codelineno-20-104"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pair</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-105" name="__codelineno-20-105" href="#__codelineno-20-105"></a><span class="w"> </span><span class="n">put</span><span class="p">(</span><span class="n">pair</span><span class="p">.</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">pair</span><span class="p">.</span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-106" name="__codelineno-20-106" href="#__codelineno-20-106"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-107" name="__codelineno-20-107" href="#__codelineno-20-107"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-108" name="__codelineno-20-108" href="#__codelineno-20-108"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-109" name="__codelineno-20-109" href="#__codelineno-20-109"></a>
|
||||
<a id="__codelineno-20-110" name="__codelineno-20-110" href="#__codelineno-20-110"></a><span class="w"> </span><span class="cm">/* 打印哈希表 */</span>
|
||||
<a id="__codelineno-20-111" name="__codelineno-20-111" href="#__codelineno-20-111"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">printHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-112" name="__codelineno-20-112" href="#__codelineno-20-112"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">Pair</span><span class="o">?</span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">_buckets</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-113" name="__codelineno-20-113" href="#__codelineno-20-113"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pair</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-114" name="__codelineno-20-114" href="#__codelineno-20-114"></a><span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s2">"null"</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-115" name="__codelineno-20-115" href="#__codelineno-20-115"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pair</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">_TOMBSTONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-116" name="__codelineno-20-116" href="#__codelineno-20-116"></a><span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s2">"TOMBSTONE"</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-117" name="__codelineno-20-117" href="#__codelineno-20-117"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-118" name="__codelineno-20-118" href="#__codelineno-20-118"></a><span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s2">"</span><span class="si">${</span><span class="n">pair</span><span class="p">.</span><span class="n">key</span><span class="si">}</span><span class="s2"> -> </span><span class="si">${</span><span class="n">pair</span><span class="p">.</span><span class="n">val</span><span class="si">}</span><span class="s2">"</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-119" name="__codelineno-20-119" href="#__codelineno-20-119"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-120" name="__codelineno-20-120" href="#__codelineno-20-120"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-121" name="__codelineno-20-121" href="#__codelineno-20-121"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-20-122" name="__codelineno-20-122" href="#__codelineno-20-122"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -3622,12 +3622,12 @@
|
|||
<a id="__codelineno-13-4" name="__codelineno-13-4" href="#__codelineno-13-4"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">kv</span><span class="p">.</span><span class="n">first</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">" -> "</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">kv</span><span class="p">.</span><span class="n">second</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
|
||||
<a id="__codelineno-13-5" name="__codelineno-13-5" href="#__codelineno-13-5"></a><span class="p">}</span>
|
||||
<a id="__codelineno-13-6" name="__codelineno-13-6" href="#__codelineno-13-6"></a><span class="c1">// 单独遍历键 key</span>
|
||||
<a id="__codelineno-13-7" name="__codelineno-13-7" href="#__codelineno-13-7"></a><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="n">key</span><span class="o">:</span><span class="w"> </span><span class="n">map</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-13-8" name="__codelineno-13-8" href="#__codelineno-13-8"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">key</span><span class="p">.</span><span class="n">first</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
|
||||
<a id="__codelineno-13-7" name="__codelineno-13-7" href="#__codelineno-13-7"></a><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="n">kv</span><span class="o">:</span><span class="w"> </span><span class="n">map</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-13-8" name="__codelineno-13-8" href="#__codelineno-13-8"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">kv</span><span class="p">.</span><span class="n">first</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
|
||||
<a id="__codelineno-13-9" name="__codelineno-13-9" href="#__codelineno-13-9"></a><span class="p">}</span>
|
||||
<a id="__codelineno-13-10" name="__codelineno-13-10" href="#__codelineno-13-10"></a><span class="c1">// 单独遍历值 value</span>
|
||||
<a id="__codelineno-13-11" name="__codelineno-13-11" href="#__codelineno-13-11"></a><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="n">val</span><span class="o">:</span><span class="w"> </span><span class="n">map</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-13-12" name="__codelineno-13-12" href="#__codelineno-13-12"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">val</span><span class="p">.</span><span class="n">second</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
|
||||
<a id="__codelineno-13-11" name="__codelineno-13-11" href="#__codelineno-13-11"></a><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="n">kv</span><span class="o">:</span><span class="w"> </span><span class="n">map</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-13-12" name="__codelineno-13-12" href="#__codelineno-13-12"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">kv</span><span class="p">.</span><span class="n">second</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
|
||||
<a id="__codelineno-13-13" name="__codelineno-13-13" href="#__codelineno-13-13"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
|
@ -4674,7 +4674,126 @@
|
|||
<a id="__codelineno-35-6" name="__codelineno-35-6" href="#__codelineno-35-6"></a>
|
||||
<a id="__codelineno-35-7" name="__codelineno-35-7" href="#__codelineno-35-7"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">pair</span><span class="w"> </span><span class="n">pair</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-8" name="__codelineno-35-8" href="#__codelineno-35-8"></a>
|
||||
<a id="__codelineno-35-9" name="__codelineno-35-9" href="#__codelineno-35-9"></a><span class="p">[</span><span class="n">class</span><span class="p">]{</span><span class="n">arrayHashMap</span><span class="p">}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{}</span>
|
||||
<a id="__codelineno-35-9" name="__codelineno-35-9" href="#__codelineno-35-9"></a><span class="cm">/* 基于数组简易实现的哈希表 */</span>
|
||||
<a id="__codelineno-35-10" name="__codelineno-35-10" href="#__codelineno-35-10"></a><span class="k">struct</span><span class="w"> </span><span class="nc">arrayHashMap</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-11" name="__codelineno-35-11" href="#__codelineno-35-11"></a><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">*</span><span class="n">buckets</span><span class="p">[</span><span class="n">HASH_MAP_DEFAULT_SIZE</span><span class="p">];</span>
|
||||
<a id="__codelineno-35-12" name="__codelineno-35-12" href="#__codelineno-35-12"></a><span class="p">};</span>
|
||||
<a id="__codelineno-35-13" name="__codelineno-35-13" href="#__codelineno-35-13"></a>
|
||||
<a id="__codelineno-35-14" name="__codelineno-35-14" href="#__codelineno-35-14"></a><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">arrayHashMap</span><span class="w"> </span><span class="n">arrayHashMap</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-15" name="__codelineno-35-15" href="#__codelineno-35-15"></a>
|
||||
<a id="__codelineno-35-16" name="__codelineno-35-16" href="#__codelineno-35-16"></a><span class="cm">/* 哈希表初始化函数 */</span>
|
||||
<a id="__codelineno-35-17" name="__codelineno-35-17" href="#__codelineno-35-17"></a><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="nf">newArrayHashMap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-18" name="__codelineno-35-18" href="#__codelineno-35-18"></a><span class="w"> </span><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">map</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">arrayHashMap</span><span class="p">));</span>
|
||||
<a id="__codelineno-35-19" name="__codelineno-35-19" href="#__codelineno-35-19"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">map</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-20" name="__codelineno-35-20" href="#__codelineno-35-20"></a><span class="p">}</span>
|
||||
<a id="__codelineno-35-21" name="__codelineno-35-21" href="#__codelineno-35-21"></a>
|
||||
<a id="__codelineno-35-22" name="__codelineno-35-22" href="#__codelineno-35-22"></a><span class="cm">/* 添加操作 */</span>
|
||||
<a id="__codelineno-35-23" name="__codelineno-35-23" href="#__codelineno-35-23"></a><span class="kt">void</span><span class="w"> </span><span class="nf">put</span><span class="p">(</span><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-24" name="__codelineno-35-24" href="#__codelineno-35-24"></a><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">*</span><span class="n">pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">pair</span><span class="p">));</span>
|
||||
<a id="__codelineno-35-25" name="__codelineno-35-25" href="#__codelineno-35-25"></a><span class="w"> </span><span class="n">pair</span><span class="o">-></span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-26" name="__codelineno-35-26" href="#__codelineno-35-26"></a><span class="w"> </span><span class="n">pair</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-27" name="__codelineno-35-27" href="#__codelineno-35-27"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">pair</span><span class="o">-></span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-28" name="__codelineno-35-28" href="#__codelineno-35-28"></a>
|
||||
<a id="__codelineno-35-29" name="__codelineno-35-29" href="#__codelineno-35-29"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-30" name="__codelineno-35-30" href="#__codelineno-35-30"></a><span class="w"> </span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pair</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-31" name="__codelineno-35-31" href="#__codelineno-35-31"></a><span class="p">}</span>
|
||||
<a id="__codelineno-35-32" name="__codelineno-35-32" href="#__codelineno-35-32"></a>
|
||||
<a id="__codelineno-35-33" name="__codelineno-35-33" href="#__codelineno-35-33"></a><span class="cm">/* 删除操作 */</span>
|
||||
<a id="__codelineno-35-34" name="__codelineno-35-34" href="#__codelineno-35-34"></a><span class="kt">void</span><span class="w"> </span><span class="nf">removeItem</span><span class="p">(</span><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-35" name="__codelineno-35-35" href="#__codelineno-35-35"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hashFunc</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-36" name="__codelineno-35-36" href="#__codelineno-35-36"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-37" name="__codelineno-35-37" href="#__codelineno-35-37"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]);</span>
|
||||
<a id="__codelineno-35-38" name="__codelineno-35-38" href="#__codelineno-35-38"></a><span class="w"> </span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-39" name="__codelineno-35-39" href="#__codelineno-35-39"></a><span class="p">}</span>
|
||||
<a id="__codelineno-35-40" name="__codelineno-35-40" href="#__codelineno-35-40"></a>
|
||||
<a id="__codelineno-35-41" name="__codelineno-35-41" href="#__codelineno-35-41"></a><span class="cm">/* 获取所有键值对 */</span>
|
||||
<a id="__codelineno-35-42" name="__codelineno-35-42" href="#__codelineno-35-42"></a><span class="kt">void</span><span class="w"> </span><span class="nf">pairSet</span><span class="p">(</span><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="n">mapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-43" name="__codelineno-35-43" href="#__codelineno-35-43"></a><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-44" name="__codelineno-35-44" href="#__codelineno-35-44"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-45" name="__codelineno-35-45" href="#__codelineno-35-45"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-46" name="__codelineno-35-46" href="#__codelineno-35-46"></a>
|
||||
<a id="__codelineno-35-47" name="__codelineno-35-47" href="#__codelineno-35-47"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||
<a id="__codelineno-35-48" name="__codelineno-35-48" href="#__codelineno-35-48"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASH_MAP_DEFAULT_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-49" name="__codelineno-35-49" href="#__codelineno-35-49"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-50" name="__codelineno-35-50" href="#__codelineno-35-50"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-51" name="__codelineno-35-51" href="#__codelineno-35-51"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-52" name="__codelineno-35-52" href="#__codelineno-35-52"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-53" name="__codelineno-35-53" href="#__codelineno-35-53"></a>
|
||||
<a id="__codelineno-35-54" name="__codelineno-35-54" href="#__codelineno-35-54"></a><span class="w"> </span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">pair</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">total</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-55" name="__codelineno-35-55" href="#__codelineno-35-55"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASH_MAP_DEFAULT_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-56" name="__codelineno-35-56" href="#__codelineno-35-56"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-57" name="__codelineno-35-57" href="#__codelineno-35-57"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-58" name="__codelineno-35-58" href="#__codelineno-35-58"></a><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">));</span>
|
||||
<a id="__codelineno-35-59" name="__codelineno-35-59" href="#__codelineno-35-59"></a><span class="w"> </span><span class="n">strcpy</span><span class="p">(</span><span class="n">entries</span><span class="p">[</span><span class="n">index</span><span class="p">].</span><span class="n">val</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-60" name="__codelineno-35-60" href="#__codelineno-35-60"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-61" name="__codelineno-35-61" href="#__codelineno-35-61"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-62" name="__codelineno-35-62" href="#__codelineno-35-62"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-63" name="__codelineno-35-63" href="#__codelineno-35-63"></a>
|
||||
<a id="__codelineno-35-64" name="__codelineno-35-64" href="#__codelineno-35-64"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">entries</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-65" name="__codelineno-35-65" href="#__codelineno-35-65"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-66" name="__codelineno-35-66" href="#__codelineno-35-66"></a><span class="p">}</span>
|
||||
<a id="__codelineno-35-67" name="__codelineno-35-67" href="#__codelineno-35-67"></a>
|
||||
<a id="__codelineno-35-68" name="__codelineno-35-68" href="#__codelineno-35-68"></a><span class="cm">/* 获取所有键 */</span>
|
||||
<a id="__codelineno-35-69" name="__codelineno-35-69" href="#__codelineno-35-69"></a><span class="kt">void</span><span class="w"> </span><span class="nf">keySet</span><span class="p">(</span><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="n">mapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-70" name="__codelineno-35-70" href="#__codelineno-35-70"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">keys</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-71" name="__codelineno-35-71" href="#__codelineno-35-71"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-72" name="__codelineno-35-72" href="#__codelineno-35-72"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-73" name="__codelineno-35-73" href="#__codelineno-35-73"></a>
|
||||
<a id="__codelineno-35-74" name="__codelineno-35-74" href="#__codelineno-35-74"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||
<a id="__codelineno-35-75" name="__codelineno-35-75" href="#__codelineno-35-75"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASH_MAP_DEFAULT_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-76" name="__codelineno-35-76" href="#__codelineno-35-76"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-77" name="__codelineno-35-77" href="#__codelineno-35-77"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-78" name="__codelineno-35-78" href="#__codelineno-35-78"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-79" name="__codelineno-35-79" href="#__codelineno-35-79"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-80" name="__codelineno-35-80" href="#__codelineno-35-80"></a>
|
||||
<a id="__codelineno-35-81" name="__codelineno-35-81" href="#__codelineno-35-81"></a><span class="w"> </span><span class="n">keys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
|
||||
<a id="__codelineno-35-82" name="__codelineno-35-82" href="#__codelineno-35-82"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASH_MAP_DEFAULT_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-83" name="__codelineno-35-83" href="#__codelineno-35-83"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-84" name="__codelineno-35-84" href="#__codelineno-35-84"></a><span class="w"> </span><span class="n">keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">key</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-85" name="__codelineno-35-85" href="#__codelineno-35-85"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-86" name="__codelineno-35-86" href="#__codelineno-35-86"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-87" name="__codelineno-35-87" href="#__codelineno-35-87"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-88" name="__codelineno-35-88" href="#__codelineno-35-88"></a>
|
||||
<a id="__codelineno-35-89" name="__codelineno-35-89" href="#__codelineno-35-89"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-90" name="__codelineno-35-90" href="#__codelineno-35-90"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-91" name="__codelineno-35-91" href="#__codelineno-35-91"></a><span class="p">}</span>
|
||||
<a id="__codelineno-35-92" name="__codelineno-35-92" href="#__codelineno-35-92"></a>
|
||||
<a id="__codelineno-35-93" name="__codelineno-35-93" href="#__codelineno-35-93"></a><span class="cm">/* 获取所有值 */</span>
|
||||
<a id="__codelineno-35-94" name="__codelineno-35-94" href="#__codelineno-35-94"></a><span class="kt">void</span><span class="w"> </span><span class="nf">valueSet</span><span class="p">(</span><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="n">mapSet</span><span class="w"> </span><span class="o">*</span><span class="n">set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-95" name="__codelineno-35-95" href="#__codelineno-35-95"></a><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">vals</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-96" name="__codelineno-35-96" href="#__codelineno-35-96"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-97" name="__codelineno-35-97" href="#__codelineno-35-97"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-98" name="__codelineno-35-98" href="#__codelineno-35-98"></a>
|
||||
<a id="__codelineno-35-99" name="__codelineno-35-99" href="#__codelineno-35-99"></a><span class="w"> </span><span class="cm">/* 统计有效键值对数量 */</span>
|
||||
<a id="__codelineno-35-100" name="__codelineno-35-100" href="#__codelineno-35-100"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASH_MAP_DEFAULT_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-101" name="__codelineno-35-101" href="#__codelineno-35-101"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-102" name="__codelineno-35-102" href="#__codelineno-35-102"></a><span class="w"> </span><span class="n">total</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-103" name="__codelineno-35-103" href="#__codelineno-35-103"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-104" name="__codelineno-35-104" href="#__codelineno-35-104"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-105" name="__codelineno-35-105" href="#__codelineno-35-105"></a>
|
||||
<a id="__codelineno-35-106" name="__codelineno-35-106" href="#__codelineno-35-106"></a><span class="w"> </span><span class="n">vals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">total</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">));</span>
|
||||
<a id="__codelineno-35-107" name="__codelineno-35-107" href="#__codelineno-35-107"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">HASH_MAP_DEFAULT_SIZE</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-108" name="__codelineno-35-108" href="#__codelineno-35-108"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-109" name="__codelineno-35-109" href="#__codelineno-35-109"></a><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">d</span><span class="o">-></span><span class="n">buckets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-></span><span class="n">val</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-110" name="__codelineno-35-110" href="#__codelineno-35-110"></a><span class="w"> </span><span class="n">index</span><span class="o">++</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-111" name="__codelineno-35-111" href="#__codelineno-35-111"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-112" name="__codelineno-35-112" href="#__codelineno-35-112"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-113" name="__codelineno-35-113" href="#__codelineno-35-113"></a>
|
||||
<a id="__codelineno-35-114" name="__codelineno-35-114" href="#__codelineno-35-114"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">set</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vals</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-115" name="__codelineno-35-115" href="#__codelineno-35-115"></a><span class="w"> </span><span class="n">set</span><span class="o">-></span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-116" name="__codelineno-35-116" href="#__codelineno-35-116"></a><span class="p">}</span>
|
||||
<a id="__codelineno-35-117" name="__codelineno-35-117" href="#__codelineno-35-117"></a>
|
||||
<a id="__codelineno-35-118" name="__codelineno-35-118" href="#__codelineno-35-118"></a><span class="cm">/* 打印哈希表 */</span>
|
||||
<a id="__codelineno-35-119" name="__codelineno-35-119" href="#__codelineno-35-119"></a><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">(</span><span class="n">arrayHashMap</span><span class="w"> </span><span class="o">*</span><span class="n">d</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-120" name="__codelineno-35-120" href="#__codelineno-35-120"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-121" name="__codelineno-35-121" href="#__codelineno-35-121"></a><span class="w"> </span><span class="n">mapSet</span><span class="w"> </span><span class="n">set</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-122" name="__codelineno-35-122" href="#__codelineno-35-122"></a><span class="w"> </span><span class="n">pairSet</span><span class="p">(</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="n">set</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-123" name="__codelineno-35-123" href="#__codelineno-35-123"></a><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">*</span><span class="n">entries</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">pair</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">;</span>
|
||||
<a id="__codelineno-35-124" name="__codelineno-35-124" href="#__codelineno-35-124"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-35-125" name="__codelineno-35-125" href="#__codelineno-35-125"></a><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d -> %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">entries</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">val</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-126" name="__codelineno-35-126" href="#__codelineno-35-126"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-35-127" name="__codelineno-35-127" href="#__codelineno-35-127"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">set</span><span class="p">.</span><span class="n">set</span><span class="p">);</span>
|
||||
<a id="__codelineno-35-128" name="__codelineno-35-128" href="#__codelineno-35-128"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 185 KiB After Width: | Height: | Size: 157 KiB |
File diff suppressed because one or more lines are too long
204
sitemap.xml
204
sitemap.xml
|
@ -2,512 +2,512 @@
|
|||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/contribution/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/installation/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/n_queens_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/permutations_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/subset_sum_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/iteration_and_recursion/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/basic_data_types/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/character_encoding/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/number_encoding/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/binary_search_recur/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/build_binary_tree_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/divide_and_conquer/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/hanota_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_problem_features/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_solution_pipeline/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/edit_distance_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/knapsack_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/unbounded_knapsack_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/greedy_algorithm/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/max_capacity_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/max_product_cutting_problem/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_algorithm/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/build_heap/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/heap/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/top_k/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_reference/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search_edge/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search_insertion/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bucket_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/counting_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/heap_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/radix_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/selection_sort/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/sorting_algorithm/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/array_representation_of_tree/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/summary/</loc>
|
||||
<lastmod>2023-09-24</lastmod>
|
||||
<lastmod>2023-09-26</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
</urlset>
|
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
Loading…
Reference in a new issue