This commit is contained in:
krahets
2023-09-21 20:44:52 +08:00
parent 830dc9e326
commit 0ba5acd92b
111 changed files with 13161 additions and 5302 deletions
+27 -4
View File
@@ -3357,10 +3357,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Footer -->
<footer class="md-footer">
<!-- Further information -->
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3371,6 +3392,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_appendix/contribution.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_appendix/contribution.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="162">16.2 &nbsp; 一起参与创作<a class="headerlink" href="#162" title="Permanent link">&para;</a></h1> <h1 id="162">16.2 &nbsp; 一起参与创作<a class="headerlink" href="#162" title="Permanent link">&para;</a></h1>
<p>由于作者能力有限,书中难免存在一些遗漏和错误,请您谅解。如果您发现了笔误、失效链接、内容缺失、文字歧义、解释不清晰或行文结构不合理等问题,请协助我们进行修正,以给读者提供更优质的学习资源。</p> <p>由于作者能力有限,书中难免存在一些遗漏和错误,请您谅解。如果您发现了笔误、失效链接、内容缺失、文字歧义、解释不清晰或行文结构不合理等问题,请协助我们进行修正,以给读者提供更优质的学习资源。</p>
<p>所有<a href="https://github.com/krahets/hello-algo/graphs/contributors">撰稿人</a>的 GitHub ID 将被展示在本书的仓库主页上,以感谢他们对开源社区的无私奉献。</p> <p>所有<a href="https://github.com/krahets/hello-algo/graphs/contributors">撰稿人</a>的 GitHub ID 将被展示在本书的仓库主页上,以感谢他们对开源社区的无私奉献。</p>
@@ -3491,11 +3523,73 @@
<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>docker-compose<span class="w"> </span>down <div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>docker-compose<span class="w"> </span>down
</code></pre></div> </code></pre></div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../installation/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 16.1 &amp;nbsp; 编程环境安装"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
16.1 &nbsp; 编程环境安装
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_reference/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 参考文献"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
参考文献
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3561,48 +3655,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../installation/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 16.1 &amp;nbsp; 编程环境安装" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16.1 &nbsp; 编程环境安装 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_reference/" class="md-footer__link md-footer__link--next" aria-label="下一页: 参考文献" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
参考文献
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3613,6 +3690,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_appendix/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_appendix/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="16">第 16 章 &nbsp; 附录<a class="headerlink" href="#16" title="Permanent link">&para;</a></h1> <h1 id="16">第 16 章 &nbsp; 附录<a class="headerlink" href="#16" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="附录" src="../assets/covers/chapter_appendix.jpg" width="600" /></p> <p><img alt="附录" src="../assets/covers/chapter_appendix.jpg" width="600" /></p>
@@ -3390,11 +3422,73 @@
<li><a href="https://www.hello-algo.com/chapter_appendix/contribution/">16.2 &nbsp; 一起参与创作</a></li> <li><a href="https://www.hello-algo.com/chapter_appendix/contribution/">16.2 &nbsp; 一起参与创作</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_greedy/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 15.5 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
15.5 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="installation/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 16.1 &amp;nbsp; 编程环境安装"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
16.1 &nbsp; 编程环境安装
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3460,48 +3554,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_greedy/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 15.5 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15.5 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="installation/" class="md-footer__link md-footer__link--next" aria-label="下一页: 16.1 &amp;nbsp; 编程环境安装" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
16.1 &nbsp; 编程环境安装
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3512,6 +3589,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3542,7 +3542,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_appendix/installation.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_appendix/installation.md" title="编辑此页" class="md-content__button md-icon">
@@ -3553,6 +3578,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="161">16.1 &nbsp; 编程环境安装<a class="headerlink" href="#161" title="Permanent link">&para;</a></h1> <h1 id="161">16.1 &nbsp; 编程环境安装<a class="headerlink" href="#161" title="Permanent link">&para;</a></h1>
<h3 id="1-vscode">1. &nbsp; VSCode<a class="headerlink" href="#1-vscode" title="Permanent link">&para;</a></h3> <h3 id="1-vscode">1. &nbsp; VSCode<a class="headerlink" href="#1-vscode" title="Permanent link">&para;</a></h3>
<p>本书推荐使用开源轻量的 VSCode 作为本地 IDE ,下载并安装 <a href="https://code.visualstudio.com/">VSCode</a></p> <p>本书推荐使用开源轻量的 VSCode 作为本地 IDE ,下载并安装 <a href="https://code.visualstudio.com/">VSCode</a></p>
@@ -3607,11 +3639,73 @@
<li>在 VSCode 的插件市场中搜索 <code>rust</code> ,安装 <a href="https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer">rust-analyzer</a></li> <li>在 VSCode 的插件市场中搜索 <code>rust</code> ,安装 <a href="https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer">rust-analyzer</a></li>
</ol> </ol>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 16 章 &amp;nbsp; 附录"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 16 章 &nbsp; 附录
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../contribution/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 16.2 &amp;nbsp; 一起参与创作"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
16.2 &nbsp; 一起参与创作
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3677,48 +3771,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 16 章 &amp;nbsp; 附录" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 16 章 &nbsp; 附录 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../contribution/" class="md-footer__link md-footer__link--next" aria-label="下一页: 16.2 &amp;nbsp; 一起参与创作" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
16.2 &nbsp; 一起参与创作
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3729,6 +3806,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3554,7 +3554,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/array.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/array.md" title="编辑此页" class="md-content__button md-icon">
@@ -3565,6 +3590,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="41">4.1 &nbsp; 数组<a class="headerlink" href="#41" title="Permanent link">&para;</a></h1> <h1 id="41">4.1 &nbsp; 数组<a class="headerlink" href="#41" title="Permanent link">&para;</a></h1>
<p>「数组 array」是一种线性数据结构,其将相同类型元素存储在连续的内存空间中。我们将元素在数组中的位置称为该元素的「索引 index」。图 4-1 展示了数组的主要术语和概念。</p> <p>「数组 array」是一种线性数据结构,其将相同类型元素存储在连续的内存空间中。我们将元素在数组中的位置称为该元素的「索引 index」。图 4-1 展示了数组的主要术语和概念。</p>
<p><img alt="数组定义与存储方式" src="../array.assets/array_definition.png" /></p> <p><img alt="数组定义与存储方式" src="../array.assets/array_definition.png" /></p>
@@ -4618,11 +4650,73 @@
<li><strong>数据结构实现</strong>:数组可以用于实现栈、队列、哈希表、堆、图等数据结构。例如,图的邻接矩阵表示实际上是一个二维数组。</li> <li><strong>数据结构实现</strong>:数组可以用于实现栈、队列、哈希表、堆、图等数据结构。例如,图的邻接矩阵表示实际上是一个二维数组。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 4 章 &amp;nbsp; 数组与链表"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 4 章 &nbsp; 数组与链表
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../linked_list/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 4.2 &amp;nbsp; 链表"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.2 &nbsp; 链表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4688,48 +4782,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 4 章 &amp;nbsp; 数组与链表" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 4 章 &nbsp; 数组与链表 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../linked_list/" class="md-footer__link md-footer__link--next" aria-label="下一页: 4.2 &amp;nbsp; 链表" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.2 &nbsp; 链表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4740,6 +4817,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="4">第 4 章 &nbsp; 数组与链表<a class="headerlink" href="#4" title="Permanent link">&para;</a></h1> <h1 id="4">第 4 章 &nbsp; 数组与链表<a class="headerlink" href="#4" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="数组与链表" src="../assets/covers/chapter_array_and_linkedlist.jpg" width="600" /></p> <p><img alt="数组与链表" src="../assets/covers/chapter_array_and_linkedlist.jpg" width="600" /></p>
@@ -3397,11 +3429,73 @@
<li><a href="https://www.hello-algo.com/chapter_array_and_linkedlist/summary/">4.4 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_array_and_linkedlist/summary/">4.4 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_data_structure/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 3.5 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
3.5 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="array/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 4.1 &amp;nbsp; 数组"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.1 &nbsp; 数组
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3467,48 +3561,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_data_structure/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 3.5 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3.5 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="array/" class="md-footer__link md-footer__link--next" aria-label="下一页: 4.1 &amp;nbsp; 数组" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.1 &nbsp; 数组
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3519,6 +3596,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3540,7 +3540,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/linked_list.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/linked_list.md" title="编辑此页" class="md-content__button md-icon">
@@ -3551,6 +3576,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="42">4.2 &nbsp; 链表<a class="headerlink" href="#42" title="Permanent link">&para;</a></h1> <h1 id="42">4.2 &nbsp; 链表<a class="headerlink" href="#42" title="Permanent link">&para;</a></h1>
<p>内存空间是所有程序的公共资源,在一个复杂的系统运行环境下,空闲的内存空间可能散落在内存各处。我们知道,存储数组的内存空间必须是连续的,而当数组非常大时,内存可能无法提供如此大的连续空间。此时链表的灵活性优势就体现出来了。</p> <p>内存空间是所有程序的公共资源,在一个复杂的系统运行环境下,空闲的内存空间可能散落在内存各处。我们知道,存储数组的内存空间必须是连续的,而当数组非常大时,内存可能无法提供如此大的连续空间。此时链表的灵活性优势就体现出来了。</p>
<p>「链表 linked list」是一种线性数据结构,其中的每个元素都是一个节点对象,各个节点通过“引用”相连接。引用记录了下一个节点的内存地址,通过它可以从当前节点访问到下一个节点。</p> <p>「链表 linked list」是一种线性数据结构,其中的每个元素都是一个节点对象,各个节点通过“引用”相连接。引用记录了下一个节点的内存地址,通过它可以从当前节点访问到下一个节点。</p>
@@ -3571,8 +3603,8 @@
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">ListNode</span><span class="p">:</span> <div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">ListNode</span><span class="p">:</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;链表节点类&quot;&quot;&quot;</span> <a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;链表节点类&quot;&quot;&quot;</span>
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span> <a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span> <a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ListNode</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向下一节点的引用</span> <a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">ListNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向下一节点的引用</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -4572,9 +4604,9 @@
<div class="highlight"><pre><span></span><code><a id="__codelineno-72-1" name="__codelineno-72-1" href="#__codelineno-72-1"></a><span class="k">class</span> <span class="nc">ListNode</span><span class="p">:</span> <div class="highlight"><pre><span></span><code><a id="__codelineno-72-1" name="__codelineno-72-1" href="#__codelineno-72-1"></a><span class="k">class</span> <span class="nc">ListNode</span><span class="p">:</span>
<a id="__codelineno-72-2" name="__codelineno-72-2" href="#__codelineno-72-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;双向链表节点类&quot;&quot;&quot;</span> <a id="__codelineno-72-2" name="__codelineno-72-2" href="#__codelineno-72-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;双向链表节点类&quot;&quot;&quot;</span>
<a id="__codelineno-72-3" name="__codelineno-72-3" href="#__codelineno-72-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span> <a id="__codelineno-72-3" name="__codelineno-72-3" href="#__codelineno-72-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-72-4" name="__codelineno-72-4" href="#__codelineno-72-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span> <a id="__codelineno-72-4" name="__codelineno-72-4" href="#__codelineno-72-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span>
<a id="__codelineno-72-5" name="__codelineno-72-5" href="#__codelineno-72-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ListNode</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向后继节点的引用</span> <a id="__codelineno-72-5" name="__codelineno-72-5" href="#__codelineno-72-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">ListNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向后继节点的引用</span>
<a id="__codelineno-72-6" name="__codelineno-72-6" href="#__codelineno-72-6"></a> <span class="bp">self</span><span class="o">.</span><span class="n">prev</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ListNode</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向前驱节点的引用</span> <a id="__codelineno-72-6" name="__codelineno-72-6" href="#__codelineno-72-6"></a> <span class="bp">self</span><span class="o">.</span><span class="n">prev</span><span class="p">:</span> <span class="n">ListNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向前驱节点的引用</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -4765,11 +4797,73 @@
<li><strong>数据缓冲区</strong>:在某些数据缓冲区的实现中,也可能会使用到循环链表。比如在音频、视频播放器中,数据流可能会被分成多个缓冲块并放入一个循环链表,以便实现无缝播放。</li> <li><strong>数据缓冲区</strong>:在某些数据缓冲区的实现中,也可能会使用到循环链表。比如在音频、视频播放器中,数据流可能会被分成多个缓冲块并放入一个循环链表,以便实现无缝播放。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../array/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 4.1 &amp;nbsp; 数组"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
4.1 &nbsp; 数组
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../list/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 4.3 &amp;nbsp; 列表"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.3 &nbsp; 列表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4835,48 +4929,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../array/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 4.1 &amp;nbsp; 数组" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4.1 &nbsp; 数组 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../list/" class="md-footer__link md-footer__link--next" aria-label="下一页: 4.3 &amp;nbsp; 列表" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.3 &nbsp; 列表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4887,6 +4964,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3526,7 +3526,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/list.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/list.md" title="编辑此页" class="md-content__button md-icon">
@@ -3537,6 +3562,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="43">4.3 &nbsp; 列表<a class="headerlink" href="#43" title="Permanent link">&para;</a></h1> <h1 id="43">4.3 &nbsp; 列表<a class="headerlink" href="#43" title="Permanent link">&para;</a></h1>
<p><strong>数组长度不可变导致实用性降低</strong>。在实际中,我们可能事先无法确定需要存储多少数据,这使数组长度的选择变得困难。若长度过小,需要在持续添加数据时频繁扩容数组;若长度过大,则会造成内存空间的浪费。</p> <p><strong>数组长度不可变导致实用性降低</strong>。在实际中,我们可能事先无法确定需要存储多少数据,这使数组长度的选择变得困难。若长度过小,需要在持续添加数据时频繁扩容数组;若长度过大,则会造成内存空间的浪费。</p>
<p>为解决此问题,出现了一种被称为「动态数组 dynamic array」的数据结构,即长度可变的数组,也常被称为「列表 list」。列表基于数组实现,继承了数组的优点,并且可以在程序运行过程中动态扩容。我们可以在列表中自由地添加元素,而无须担心超过容量限制。</p> <p>为解决此问题,出现了一种被称为「动态数组 dynamic array」的数据结构,即长度可变的数组,也常被称为「列表 list」。列表基于数组实现,继承了数组的优点,并且可以在程序运行过程中动态扩容。我们可以在列表中自由地添加元素,而无须担心超过容量限制。</p>
@@ -5494,11 +5526,73 @@
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../linked_list/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 4.2 &amp;nbsp; 链表"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
4.2 &nbsp; 链表
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 4.4 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5564,48 +5658,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../linked_list/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 4.2 &amp;nbsp; 链表" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4.2 &nbsp; 链表 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 4.4 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
4.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5616,6 +5693,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_array_and_linkedlist/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="44">4.4 &nbsp; 小结<a class="headerlink" href="#44" title="Permanent link">&para;</a></h1> <h1 id="44">4.4 &nbsp; 小结<a class="headerlink" href="#44" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3506,11 +3538,73 @@
<p>另一方面,必要使用链表的情况主要是二叉树和图。栈和队列往往会使用编程语言提供的 <code>stack</code><code>queue</code> ,而非链表。</p> <p>另一方面,必要使用链表的情况主要是二叉树和图。栈和队列往往会使用编程语言提供的 <code>stack</code><code>queue</code> ,而非链表。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../list/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 4.3 &amp;nbsp; 列表"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
4.3 &nbsp; 列表
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_stack_and_queue/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 5 章 &amp;nbsp; 栈与队列"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 5 章 &nbsp; 栈与队列
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3576,48 +3670,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../list/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 4.3 &amp;nbsp; 列表" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4.3 &nbsp; 列表 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_stack_and_queue/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 5 章 &amp;nbsp; 栈与队列" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 5 章 &nbsp; 栈与队列
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3628,6 +3705,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3486,7 +3486,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/backtracking_algorithm.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/backtracking_algorithm.md" title="编辑此页" class="md-content__button md-icon">
@@ -3497,6 +3522,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="131">13.1 &nbsp; 回溯算法<a class="headerlink" href="#131" title="Permanent link">&para;</a></h1> <h1 id="131">13.1 &nbsp; 回溯算法<a class="headerlink" href="#131" title="Permanent link">&para;</a></h1>
<p>「回溯算法 backtracking algorithm」是一种通过穷举来解决问题的方法,它的核心思想是从一个初始状态出发,暴力搜索所有可能的解决方案,当遇到正确的解则将其记录,直到找到解或者尝试了所有可能的选择都无法找到解为止。</p> <p>「回溯算法 backtracking algorithm」是一种通过穷举来解决问题的方法,它的核心思想是从一个初始状态出发,暴力搜索所有可能的解决方案,当遇到正确的解则将其记录,直到找到解或者尝试了所有可能的选择都无法找到解为止。</p>
<p>回溯算法通常采用“深度优先搜索”来遍历解空间。在二叉树章节中,我们提到前序、中序和后序遍历都属于深度优先搜索。接下来,我们利用前序遍历构造一个回溯问题,逐步了解回溯算法的工作原理。</p> <p>回溯算法通常采用“深度优先搜索”来遍历解空间。在二叉树章节中,我们提到前序、中序和后序遍历都属于深度优先搜索。接下来,我们利用前序遍历构造一个回溯问题,逐步了解回溯算法的工作原理。</p>
@@ -5108,11 +5140,73 @@
<li>最大团问题是图论中的一个经典问题,可用贪心等启发式算法来解决。</li> <li>最大团问题是图论中的一个经典问题,可用贪心等启发式算法来解决。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 13 章 &amp;nbsp; 回溯"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 13 章 &nbsp; 回溯
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../permutations_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 13.2 &amp;nbsp; 全排列问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.2 &nbsp; 全排列问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5178,48 +5272,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 13 章 &amp;nbsp; 回溯" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 13 章 &nbsp; 回溯 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../permutations_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 13.2 &amp;nbsp; 全排列问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.2 &nbsp; 全排列问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5230,6 +5307,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="13">第 13 章 &nbsp; 回溯<a class="headerlink" href="#13" title="Permanent link">&para;</a></h1> <h1 id="13">第 13 章 &nbsp; 回溯<a class="headerlink" href="#13" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="回溯" src="../assets/covers/chapter_backtracking.jpg" width="600" /></p> <p><img alt="回溯" src="../assets/covers/chapter_backtracking.jpg" width="600" /></p>
@@ -3398,11 +3430,73 @@
<li><a href="https://www.hello-algo.com/chapter_backtracking/summary/">13.5 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_backtracking/summary/">13.5 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_divide_and_conquer/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 12.5 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
12.5 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="backtracking_algorithm/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 13.1 &amp;nbsp; 回溯算法"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.1 &nbsp; 回溯算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3468,48 +3562,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_divide_and_conquer/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 12.5 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
12.5 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="backtracking_algorithm/" class="md-footer__link md-footer__link--next" aria-label="下一页: 13.1 &amp;nbsp; 回溯算法" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.1 &nbsp; 回溯算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3520,6 +3597,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/n_queens_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/n_queens_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="134-n">13.4 &nbsp; N 皇后问题<a class="headerlink" href="#134-n" title="Permanent link">&para;</a></h1> <h1 id="134-n">13.4 &nbsp; N 皇后问题<a class="headerlink" href="#134-n" title="Permanent link">&para;</a></h1>
<div class="admonition question"> <div class="admonition question">
<p class="admonition-title">Question</p> <p class="admonition-title">Question</p>
@@ -4031,11 +4063,73 @@
<p>逐行放置 <span class="arithmatex">\(n\)</span> 次,考虑列约束,则从第一行到最后一行分别有 <span class="arithmatex">\(n\)</span><span class="arithmatex">\(n-1\)</span><span class="arithmatex">\(\dots\)</span><span class="arithmatex">\(2\)</span><span class="arithmatex">\(1\)</span> 个选择,<strong>因此时间复杂度为 <span class="arithmatex">\(O(n!)\)</span></strong> 。实际上,根据对角线约束的剪枝也能够大幅地缩小搜索空间,因而搜索效率往往优于以上时间复杂度。</p> <p>逐行放置 <span class="arithmatex">\(n\)</span> 次,考虑列约束,则从第一行到最后一行分别有 <span class="arithmatex">\(n\)</span><span class="arithmatex">\(n-1\)</span><span class="arithmatex">\(\dots\)</span><span class="arithmatex">\(2\)</span><span class="arithmatex">\(1\)</span> 个选择,<strong>因此时间复杂度为 <span class="arithmatex">\(O(n!)\)</span></strong> 。实际上,根据对角线约束的剪枝也能够大幅地缩小搜索空间,因而搜索效率往往优于以上时间复杂度。</p>
<p>数组 <code>state</code> 使用 <span class="arithmatex">\(O(n^2)\)</span> 空间,数组 <code>cols</code><code>diags1</code><code>diags2</code> 皆使用 <span class="arithmatex">\(O(n)\)</span> 空间。最大递归深度为 <span class="arithmatex">\(n\)</span> ,使用 <span class="arithmatex">\(O(n)\)</span> 栈帧空间。因此,<strong>空间复杂度为 <span class="arithmatex">\(O(n^2)\)</span></strong></p> <p>数组 <code>state</code> 使用 <span class="arithmatex">\(O(n^2)\)</span> 空间,数组 <code>cols</code><code>diags1</code><code>diags2</code> 皆使用 <span class="arithmatex">\(O(n)\)</span> 空间。最大递归深度为 <span class="arithmatex">\(n\)</span> ,使用 <span class="arithmatex">\(O(n)\)</span> 栈帧空间。因此,<strong>空间复杂度为 <span class="arithmatex">\(O(n^2)\)</span></strong></p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../subset_sum_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 13.3 &amp;nbsp; 子集和问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
13.3 &nbsp; 子集和问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 13.5 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4101,48 +4195,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../subset_sum_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 13.3 &amp;nbsp; 子集和问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13.3 &nbsp; 子集和问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 13.5 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4153,6 +4230,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3524,7 +3524,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/permutations_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/permutations_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3535,6 +3560,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="132">13.2 &nbsp; 全排列问题<a class="headerlink" href="#132" title="Permanent link">&para;</a></h1> <h1 id="132">13.2 &nbsp; 全排列问题<a class="headerlink" href="#132" title="Permanent link">&para;</a></h1>
<p>全排列问题是回溯算法的一个典型应用。它的定义是在给定一个集合(如一个数组或字符串)的情况下,找出这个集合中元素的所有可能的排列。</p> <p>全排列问题是回溯算法的一个典型应用。它的定义是在给定一个集合(如一个数组或字符串)的情况下,找出这个集合中元素的所有可能的排列。</p>
<p>表 13-2 列举了几个示例数据,包括输入数组和对应的所有排列。</p> <p>表 13-2 列举了几个示例数据,包括输入数组和对应的所有排列。</p>
@@ -4394,11 +4426,73 @@
<p><img alt="两种剪枝条件的作用范围" src="../permutations_problem.assets/permutations_ii_pruning_summary.png" /></p> <p><img alt="两种剪枝条件的作用范围" src="../permutations_problem.assets/permutations_ii_pruning_summary.png" /></p>
<p align="center"> 图 13-9 &nbsp; 两种剪枝条件的作用范围 </p> <p align="center"> 图 13-9 &nbsp; 两种剪枝条件的作用范围 </p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../backtracking_algorithm/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 13.1 &amp;nbsp; 回溯算法"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
13.1 &nbsp; 回溯算法
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../subset_sum_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 13.3 &amp;nbsp; 子集和问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.3 &nbsp; 子集和问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4464,48 +4558,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../backtracking_algorithm/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 13.1 &amp;nbsp; 回溯算法" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13.1 &nbsp; 回溯算法 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../subset_sum_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 13.3 &amp;nbsp; 子集和问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.3 &nbsp; 子集和问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4516,6 +4593,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3524,7 +3524,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/subset_sum_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/subset_sum_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3535,6 +3560,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="133">13.3 &nbsp; 子集和问题<a class="headerlink" href="#133" title="Permanent link">&para;</a></h1> <h1 id="133">13.3 &nbsp; 子集和问题<a class="headerlink" href="#133" title="Permanent link">&para;</a></h1>
<h2 id="1331">13.3.1 &nbsp; 无重复元素的情况<a class="headerlink" href="#1331" title="Permanent link">&para;</a></h2> <h2 id="1331">13.3.1 &nbsp; 无重复元素的情况<a class="headerlink" href="#1331" title="Permanent link">&para;</a></h2>
<div class="admonition question"> <div class="admonition question">
@@ -4888,11 +4920,73 @@
<p><img alt="子集和 II 回溯过程" src="../subset_sum_problem.assets/subset_sum_ii.png" /></p> <p><img alt="子集和 II 回溯过程" src="../subset_sum_problem.assets/subset_sum_ii.png" /></p>
<p align="center"> 图 13-14 &nbsp; 子集和 II 回溯过程 </p> <p align="center"> 图 13-14 &nbsp; 子集和 II 回溯过程 </p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../permutations_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 13.2 &amp;nbsp; 全排列问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
13.2 &nbsp; 全排列问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../n_queens_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 13.4 &amp;nbsp; N 皇后问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.4 &nbsp; N 皇后问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4958,48 +5052,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../permutations_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 13.2 &amp;nbsp; 全排列问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13.2 &nbsp; 全排列问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../n_queens_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 13.4 &amp;nbsp; N 皇后问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.4 &nbsp; N 皇后问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5010,6 +5087,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_backtracking/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="135">13.5 &nbsp; 小结<a class="headerlink" href="#135" title="Permanent link">&para;</a></h1> <h1 id="135">13.5 &nbsp; 小结<a class="headerlink" href="#135" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3465,11 +3497,73 @@
</ul> </ul>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../n_queens_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 13.4 &amp;nbsp; N 皇后问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
13.4 &nbsp; N 皇后问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_dynamic_programming/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 14 章 &amp;nbsp; 动态规划"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 14 章 &nbsp; 动态规划
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3535,48 +3629,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../n_queens_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 13.4 &amp;nbsp; N 皇后问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13.4 &nbsp; N 皇后问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_dynamic_programming/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 14 章 &amp;nbsp; 动态规划" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 14 章 &nbsp; 动态规划
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3587,6 +3664,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="2">第 2 章 &nbsp; 复杂度分析<a class="headerlink" href="#2" title="Permanent link">&para;</a></h1> <h1 id="2">第 2 章 &nbsp; 复杂度分析<a class="headerlink" href="#2" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="复杂度分析" src="../assets/covers/chapter_complexity_analysis.jpg" width="600" /></p> <p><img alt="复杂度分析" src="../assets/covers/chapter_complexity_analysis.jpg" width="600" /></p>
@@ -3398,11 +3430,73 @@
<li><a href="https://www.hello-algo.com/chapter_computational_complexity/summary/">2.5 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_computational_complexity/summary/">2.5 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_introduction/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 1.3 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
1.3 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="performance_evaluation/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 2.1 &amp;nbsp; 算法效率评估"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.1 &nbsp; 算法效率评估
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3468,48 +3562,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_introduction/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 1.3 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.3 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="performance_evaluation/" class="md-footer__link md-footer__link--next" aria-label="下一页: 2.1 &amp;nbsp; 算法效率评估" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.1 &nbsp; 算法效率评估
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3520,6 +3597,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3560,7 +3560,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/iteration_and_recursion.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/iteration_and_recursion.md" title="编辑此页" class="md-content__button md-icon">
@@ -3571,6 +3596,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="22">2.2 &nbsp; 迭代与递归<a class="headerlink" href="#22" title="Permanent link">&para;</a></h1> <h1 id="22">2.2 &nbsp; 迭代与递归<a class="headerlink" href="#22" title="Permanent link">&para;</a></h1>
<p>在数据结构与算法中,重复执行某个任务是很常见的,其与算法的复杂度密切相关。而要重复执行某个任务,我们通常会选用两种基本的程序结构:迭代和递归。</p> <p>在数据结构与算法中,重复执行某个任务是很常见的,其与算法的复杂度密切相关。而要重复执行某个任务,我们通常会选用两种基本的程序结构:迭代和递归。</p>
<h2 id="221">2.2.1 &nbsp; 迭代<a class="headerlink" href="#221" title="Permanent link">&para;</a></h2> <h2 id="221">2.2.1 &nbsp; 迭代<a class="headerlink" href="#221" title="Permanent link">&para;</a></h2>
@@ -4916,11 +4948,73 @@
</ul> </ul>
<p>总之,<strong>选择迭代还是递归取决于特定问题的性质</strong>。在编程实践中,权衡两者的优劣并根据情境选择合适的方法是至关重要的。</p> <p>总之,<strong>选择迭代还是递归取决于特定问题的性质</strong>。在编程实践中,权衡两者的优劣并根据情境选择合适的方法是至关重要的。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../performance_evaluation/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 2.1 &amp;nbsp; 算法效率评估"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
2.1 &nbsp; 算法效率评估
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../time_complexity/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 2.3 &amp;nbsp; 时间复杂度"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.3 &nbsp; 时间复杂度
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4986,48 +5080,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../performance_evaluation/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 2.1 &amp;nbsp; 算法效率评估" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2.1 &nbsp; 算法效率评估 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../time_complexity/" class="md-footer__link md-footer__link--next" aria-label="下一页: 2.3 &amp;nbsp; 时间复杂度" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.3 &nbsp; 时间复杂度
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5038,6 +5115,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/performance_evaluation.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/performance_evaluation.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="21">2.1 &nbsp; 算法效率评估<a class="headerlink" href="#21" title="Permanent link">&para;</a></h1> <h1 id="21">2.1 &nbsp; 算法效率评估<a class="headerlink" href="#21" title="Permanent link">&para;</a></h1>
<p>在算法设计中,我们先后追求以下两个层面的目标。</p> <p>在算法设计中,我们先后追求以下两个层面的目标。</p>
<ol> <ol>
@@ -3479,11 +3511,73 @@
<p>复杂度是个数学概念,对于初学者可能比较抽象,学习难度相对较高。从这个角度看,复杂度分析可能不太适合作为最先介绍的内容。然而,当我们讨论某个数据结构或算法的特点时,难以避免要分析其运行速度和空间使用情况。</p> <p>复杂度是个数学概念,对于初学者可能比较抽象,学习难度相对较高。从这个角度看,复杂度分析可能不太适合作为最先介绍的内容。然而,当我们讨论某个数据结构或算法的特点时,难以避免要分析其运行速度和空间使用情况。</p>
<p>综上所述,建议你在深入学习数据结构与算法之前,<strong>先对复杂度分析建立初步的了解,以便能够完成简单算法的复杂度分析</strong></p> <p>综上所述,建议你在深入学习数据结构与算法之前,<strong>先对复杂度分析建立初步的了解,以便能够完成简单算法的复杂度分析</strong></p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 2 章 &amp;nbsp; 复杂度分析"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 2 章 &nbsp; 复杂度分析
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../iteration_and_recursion/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 2.2 &amp;nbsp; 迭代与递归"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.2 &nbsp; 迭代与递归
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3549,48 +3643,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 2 章 &amp;nbsp; 复杂度分析" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 2 章 &nbsp; 复杂度分析 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../iteration_and_recursion/" class="md-footer__link md-footer__link--next" aria-label="下一页: 2.2 &amp;nbsp; 迭代与递归" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.2 &nbsp; 迭代与递归
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3601,6 +3678,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3540,7 +3540,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/space_complexity.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/space_complexity.md" title="编辑此页" class="md-content__button md-icon">
@@ -3551,6 +3576,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="24">2.4 &nbsp; 空间复杂度<a class="headerlink" href="#24" title="Permanent link">&para;</a></h1> <h1 id="24">2.4 &nbsp; 空间复杂度<a class="headerlink" href="#24" title="Permanent link">&para;</a></h1>
<p>「空间复杂度 space complexity」用于衡量算法占用内存空间随着数据量变大时的增长趋势。这个概念与时间复杂度非常类似,只需将“运行时间”替换为“占用内存空间”。</p> <p>「空间复杂度 space complexity」用于衡量算法占用内存空间随着数据量变大时的增长趋势。这个概念与时间复杂度非常类似,只需将“运行时间”替换为“占用内存空间”。</p>
<h2 id="241">2.4.1 &nbsp; 算法相关空间<a class="headerlink" href="#241" title="Permanent link">&para;</a></h2> <h2 id="241">2.4.1 &nbsp; 算法相关空间<a class="headerlink" href="#241" title="Permanent link">&para;</a></h2>
@@ -3577,8 +3609,8 @@
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">Node</span><span class="p">:</span> <div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">Node</span><span class="p">:</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;&quot;&quot;&quot;</span> <a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;&quot;&quot;&quot;</span>
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span> <a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">x</span> <span class="c1"># 节点值</span> <a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">x</span> <span class="c1"># 节点值</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Node</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向下一节点的引用</span> <a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">Node</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 指向下一节点的引用</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="k">def</span> <span class="nf">function</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span> <a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="k">def</span> <span class="nf">function</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;函数&quot;&quot;&quot;</span> <a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;函数&quot;&quot;&quot;</span>
@@ -5366,11 +5398,73 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
<p><strong>降低时间复杂度通常需要以提升空间复杂度为代价,反之亦然</strong>。我们将牺牲内存空间来提升算法运行速度的思路称为“以空间换时间”;反之,则称为“以时间换空间”。</p> <p><strong>降低时间复杂度通常需要以提升空间复杂度为代价,反之亦然</strong>。我们将牺牲内存空间来提升算法运行速度的思路称为“以空间换时间”;反之,则称为“以时间换空间”。</p>
<p>选择哪种思路取决于我们更看重哪个方面。在大多数情况下,时间比空间更宝贵,因此“以空间换时间”通常是更常用的策略。当然,在数据量很大的情况下,控制空间复杂度也是非常重要的。</p> <p>选择哪种思路取决于我们更看重哪个方面。在大多数情况下,时间比空间更宝贵,因此“以空间换时间”通常是更常用的策略。当然,在数据量很大的情况下,控制空间复杂度也是非常重要的。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../time_complexity/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 2.3 &amp;nbsp; 时间复杂度"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
2.3 &nbsp; 时间复杂度
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 2.5 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5436,48 +5530,31 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../time_complexity/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 2.3 &amp;nbsp; 时间复杂度" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2.3 &nbsp; 时间复杂度 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 2.5 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5488,6 +5565,8 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="25">2.5 &nbsp; 小结<a class="headerlink" href="#25" title="Permanent link">&para;</a></h1> <h1 id="25">2.5 &nbsp; 小结<a class="headerlink" href="#25" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<p><strong>算法效率评估</strong></p> <p><strong>算法效率评估</strong></p>
@@ -3487,11 +3519,73 @@
<p>在实际中,因为我们通常不知道每个方法的“常数项”复杂度是多少,所以一般无法仅凭复杂度来选择 <span class="arithmatex">\(n = 8\)</span> 之下的最优解法。但对于 <span class="arithmatex">\(n = 8^5\)</span> 就很好选了,这时增长趋势已经占主导了。</p> <p>在实际中,因为我们通常不知道每个方法的“常数项”复杂度是多少,所以一般无法仅凭复杂度来选择 <span class="arithmatex">\(n = 8\)</span> 之下的最优解法。但对于 <span class="arithmatex">\(n = 8^5\)</span> 就很好选了,这时增长趋势已经占主导了。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../space_complexity/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 2.4 &amp;nbsp; 空间复杂度"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
2.4 &nbsp; 空间复杂度
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_data_structure/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 3 章 &amp;nbsp; 数据结构"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 3 章 &nbsp; 数据结构
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3557,48 +3651,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../space_complexity/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 2.4 &amp;nbsp; 空间复杂度" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2.4 &nbsp; 空间复杂度 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_data_structure/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 3 章 &amp;nbsp; 数据结构" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 3 章 &nbsp; 数据结构
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3609,6 +3686,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3622,7 +3622,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/time_complexity.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_computational_complexity/time_complexity.md" title="编辑此页" class="md-content__button md-icon">
@@ -3633,6 +3658,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="23">2.3 &nbsp; 时间复杂度<a class="headerlink" href="#23" title="Permanent link">&para;</a></h1> <h1 id="23">2.3 &nbsp; 时间复杂度<a class="headerlink" href="#23" title="Permanent link">&para;</a></h1>
<p>运行时间可以直观且准确地反映算法的效率。如果我们想要准确预估一段代码的运行时间,应该如何操作呢?</p> <p>运行时间可以直观且准确地反映算法的效率。如果我们想要准确预估一段代码的运行时间,应该如何操作呢?</p>
<ol> <ol>
@@ -6583,11 +6615,73 @@ n! = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1
<p>可能由于 <span class="arithmatex">\(O\)</span> 符号过于朗朗上口,我们常常使用它来表示平均时间复杂度。但从严格意义上看,这种做法并不规范。在本书和其他资料中,若遇到类似“平均时间复杂度 <span class="arithmatex">\(O(n)\)</span>”的表述,请将其直接理解为 <span class="arithmatex">\(\Theta(n)\)</span></p> <p>可能由于 <span class="arithmatex">\(O\)</span> 符号过于朗朗上口,我们常常使用它来表示平均时间复杂度。但从严格意义上看,这种做法并不规范。在本书和其他资料中,若遇到类似“平均时间复杂度 <span class="arithmatex">\(O(n)\)</span>”的表述,请将其直接理解为 <span class="arithmatex">\(\Theta(n)\)</span></p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../iteration_and_recursion/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 2.2 &amp;nbsp; 迭代与递归"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
2.2 &nbsp; 迭代与递归
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../space_complexity/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 2.4 &amp;nbsp; 空间复杂度"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.4 &nbsp; 空间复杂度
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -6653,48 +6747,31 @@ n! = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../iteration_and_recursion/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 2.2 &amp;nbsp; 迭代与递归" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2.2 &nbsp; 迭代与递归 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../space_complexity/" class="md-footer__link md-footer__link--next" aria-label="下一页: 2.4 &amp;nbsp; 空间复杂度" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
2.4 &nbsp; 空间复杂度
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -6705,6 +6782,8 @@ n! = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/basic_data_types.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/basic_data_types.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="32">3.2 &nbsp; 基本数据类型<a class="headerlink" href="#32" title="Permanent link">&para;</a></h1> <h1 id="32">3.2 &nbsp; 基本数据类型<a class="headerlink" href="#32" title="Permanent link">&para;</a></h1>
<p>谈及计算机中的数据,我们会想到文本、图片、视频、语音、3D 模型等各种形式。尽管这些数据的组织形式各异,但它们都由各种基本数据类型构成。</p> <p>谈及计算机中的数据,我们会想到文本、图片、视频、语音、3D 模型等各种形式。尽管这些数据的组织形式各异,但它们都由各种基本数据类型构成。</p>
<p><strong>基本数据类型是 CPU 可以直接进行运算的类型</strong>,在算法中直接被使用,主要包括以下几种类型。</p> <p><strong>基本数据类型是 CPU 可以直接进行运算的类型</strong>,在算法中直接被使用,主要包括以下几种类型。</p>
@@ -3574,11 +3606,73 @@
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../classification_of_data_structure/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 3.1 &amp;nbsp; 数据结构分类"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
3.1 &nbsp; 数据结构分类
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../number_encoding/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 3.3 &amp;nbsp; 数字编码 *"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.3 &nbsp; 数字编码 *
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3644,48 +3738,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../classification_of_data_structure/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 3.1 &amp;nbsp; 数据结构分类" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3.1 &nbsp; 数据结构分类 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../number_encoding/" class="md-footer__link md-footer__link--next" aria-label="下一页: 3.3 &amp;nbsp; 数字编码 *" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.3 &nbsp; 数字编码 *
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3696,6 +3773,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3472,7 +3472,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/character_encoding.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/character_encoding.md" title="编辑此页" class="md-content__button md-icon">
@@ -3483,6 +3508,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="34">3.4 &nbsp; 字符编码 *<a class="headerlink" href="#34" title="Permanent link">&para;</a></h1> <h1 id="34">3.4 &nbsp; 字符编码 *<a class="headerlink" href="#34" title="Permanent link">&para;</a></h1>
<p>在计算机中,所有数据都是以二进制数的形式存储的,字符 <code>char</code> 也不例外。为了表示字符,我们需要建立一套“字符集”,规定每个字符和二进制数之间的一一对应关系。有了字符集之后,计算机就可以通过查表完成二进制数到字符的转换。</p> <p>在计算机中,所有数据都是以二进制数的形式存储的,字符 <code>char</code> 也不例外。为了表示字符,我们需要建立一套“字符集”,规定每个字符和二进制数之间的一一对应关系。有了字符集之后,计算机就可以通过查表完成二进制数到字符的转换。</p>
<h2 id="341-ascii">3.4.1 &nbsp; ASCII 字符集<a class="headerlink" href="#341-ascii" title="Permanent link">&para;</a></h2> <h2 id="341-ascii">3.4.1 &nbsp; ASCII 字符集<a class="headerlink" href="#341-ascii" title="Permanent link">&para;</a></h2>
@@ -3548,11 +3580,73 @@
</ul> </ul>
<p>需要注意的是,以上讨论的都是字符串在编程语言中的存储方式,<strong>这和字符串如何在文件中存储或在网络中传输是两个不同的问题</strong>。在文件存储或网络传输中,我们通常会将字符串编码为 UTF-8 格式,以达到最优的兼容性和空间效率。</p> <p>需要注意的是,以上讨论的都是字符串在编程语言中的存储方式,<strong>这和字符串如何在文件中存储或在网络中传输是两个不同的问题</strong>。在文件存储或网络传输中,我们通常会将字符串编码为 UTF-8 格式,以达到最优的兼容性和空间效率。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../number_encoding/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 3.3 &amp;nbsp; 数字编码 *"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
3.3 &nbsp; 数字编码 *
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 3.5 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3618,48 +3712,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../number_encoding/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 3.3 &amp;nbsp; 数字编码 *" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3.3 &nbsp; 数字编码 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 3.5 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3670,6 +3747,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/classification_of_data_structure.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/classification_of_data_structure.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="31">3.1 &nbsp; 数据结构分类<a class="headerlink" href="#31" title="Permanent link">&para;</a></h1> <h1 id="31">3.1 &nbsp; 数据结构分类<a class="headerlink" href="#31" title="Permanent link">&para;</a></h1>
<p>常见的数据结构包括数组、链表、栈、队列、哈希表、树、堆、图,它们可以从“逻辑结构”和“物理结构”两个维度进行分类。</p> <p>常见的数据结构包括数组、链表、栈、队列、哈希表、树、堆、图,它们可以从“逻辑结构”和“物理结构”两个维度进行分类。</p>
<h2 id="311">3.1.1 &nbsp; 逻辑结构:线性与非线性<a class="headerlink" href="#311" title="Permanent link">&para;</a></h2> <h2 id="311">3.1.1 &nbsp; 逻辑结构:线性与非线性<a class="headerlink" href="#311" title="Permanent link">&para;</a></h2>
@@ -3482,11 +3514,73 @@
<p>如果你感觉物理结构理解起来有困难,建议先阅读下一章“数组与链表”,然后再回顾本节内容。</p> <p>如果你感觉物理结构理解起来有困难,建议先阅读下一章“数组与链表”,然后再回顾本节内容。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 3 章 &amp;nbsp; 数据结构"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 3 章 &nbsp; 数据结构
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../basic_data_types/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 3.2 &amp;nbsp; 基本数据类型"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.2 &nbsp; 基本数据类型
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3552,48 +3646,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 3 章 &amp;nbsp; 数据结构" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 3 章 &nbsp; 数据结构 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../basic_data_types/" class="md-footer__link md-footer__link--next" aria-label="下一页: 3.2 &amp;nbsp; 基本数据类型" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.2 &nbsp; 基本数据类型
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3604,6 +3681,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="3">第 3 章 &nbsp; 数据结构<a class="headerlink" href="#3" title="Permanent link">&para;</a></h1> <h1 id="3">第 3 章 &nbsp; 数据结构<a class="headerlink" href="#3" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="数据结构" src="../assets/covers/chapter_data_structure.jpg" width="600" /></p> <p><img alt="数据结构" src="../assets/covers/chapter_data_structure.jpg" width="600" /></p>
@@ -3398,11 +3430,73 @@
<li><a href="https://www.hello-algo.com/chapter_data_structure/summary/">3.5 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_data_structure/summary/">3.5 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_computational_complexity/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 2.5 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
2.5 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="classification_of_data_structure/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 3.1 &amp;nbsp; 数据结构分类"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.1 &nbsp; 数据结构分类
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3468,48 +3562,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_computational_complexity/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 2.5 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2.5 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="classification_of_data_structure/" class="md-footer__link md-footer__link--next" aria-label="下一页: 3.1 &amp;nbsp; 数据结构分类" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.1 &nbsp; 数据结构分类
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3520,6 +3597,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/number_encoding.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/number_encoding.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="33">3.3 &nbsp; 数字编码 *<a class="headerlink" href="#33" title="Permanent link">&para;</a></h1> <h1 id="33">3.3 &nbsp; 数字编码 *<a class="headerlink" href="#33" title="Permanent link">&para;</a></h1>
<div class="admonition note"> <div class="admonition note">
<p class="admonition-title">Note</p> <p class="admonition-title">Note</p>
@@ -3584,11 +3616,73 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
<p>值得说明的是,次正规数显著提升了浮点数的精度。最小正正规数为 <span class="arithmatex">\(2^{-126}\)</span> ,最小正次正规数为 <span class="arithmatex">\(2^{-126} \times 2^{-23}\)</span></p> <p>值得说明的是,次正规数显著提升了浮点数的精度。最小正正规数为 <span class="arithmatex">\(2^{-126}\)</span> ,最小正次正规数为 <span class="arithmatex">\(2^{-126} \times 2^{-23}\)</span></p>
<p>双精度 <code>double</code> 也采用类似 <code>float</code> 的表示方法,在此不做赘述。</p> <p>双精度 <code>double</code> 也采用类似 <code>float</code> 的表示方法,在此不做赘述。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../basic_data_types/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 3.2 &amp;nbsp; 基本数据类型"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
3.2 &nbsp; 基本数据类型
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../character_encoding/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 3.4 &amp;nbsp; 字符编码 *"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.4 &nbsp; 字符编码 *
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3654,48 +3748,31 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../basic_data_types/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 3.2 &amp;nbsp; 基本数据类型" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3.2 &nbsp; 基本数据类型 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../character_encoding/" class="md-footer__link md-footer__link--next" aria-label="下一页: 3.4 &amp;nbsp; 字符编码 *" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
3.4 &nbsp; 字符编码 *
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3706,6 +3783,8 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_data_structure/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="35">3.5 &nbsp; 小结<a class="headerlink" href="#35" title="Permanent link">&para;</a></h1> <h1 id="35">3.5 &nbsp; 小结<a class="headerlink" href="#35" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3465,11 +3497,73 @@
<p><code>char</code> 类型的长度由编程语言采用的编码方法决定。例如,Java、JS、TS、C# 都采用 UTF-16 编码(保存 Unicode 码点),因此 char 类型的长度为 2 bytes 。</p> <p><code>char</code> 类型的长度由编程语言采用的编码方法决定。例如,Java、JS、TS、C# 都采用 UTF-16 编码(保存 Unicode 码点),因此 char 类型的长度为 2 bytes 。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../character_encoding/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 3.4 &amp;nbsp; 字符编码 *"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
3.4 &nbsp; 字符编码 *
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_array_and_linkedlist/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 4 章 &amp;nbsp; 数组与链表"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 4 章 &nbsp; 数组与链表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3535,48 +3629,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../character_encoding/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 3.4 &amp;nbsp; 字符编码 *" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3.4 &nbsp; 字符编码 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_array_and_linkedlist/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 4 章 &amp;nbsp; 数组与链表" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 4 章 &nbsp; 数组与链表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3587,6 +3664,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3424,7 +3424,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/binary_search_recur.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/binary_search_recur.md" title="编辑此页" class="md-content__button md-icon">
@@ -3435,6 +3460,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="122">12.2 &nbsp; 分治搜索策略<a class="headerlink" href="#122" title="Permanent link">&para;</a></h1> <h1 id="122">12.2 &nbsp; 分治搜索策略<a class="headerlink" href="#122" title="Permanent link">&para;</a></h1>
<p>我们已经学过,搜索算法分为两大类。</p> <p>我们已经学过,搜索算法分为两大类。</p>
<ul> <ul>
@@ -3773,11 +3805,73 @@
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../divide_and_conquer/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 12.1 &amp;nbsp; 分治算法"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
12.1 &nbsp; 分治算法
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../build_binary_tree_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 12.3 &amp;nbsp; 构建树问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.3 &nbsp; 构建树问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3843,48 +3937,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../divide_and_conquer/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 12.1 &amp;nbsp; 分治算法" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
12.1 &nbsp; 分治算法 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../build_binary_tree_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 12.3 &amp;nbsp; 构建树问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.3 &nbsp; 构建树问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3895,6 +3972,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3466,7 +3466,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/build_binary_tree_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/build_binary_tree_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3477,6 +3502,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="123">12.3 &nbsp; 构建二叉树问题<a class="headerlink" href="#123" title="Permanent link">&para;</a></h1> <h1 id="123">12.3 &nbsp; 构建二叉树问题<a class="headerlink" href="#123" title="Permanent link">&para;</a></h1>
<div class="admonition question"> <div class="admonition question">
<p class="admonition-title">Question</p> <p class="admonition-title">Question</p>
@@ -3919,11 +3951,73 @@
<p>设树的节点数量为 <span class="arithmatex">\(n\)</span> ,初始化每一个节点(执行一个递归函数 <code>dfs()</code> )使用 <span class="arithmatex">\(O(1)\)</span> 时间。<strong>因此总体时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong></p> <p>设树的节点数量为 <span class="arithmatex">\(n\)</span> ,初始化每一个节点(执行一个递归函数 <code>dfs()</code> )使用 <span class="arithmatex">\(O(1)\)</span> 时间。<strong>因此总体时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong></p>
<p>哈希表存储 <code>inorder</code> 元素到索引的映射,空间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。最差情况下,即二叉树退化为链表时,递归深度达到 <span class="arithmatex">\(n\)</span> ,使用 <span class="arithmatex">\(O(n)\)</span> 的栈帧空间。<strong>因此总体空间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong></p> <p>哈希表存储 <code>inorder</code> 元素到索引的映射,空间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。最差情况下,即二叉树退化为链表时,递归深度达到 <span class="arithmatex">\(n\)</span> ,使用 <span class="arithmatex">\(O(n)\)</span> 的栈帧空间。<strong>因此总体空间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong></p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../binary_search_recur/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 12.2 &amp;nbsp; 分治搜索策略"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
12.2 &nbsp; 分治搜索策略
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../hanota_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 12.4 &amp;nbsp; 汉诺塔问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.4 &nbsp; 汉诺塔问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3989,48 +4083,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../binary_search_recur/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 12.2 &amp;nbsp; 分治搜索策略" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
12.2 &nbsp; 分治搜索策略 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../hanota_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 12.4 &amp;nbsp; 汉诺塔问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.4 &nbsp; 汉诺塔问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4041,6 +4118,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3492,7 +3492,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/divide_and_conquer.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/divide_and_conquer.md" title="编辑此页" class="md-content__button md-icon">
@@ -3503,6 +3528,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="121">12.1 &nbsp; 分治算法<a class="headerlink" href="#121" title="Permanent link">&para;</a></h1> <h1 id="121">12.1 &nbsp; 分治算法<a class="headerlink" href="#121" title="Permanent link">&para;</a></h1>
<p>「分治 divide and conquer」,全称分而治之,是一种非常重要且常见的算法策略。分治通常基于递归实现,包括“分”和“治”两个步骤。</p> <p>「分治 divide and conquer」,全称分而治之,是一种非常重要且常见的算法策略。分治通常基于递归实现,包括“分”和“治”两个步骤。</p>
<ol> <ol>
@@ -3580,11 +3612,73 @@ n(n - 4) &amp; &gt; 0
</ul> </ul>
<p>可以看出,<strong>分治是一种“润物细无声”的算法思想</strong>,隐含在各种算法与数据结构之中。</p> <p>可以看出,<strong>分治是一种“润物细无声”的算法思想</strong>,隐含在各种算法与数据结构之中。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 12 章 &amp;nbsp; 分治"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 12 章 &nbsp; 分治
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../binary_search_recur/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 12.2 &amp;nbsp; 分治搜索策略"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.2 &nbsp; 分治搜索策略
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3650,48 +3744,31 @@ n(n - 4) &amp; &gt; 0
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 12 章 &amp;nbsp; 分治" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 12 章 &nbsp; 分治 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../binary_search_recur/" class="md-footer__link md-footer__link--next" aria-label="下一页: 12.2 &amp;nbsp; 分治搜索策略" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.2 &nbsp; 分治搜索策略
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3702,6 +3779,8 @@ n(n - 4) &amp; &gt; 0
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3452,7 +3452,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/hanota_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/hanota_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3463,6 +3488,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="124">12.4 &nbsp; 汉诺塔问题<a class="headerlink" href="#124" title="Permanent link">&para;</a></h1> <h1 id="124">12.4 &nbsp; 汉诺塔问题<a class="headerlink" href="#124" title="Permanent link">&para;</a></h1>
<p>在归并排序和构建二叉树中,我们都是将原问题分解为两个规模为原问题一半的子问题。然而对于汉诺塔问题,我们采用不同的分解策略。</p> <p>在归并排序和构建二叉树中,我们都是将原问题分解为两个规模为原问题一半的子问题。然而对于汉诺塔问题,我们采用不同的分解策略。</p>
<div class="admonition question"> <div class="admonition question">
@@ -3907,11 +3939,73 @@
<p>然而,即使僧侣们每秒钟移动一次,总共需要大约 <span class="arithmatex">\(2^{64} \approx 1.84×10^{19}\)</span> 秒,合约 <span class="arithmatex">\(5850\)</span> 亿年,远远超过了现在对宇宙年龄的估计。所以,倘若这个传说是真的,我们应该不需要担心世界末日的到来。</p> <p>然而,即使僧侣们每秒钟移动一次,总共需要大约 <span class="arithmatex">\(2^{64} \approx 1.84×10^{19}\)</span> 秒,合约 <span class="arithmatex">\(5850\)</span> 亿年,远远超过了现在对宇宙年龄的估计。所以,倘若这个传说是真的,我们应该不需要担心世界末日的到来。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../build_binary_tree_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 12.3 &amp;nbsp; 构建树问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
12.3 &nbsp; 构建树问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 12.5 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3977,48 +4071,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../build_binary_tree_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 12.3 &amp;nbsp; 构建树问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
12.3 &nbsp; 构建树问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 12.5 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4029,6 +4106,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="12">第 12 章 &nbsp; 分治<a class="headerlink" href="#12" title="Permanent link">&para;</a></h1> <h1 id="12">第 12 章 &nbsp; 分治<a class="headerlink" href="#12" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="分治" src="../assets/covers/chapter_divide_and_conquer.jpg" width="600" /></p> <p><img alt="分治" src="../assets/covers/chapter_divide_and_conquer.jpg" width="600" /></p>
@@ -3398,11 +3430,73 @@
<li><a href="https://www.hello-algo.com/chapter_divide_and_conquer/summary/">12.5 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_divide_and_conquer/summary/">12.5 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_sorting/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.11 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.11 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="divide_and_conquer/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 12.1 &amp;nbsp; 分治算法"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.1 &nbsp; 分治算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3468,48 +3562,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_sorting/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.11 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.11 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="divide_and_conquer/" class="md-footer__link md-footer__link--next" aria-label="下一页: 12.1 &amp;nbsp; 分治算法" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
12.1 &nbsp; 分治算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3520,6 +3597,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_divide_and_conquer/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="125">12.5 &nbsp; 小结<a class="headerlink" href="#125" title="Permanent link">&para;</a></h1> <h1 id="125">12.5 &nbsp; 小结<a class="headerlink" href="#125" title="Permanent link">&para;</a></h1>
<ul> <ul>
<li>分治算法是一种常见的算法设计策略,包括分(划分)和治(合并)两个阶段,通常基于递归实现。</li> <li>分治算法是一种常见的算法设计策略,包括分(划分)和治(合并)两个阶段,通常基于递归实现。</li>
@@ -3388,11 +3420,73 @@
<li>在汉诺塔问题中,一个规模为 <span class="arithmatex">\(n\)</span> 的问题可以被划分为两个规模为 <span class="arithmatex">\(n-1\)</span> 的子问题和一个规模为 <span class="arithmatex">\(1\)</span> 的子问题。按顺序解决这三个子问题后,原问题随之得到解决。</li> <li>在汉诺塔问题中,一个规模为 <span class="arithmatex">\(n\)</span> 的问题可以被划分为两个规模为 <span class="arithmatex">\(n-1\)</span> 的子问题和一个规模为 <span class="arithmatex">\(1\)</span> 的子问题。按顺序解决这三个子问题后,原问题随之得到解决。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../hanota_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 12.4 &amp;nbsp; 汉诺塔问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
12.4 &nbsp; 汉诺塔问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_backtracking/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 13 章 &amp;nbsp; 回溯"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 13 章 &nbsp; 回溯
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3458,48 +3552,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../hanota_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 12.4 &amp;nbsp; 汉诺塔问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
12.4 &nbsp; 汉诺塔问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_backtracking/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 13 章 &amp;nbsp; 回溯" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 13 章 &nbsp; 回溯
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3510,6 +3587,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3438,7 +3438,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/dp_problem_features.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/dp_problem_features.md" title="编辑此页" class="md-content__button md-icon">
@@ -3449,6 +3474,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="142">14.2 &nbsp; 动态规划问题特性<a class="headerlink" href="#142" title="Permanent link">&para;</a></h1> <h1 id="142">14.2 &nbsp; 动态规划问题特性<a class="headerlink" href="#142" title="Permanent link">&para;</a></h1>
<p>在上节中,我们学习了动态规划是如何通过子问题分解来求解问题的。实际上,子问题分解是一种通用的算法思路,在分治、动态规划、回溯中的侧重点不同。</p> <p>在上节中,我们学习了动态规划是如何通过子问题分解来求解问题的。实际上,子问题分解是一种通用的算法思路,在分治、动态规划、回溯中的侧重点不同。</p>
<ul> <ul>
@@ -4165,11 +4197,73 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
<p>在这个问题中,下次跳跃依赖于过去所有的状态,因为每一次跳跃都会在更高的阶梯上设置障碍,并影响未来的跳跃。对于这类问题,动态规划往往难以解决。</p> <p>在这个问题中,下次跳跃依赖于过去所有的状态,因为每一次跳跃都会在更高的阶梯上设置障碍,并影响未来的跳跃。对于这类问题,动态规划往往难以解决。</p>
<p>实际上,许多复杂的组合优化问题(例如旅行商问题)都不满足无后效性。对于这类问题,我们通常会选择使用其他方法,例如启发式搜索、遗传算法、强化学习等,从而在有限时间内得到可用的局部最优解。</p> <p>实际上,许多复杂的组合优化问题(例如旅行商问题)都不满足无后效性。对于这类问题,我们通常会选择使用其他方法,例如启发式搜索、遗传算法、强化学习等,从而在有限时间内得到可用的局部最优解。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../intro_to_dynamic_programming/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 14.1 &amp;nbsp; 初探动态规划"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
14.1 &nbsp; 初探动态规划
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../dp_solution_pipeline/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 14.3 &amp;nbsp; DP 解题思路"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.3 &nbsp; DP 解题思路
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4235,48 +4329,31 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../intro_to_dynamic_programming/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 14.1 &amp;nbsp; 初探动态规划" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14.1 &nbsp; 初探动态规划 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../dp_solution_pipeline/" class="md-footer__link md-footer__link--next" aria-label="下一页: 14.3 &amp;nbsp; DP 解题思路" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.3 &nbsp; DP 解题思路
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4287,6 +4364,8 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3506,7 +3506,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/dp_solution_pipeline.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/dp_solution_pipeline.md" title="编辑此页" class="md-content__button md-icon">
@@ -3517,6 +3542,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="143">14.3 &nbsp; 动态规划解题思路<a class="headerlink" href="#143" title="Permanent link">&para;</a></h1> <h1 id="143">14.3 &nbsp; 动态规划解题思路<a class="headerlink" href="#143" title="Permanent link">&para;</a></h1>
<p>上两节介绍了动态规划问题的主要特征,接下来我们一起探究两个更加实用的问题。</p> <p>上两节介绍了动态规划问题的主要特征,接下来我们一起探究两个更加实用的问题。</p>
<ol> <ol>
@@ -4712,11 +4744,73 @@ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j]
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../dp_problem_features/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 14.2 &amp;nbsp; DP 问题特性"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
14.2 &nbsp; DP 问题特性
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../knapsack_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 14.4 &amp;nbsp; 0-1 背包问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.4 &nbsp; 0-1 背包问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4782,48 +4876,31 @@ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j]
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../dp_problem_features/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 14.2 &amp;nbsp; DP 问题特性" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14.2 &nbsp; DP 问题特性 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../knapsack_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 14.4 &amp;nbsp; 0-1 背包问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.4 &nbsp; 0-1 背包问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4834,6 +4911,8 @@ dp[i, j] = \min(dp[i-1, j], dp[i, j-1]) + grid[i, j]
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3452,7 +3452,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/edit_distance_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/edit_distance_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3463,6 +3488,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="146">14.6 &nbsp; 编辑距离问题<a class="headerlink" href="#146" title="Permanent link">&para;</a></h1> <h1 id="146">14.6 &nbsp; 编辑距离问题<a class="headerlink" href="#146" title="Permanent link">&para;</a></h1>
<p>编辑距离,也被称为 Levenshtein 距离,指两个字符串之间互相转换的最小修改次数,通常用于在信息检索和自然语言处理中度量两个序列的相似度。</p> <p>编辑距离,也被称为 Levenshtein 距离,指两个字符串之间互相转换的最小修改次数,通常用于在信息检索和自然语言处理中度量两个序列的相似度。</p>
<div class="admonition question"> <div class="admonition question">
@@ -4246,11 +4278,73 @@ dp[i, j] = dp[i-1, j-1]
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../unbounded_knapsack_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 14.5 &amp;nbsp; 完全背包问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
14.5 &nbsp; 完全背包问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 14.7 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.7 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4316,48 +4410,31 @@ dp[i, j] = dp[i-1, j-1]
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../unbounded_knapsack_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 14.5 &amp;nbsp; 完全背包问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14.5 &nbsp; 完全背包问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 14.7 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.7 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4368,6 +4445,8 @@ dp[i, j] = dp[i-1, j-1]
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="14">第 14 章 &nbsp; 动态规划<a class="headerlink" href="#14" title="Permanent link">&para;</a></h1> <h1 id="14">第 14 章 &nbsp; 动态规划<a class="headerlink" href="#14" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="动态规划" src="../assets/covers/chapter_dynamic_programming.jpg" width="600" /></p> <p><img alt="动态规划" src="../assets/covers/chapter_dynamic_programming.jpg" width="600" /></p>
@@ -3400,11 +3432,73 @@
<li><a href="https://www.hello-algo.com/chapter_dynamic_programming/summary/">14.7 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_dynamic_programming/summary/">14.7 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_backtracking/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 13.5 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
13.5 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="intro_to_dynamic_programming/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 14.1 &amp;nbsp; 初探动态规划"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.1 &nbsp; 初探动态规划
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3470,48 +3564,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_backtracking/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 13.5 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13.5 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="intro_to_dynamic_programming/" class="md-footer__link md-footer__link--next" aria-label="下一页: 14.1 &amp;nbsp; 初探动态规划" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.1 &nbsp; 初探动态规划
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3522,6 +3599,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3466,7 +3466,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/intro_to_dynamic_programming.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/intro_to_dynamic_programming.md" title="编辑此页" class="md-content__button md-icon">
@@ -3477,6 +3502,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="141">14.1 &nbsp; 初探动态规划<a class="headerlink" href="#141" title="Permanent link">&para;</a></h1> <h1 id="141">14.1 &nbsp; 初探动态规划<a class="headerlink" href="#141" title="Permanent link">&para;</a></h1>
<p>「动态规划 dynamic programming」是一个重要的算法范式,它将一个问题分解为一系列更小的子问题,并通过存储子问题的解来避免重复计算,从而大幅提升时间效率。</p> <p>「动态规划 dynamic programming」是一个重要的算法范式,它将一个问题分解为一系列更小的子问题,并通过存储子问题的解来避免重复计算,从而大幅提升时间效率。</p>
<p>在本节中,我们从一个经典例题入手,先给出它的暴力回溯解法,观察其中包含的重叠子问题,再逐步导出更高效的动态规划解法。</p> <p>在本节中,我们从一个经典例题入手,先给出它的暴力回溯解法,观察其中包含的重叠子问题,再逐步导出更高效的动态规划解法。</p>
@@ -4703,11 +4735,73 @@ dp[i] = dp[i-1] + dp[i-2]
<p>观察以上代码,由于省去了数组 <code>dp</code> 占用的空间,因此空间复杂度从 <span class="arithmatex">\(O(n)\)</span> 降低至 <span class="arithmatex">\(O(1)\)</span></p> <p>观察以上代码,由于省去了数组 <code>dp</code> 占用的空间,因此空间复杂度从 <span class="arithmatex">\(O(n)\)</span> 降低至 <span class="arithmatex">\(O(1)\)</span></p>
<p>在动态规划问题中,当前状态往往仅与前面有限个状态有关,这时我们可以只保留必要的状态,通过“降维”来节省内存空间。<strong>这种空间优化技巧被称为“滚动变量”或“滚动数组”</strong></p> <p>在动态规划问题中,当前状态往往仅与前面有限个状态有关,这时我们可以只保留必要的状态,通过“降维”来节省内存空间。<strong>这种空间优化技巧被称为“滚动变量”或“滚动数组”</strong></p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 14 章 &amp;nbsp; 动态规划"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 14 章 &nbsp; 动态规划
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../dp_problem_features/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 14.2 &amp;nbsp; DP 问题特性"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.2 &nbsp; DP 问题特性
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4773,48 +4867,31 @@ dp[i] = dp[i-1] + dp[i-2]
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 14 章 &amp;nbsp; 动态规划" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 14 章 &nbsp; 动态规划 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../dp_problem_features/" class="md-footer__link md-footer__link--next" aria-label="下一页: 14.2 &amp;nbsp; DP 问题特性" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.2 &nbsp; DP 问题特性
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4825,6 +4902,8 @@ dp[i] = dp[i-1] + dp[i-2]
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3466,7 +3466,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/knapsack_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/knapsack_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3477,6 +3502,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="144-0-1">14.4 &nbsp; 0-1 背包问题<a class="headerlink" href="#144-0-1" title="Permanent link">&para;</a></h1> <h1 id="144-0-1">14.4 &nbsp; 0-1 背包问题<a class="headerlink" href="#144-0-1" title="Permanent link">&para;</a></h1>
<p>背包问题是一个非常好的动态规划入门题目,是动态规划中最常见的问题形式。其具有很多变种,例如 0-1 背包问题、完全背包问题、多重背包问题等。</p> <p>背包问题是一个非常好的动态规划入门题目,是动态规划中最常见的问题形式。其具有很多变种,例如 0-1 背包问题、完全背包问题、多重背包问题等。</p>
<p>在本节中,我们先来求解最常见的 0-1 背包问题。</p> <p>在本节中,我们先来求解最常见的 0-1 背包问题。</p>
@@ -4615,11 +4647,73 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../dp_solution_pipeline/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 14.3 &amp;nbsp; DP 解题思路"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
14.3 &nbsp; DP 解题思路
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../unbounded_knapsack_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 14.5 &amp;nbsp; 完全背包问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.5 &nbsp; 完全背包问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4685,48 +4779,31 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../dp_solution_pipeline/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 14.3 &amp;nbsp; DP 解题思路" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14.3 &nbsp; DP 解题思路 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../unbounded_knapsack_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 14.5 &amp;nbsp; 完全背包问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.5 &nbsp; 完全背包问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4737,6 +4814,8 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="147">14.7 &nbsp; 小结<a class="headerlink" href="#147" title="Permanent link">&para;</a></h1> <h1 id="147">14.7 &nbsp; 小结<a class="headerlink" href="#147" title="Permanent link">&para;</a></h1>
<ul> <ul>
<li>动态规划对问题进行分解,并通过存储子问题的解来规避重复计算,实现高效的计算效率。</li> <li>动态规划对问题进行分解,并通过存储子问题的解来规避重复计算,实现高效的计算效率。</li>
@@ -3400,11 +3432,73 @@
<li>在编辑距离中,状态依赖于其正上方、正左方、左上方的状态,因此空间优化后正序或倒序遍历都无法正确地进行状态转移。为此,我们利用一个变量暂存左上方状态,从而转化到与完全背包等价的情况,可以在空间优化后进行正序遍历。</li> <li>在编辑距离中,状态依赖于其正上方、正左方、左上方的状态,因此空间优化后正序或倒序遍历都无法正确地进行状态转移。为此,我们利用一个变量暂存左上方状态,从而转化到与完全背包等价的情况,可以在空间优化后进行正序遍历。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../edit_distance_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 14.6 &amp;nbsp; 编辑距离问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
14.6 &nbsp; 编辑距离问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_greedy/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 15 章 &amp;nbsp; 贪心"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 15 章 &nbsp; 贪心
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3470,48 +3564,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../edit_distance_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 14.6 &amp;nbsp; 编辑距离问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14.6 &nbsp; 编辑距离问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_greedy/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 15 章 &amp;nbsp; 贪心" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 15 章 &nbsp; 贪心
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3522,6 +3599,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3614,7 +3614,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/unbounded_knapsack_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_dynamic_programming/unbounded_knapsack_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3625,6 +3650,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="145">14.5 &nbsp; 完全背包问题<a class="headerlink" href="#145" title="Permanent link">&para;</a></h1> <h1 id="145">14.5 &nbsp; 完全背包问题<a class="headerlink" href="#145" title="Permanent link">&para;</a></h1>
<p>在本节中,我们先求解另一个常见的背包问题:完全背包,再了解它的一种特例:零钱兑换。</p> <p>在本节中,我们先求解另一个常见的背包问题:完全背包,再了解它的一种特例:零钱兑换。</p>
<h2 id="1451">14.5.1 &nbsp; 完全背包<a class="headerlink" href="#1451" title="Permanent link">&para;</a></h2> <h2 id="1451">14.5.1 &nbsp; 完全背包<a class="headerlink" href="#1451" title="Permanent link">&para;</a></h2>
@@ -5454,11 +5486,73 @@ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]]
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../knapsack_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 14.4 &amp;nbsp; 0-1 背包问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
14.4 &nbsp; 0-1 背包问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../edit_distance_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 14.6 &amp;nbsp; 编辑距离问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.6 &nbsp; 编辑距离问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5524,48 +5618,31 @@ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]]
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../knapsack_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 14.4 &amp;nbsp; 0-1 背包问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14.4 &nbsp; 0-1 背包问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../edit_distance_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 14.6 &amp;nbsp; 编辑距离问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
14.6 &nbsp; 编辑距离问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5576,6 +5653,8 @@ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]]
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3484,7 +3484,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/graph.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/graph.md" title="编辑此页" class="md-content__button md-icon">
@@ -3495,6 +3520,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="91">9.1 &nbsp;<a class="headerlink" href="#91" title="Permanent link">&para;</a></h1> <h1 id="91">9.1 &nbsp;<a class="headerlink" href="#91" title="Permanent link">&para;</a></h1>
<p>「图 graph」是一种非线性数据结构,由「顶点 vertex」和「边 edge」组成。我们可以将图 <span class="arithmatex">\(G\)</span> 抽象地表示为一组顶点 <span class="arithmatex">\(V\)</span> 和一组边 <span class="arithmatex">\(E\)</span> 的集合。以下示例展示了一个包含 5 个顶点和 7 条边的图。</p> <p>「图 graph」是一种非线性数据结构,由「顶点 vertex」和「边 edge」组成。我们可以将图 <span class="arithmatex">\(G\)</span> 抽象地表示为一组顶点 <span class="arithmatex">\(V\)</span> 和一组边 <span class="arithmatex">\(E\)</span> 的集合。以下示例展示了一个包含 5 个顶点和 7 条边的图。</p>
<div class="arithmatex">\[ <div class="arithmatex">\[
@@ -3594,11 +3626,73 @@ G &amp; = \{ V, E \} \newline
</table> </table>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 9 章 &amp;nbsp; 图"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 9 章 &nbsp;
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../graph_operations/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 9.2 &amp;nbsp; 图基础操作"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.2 &nbsp; 图基础操作
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3664,48 +3758,31 @@ G &amp; = \{ V, E \} \newline
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 9 章 &amp;nbsp; 图" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 9 章 &nbsp; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../graph_operations/" class="md-footer__link md-footer__link--next" aria-label="下一页: 9.2 &amp;nbsp; 图基础操作" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.2 &nbsp; 图基础操作
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3716,6 +3793,8 @@ G &amp; = \{ V, E \} \newline
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/graph_operations.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/graph_operations.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="92">9.2 &nbsp; 图基础操作<a class="headerlink" href="#92" title="Permanent link">&para;</a></h1> <h1 id="92">9.2 &nbsp; 图基础操作<a class="headerlink" href="#92" title="Permanent link">&para;</a></h1>
<p>图的基础操作可分为对“边”的操作和对“顶点”的操作。在“邻接矩阵”和“邻接表”两种表示方法下,实现方式有所不同。</p> <p>图的基础操作可分为对“边”的操作和对“顶点”的操作。在“邻接矩阵”和“邻接表”两种表示方法下,实现方式有所不同。</p>
<h2 id="921">9.2.1 &nbsp; 基于邻接矩阵的实现<a class="headerlink" href="#921" title="Permanent link">&para;</a></h2> <h2 id="921">9.2.1 &nbsp; 基于邻接矩阵的实现<a class="headerlink" href="#921" title="Permanent link">&para;</a></h2>
@@ -5582,11 +5614,73 @@
</div> </div>
<p>观察表 9-2 ,似乎邻接表(哈希表)的时间与空间效率最优。但实际上,在邻接矩阵中操作边的效率更高,只需要一次数组访问或赋值操作即可。综合来看,邻接矩阵体现了“以空间换时间”的原则,而邻接表体现了“以时间换空间”的原则。</p> <p>观察表 9-2 ,似乎邻接表(哈希表)的时间与空间效率最优。但实际上,在邻接矩阵中操作边的效率更高,只需要一次数组访问或赋值操作即可。综合来看,邻接矩阵体现了“以空间换时间”的原则,而邻接表体现了“以时间换空间”的原则。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../graph/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 9.1 &amp;nbsp; 图"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
9.1 &nbsp;
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../graph_traversal/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 9.3 &amp;nbsp; 图的遍历"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.3 &nbsp; 图的遍历
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5652,48 +5746,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../graph/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 9.1 &amp;nbsp; 图" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9.1 &nbsp; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../graph_traversal/" class="md-footer__link md-footer__link--next" aria-label="下一页: 9.3 &amp;nbsp; 图的遍历" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.3 &nbsp; 图的遍历
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5704,6 +5781,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3510,7 +3510,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/graph_traversal.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/graph_traversal.md" title="编辑此页" class="md-content__button md-icon">
@@ -3521,6 +3546,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="93">9.3 &nbsp; 图的遍历<a class="headerlink" href="#93" title="Permanent link">&para;</a></h1> <h1 id="93">9.3 &nbsp; 图的遍历<a class="headerlink" href="#93" title="Permanent link">&para;</a></h1>
<p>树代表的是“一对多”的关系,而图则具有更高的自由度,可以表示任意的“多对多”关系。因此,我们可以把树看作是图的一种特例。显然,<strong>树的遍历操作也是图的遍历操作的一种特例</strong></p> <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>
@@ -4313,11 +4345,73 @@
<p><strong>时间复杂度:</strong> 所有顶点都会被访问 <span class="arithmatex">\(1\)</span> 次,使用 <span class="arithmatex">\(O(|V|)\)</span> 时间;所有边都会被访问 <span class="arithmatex">\(2\)</span> 次,使用 <span class="arithmatex">\(O(2|E|)\)</span> 时间;总体使用 <span class="arithmatex">\(O(|V| + |E|)\)</span> 时间。</p> <p><strong>时间复杂度:</strong> 所有顶点都会被访问 <span class="arithmatex">\(1\)</span> 次,使用 <span class="arithmatex">\(O(|V|)\)</span> 时间;所有边都会被访问 <span class="arithmatex">\(2\)</span> 次,使用 <span class="arithmatex">\(O(2|E|)\)</span> 时间;总体使用 <span class="arithmatex">\(O(|V| + |E|)\)</span> 时间。</p>
<p><strong>空间复杂度:</strong> 列表 <code>res</code> ,哈希表 <code>visited</code> 顶点数量最多为 <span class="arithmatex">\(|V|\)</span> ,递归深度最大为 <span class="arithmatex">\(|V|\)</span> ,因此使用 <span class="arithmatex">\(O(|V|)\)</span> 空间。</p> <p><strong>空间复杂度:</strong> 列表 <code>res</code> ,哈希表 <code>visited</code> 顶点数量最多为 <span class="arithmatex">\(|V|\)</span> ,递归深度最大为 <span class="arithmatex">\(|V|\)</span> ,因此使用 <span class="arithmatex">\(O(|V|)\)</span> 空间。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../graph_operations/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 9.2 &amp;nbsp; 图基础操作"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
9.2 &nbsp; 图基础操作
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 9.4 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4383,48 +4477,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../graph_operations/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 9.2 &amp;nbsp; 图基础操作" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9.2 &nbsp; 图基础操作 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 9.4 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4435,6 +4512,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="9">第 9 章 &nbsp;<a class="headerlink" href="#9" title="Permanent link">&para;</a></h1> <h1 id="9">第 9 章 &nbsp;<a class="headerlink" href="#9" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="图" src="../assets/covers/chapter_graph.jpg" width="600" /></p> <p><img alt="图" src="../assets/covers/chapter_graph.jpg" width="600" /></p>
@@ -3397,11 +3429,73 @@
<li><a href="https://www.hello-algo.com/chapter_graph/summary/">9.4 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_graph/summary/">9.4 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_heap/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 8.4 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
8.4 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="graph/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 9.1 &amp;nbsp; 图"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.1 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3467,48 +3561,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_heap/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.4 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8.4 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="graph/" class="md-footer__link md-footer__link--next" aria-label="下一页: 9.1 &amp;nbsp; 图" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.1 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3519,6 +3596,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="94">9.4 &nbsp; 小结<a class="headerlink" href="#94" title="Permanent link">&para;</a></h1> <h1 id="94">9.4 &nbsp; 小结<a class="headerlink" href="#94" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3471,11 +3503,73 @@
<p>可以是任意顺序。但在实际应用中,可能会需要按照指定规则来排序,比如按照顶点添加的次序、或者按照顶点值大小的顺序等等,这样可以有助于快速查找“带有某种极值”的顶点。</p> <p>可以是任意顺序。但在实际应用中,可能会需要按照指定规则来排序,比如按照顶点添加的次序、或者按照顶点值大小的顺序等等,这样可以有助于快速查找“带有某种极值”的顶点。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../graph_traversal/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 9.3 &amp;nbsp; 图的遍历"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
9.3 &nbsp; 图的遍历
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_searching/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 10 章 &amp;nbsp; 搜索"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 10 章 &nbsp; 搜索
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3541,48 +3635,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../graph_traversal/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 9.3 &amp;nbsp; 图的遍历" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9.3 &nbsp; 图的遍历 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_searching/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 10 章 &amp;nbsp; 搜索" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 10 章 &nbsp; 搜索
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3593,6 +3670,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3452,7 +3452,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/fractional_knapsack_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/fractional_knapsack_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3463,6 +3488,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="152">15.2 &nbsp; 分数背包问题<a class="headerlink" href="#152" title="Permanent link">&para;</a></h1> <h1 id="152">15.2 &nbsp; 分数背包问题<a class="headerlink" href="#152" title="Permanent link">&para;</a></h1>
<div class="admonition question"> <div class="admonition question">
<p class="admonition-title">Question</p> <p class="admonition-title">Question</p>
@@ -3883,11 +3915,73 @@
<p><img alt="分数背包问题的几何表示" src="../fractional_knapsack_problem.assets/fractional_knapsack_area_chart.png" /></p> <p><img alt="分数背包问题的几何表示" src="../fractional_knapsack_problem.assets/fractional_knapsack_area_chart.png" /></p>
<p align="center"> 图 15-6 &nbsp; 分数背包问题的几何表示 </p> <p align="center"> 图 15-6 &nbsp; 分数背包问题的几何表示 </p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../greedy_algorithm/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 15.1 &amp;nbsp; 贪心算法"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
15.1 &nbsp; 贪心算法
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../max_capacity_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 15.3 &amp;nbsp; 最大容量问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.3 &nbsp; 最大容量问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3953,48 +4047,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../greedy_algorithm/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 15.1 &amp;nbsp; 贪心算法" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15.1 &nbsp; 贪心算法 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../max_capacity_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 15.3 &amp;nbsp; 最大容量问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.3 &nbsp; 最大容量问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4005,6 +4082,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3466,7 +3466,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/greedy_algorithm.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/greedy_algorithm.md" title="编辑此页" class="md-content__button md-icon">
@@ -3477,6 +3502,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="151">15.1 &nbsp; 贪心算法<a class="headerlink" href="#151" title="Permanent link">&para;</a></h1> <h1 id="151">15.1 &nbsp; 贪心算法<a class="headerlink" href="#151" title="Permanent link">&para;</a></h1>
<p>「贪心算法 greedy algorithm」是一种常见的解决优化问题的算法,其基本思想是在问题的每个决策阶段,都选择当前看起来最优的选择,即贪心地做出局部最优的决策,以期望获得全局最优解。贪心算法简洁且高效,在许多实际问题中都有着广泛的应用。</p> <p>「贪心算法 greedy algorithm」是一种常见的解决优化问题的算法,其基本思想是在问题的每个决策阶段,都选择当前看起来最优的选择,即贪心地做出局部最优的决策,以期望获得全局最优解。贪心算法简洁且高效,在许多实际问题中都有着广泛的应用。</p>
<p>贪心算法和动态规划都常用于解决优化问题。它们之间存在一些相似之处,比如都依赖最优子结构性质,但工作原理是不同的。</p> <p>贪心算法和动态规划都常用于解决优化问题。它们之间存在一些相似之处,比如都依赖最优子结构性质,但工作原理是不同的。</p>
@@ -3795,11 +3827,73 @@
<li><strong>Dijkstra 算法</strong>:它是一种解决给定源顶点到其余各顶点的最短路径问题的贪心算法。</li> <li><strong>Dijkstra 算法</strong>:它是一种解决给定源顶点到其余各顶点的最短路径问题的贪心算法。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 15 章 &amp;nbsp; 贪心"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 15 章 &nbsp; 贪心
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../fractional_knapsack_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 15.2 &amp;nbsp; 分数背包问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.2 &nbsp; 分数背包问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3865,48 +3959,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 15 章 &amp;nbsp; 贪心" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 15 章 &nbsp; 贪心 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../fractional_knapsack_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 15.2 &amp;nbsp; 分数背包问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.2 &nbsp; 分数背包问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3917,6 +3994,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="15">第 15 章 &nbsp; 贪心<a class="headerlink" href="#15" title="Permanent link">&para;</a></h1> <h1 id="15">第 15 章 &nbsp; 贪心<a class="headerlink" href="#15" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="贪心" src="../assets/covers/chapter_greedy.jpg" width="600" /></p> <p><img alt="贪心" src="../assets/covers/chapter_greedy.jpg" width="600" /></p>
@@ -3398,11 +3430,73 @@
<li><a href="https://www.hello-algo.com/chapter_greedy/summary/">15.5 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_greedy/summary/">15.5 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_dynamic_programming/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 14.7 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
14.7 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="greedy_algorithm/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 15.1 &amp;nbsp; 贪心算法"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.1 &nbsp; 贪心算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3468,48 +3562,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_dynamic_programming/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 14.7 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14.7 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="greedy_algorithm/" class="md-footer__link md-footer__link--next" aria-label="下一页: 15.1 &amp;nbsp; 贪心算法" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.1 &nbsp; 贪心算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3520,6 +3597,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3452,7 +3452,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/max_capacity_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/max_capacity_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3463,6 +3488,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="153">15.3 &nbsp; 最大容量问题<a class="headerlink" href="#153" title="Permanent link">&para;</a></h1> <h1 id="153">15.3 &nbsp; 最大容量问题<a class="headerlink" href="#153" title="Permanent link">&para;</a></h1>
<div class="admonition question"> <div class="admonition question">
<p class="admonition-title">Question</p> <p class="admonition-title">Question</p>
@@ -3791,11 +3823,73 @@ cap[i, i+1], cap[i, i+2], \dots, cap[i, j-2], cap[i, j-1]
<p>观察发现,<strong>这些被跳过的状态实际上就是将长板 <span class="arithmatex">\(j\)</span> 向内移动的所有状态</strong>。而在第二步中,我们已经证明内移长板一定会导致容量变小。也就是说,被跳过的状态都不可能是最优解,<strong>跳过它们不会导致错过最优解</strong></p> <p>观察发现,<strong>这些被跳过的状态实际上就是将长板 <span class="arithmatex">\(j\)</span> 向内移动的所有状态</strong>。而在第二步中,我们已经证明内移长板一定会导致容量变小。也就是说,被跳过的状态都不可能是最优解,<strong>跳过它们不会导致错过最优解</strong></p>
<p>以上的分析说明,<strong>移动短板的操作是“安全”的</strong>,贪心策略是有效的。</p> <p>以上的分析说明,<strong>移动短板的操作是“安全”的</strong>,贪心策略是有效的。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../fractional_knapsack_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 15.2 &amp;nbsp; 分数背包问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
15.2 &nbsp; 分数背包问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../max_product_cutting_problem/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 15.4 &amp;nbsp; 最大切分乘积问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.4 &nbsp; 最大切分乘积问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3861,48 +3955,31 @@ cap[i, i+1], cap[i, i+2], \dots, cap[i, j-2], cap[i, j-1]
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../fractional_knapsack_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 15.2 &amp;nbsp; 分数背包问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15.2 &nbsp; 分数背包问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../max_product_cutting_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 15.4 &amp;nbsp; 最大切分乘积问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.4 &nbsp; 最大切分乘积问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3913,6 +3990,8 @@ cap[i, i+1], cap[i, i+2], \dots, cap[i, j-2], cap[i, j-1]
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3452,7 +3452,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/max_product_cutting_problem.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/max_product_cutting_problem.md" title="编辑此页" class="md-content__button md-icon">
@@ -3463,6 +3488,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="154">15.4 &nbsp; 最大切分乘积问题<a class="headerlink" href="#154" title="Permanent link">&para;</a></h1> <h1 id="154">15.4 &nbsp; 最大切分乘积问题<a class="headerlink" href="#154" title="Permanent link">&para;</a></h1>
<div class="admonition question"> <div class="admonition question">
<p class="admonition-title">Question</p> <p class="admonition-title">Question</p>
@@ -3767,11 +3799,73 @@ n = 3 a + b
<li><strong>切分方案最多包含两个 <span class="arithmatex">\(2\)</span></strong> :假设最优切分方案中包含三个 <span class="arithmatex">\(2\)</span> ,那么一定可以替换为两个 <span class="arithmatex">\(3\)</span> ,乘积更大。这与假设矛盾。</li> <li><strong>切分方案最多包含两个 <span class="arithmatex">\(2\)</span></strong> :假设最优切分方案中包含三个 <span class="arithmatex">\(2\)</span> ,那么一定可以替换为两个 <span class="arithmatex">\(3\)</span> ,乘积更大。这与假设矛盾。</li>
</ol> </ol>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../max_capacity_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 15.3 &amp;nbsp; 最大容量问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
15.3 &nbsp; 最大容量问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 15.5 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3837,48 +3931,31 @@ n = 3 a + b
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../max_capacity_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 15.3 &amp;nbsp; 最大容量问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15.3 &nbsp; 最大容量问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 15.5 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
15.5 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3889,6 +3966,8 @@ n = 3 a + b
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_greedy/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="155">15.5 &nbsp; 小结<a class="headerlink" href="#155" title="Permanent link">&para;</a></h1> <h1 id="155">15.5 &nbsp; 小结<a class="headerlink" href="#155" title="Permanent link">&para;</a></h1>
<ul> <ul>
<li>贪心算法通常用于解决最优化问题,其原理是在每个决策阶段都做出局部最优的决策,以期望获得全局最优解。</li> <li>贪心算法通常用于解决最优化问题,其原理是在每个决策阶段都做出局部最优的决策,以期望获得全局最优解。</li>
@@ -3389,11 +3421,73 @@
<li>在最大切分乘积问题中,我们先后推理出两个贪心策略:<span class="arithmatex">\(\geq 4\)</span> 的整数都应该继续切分、最优切分因子为 <span class="arithmatex">\(3\)</span> 。代码中包含幂运算,时间复杂度取决于幂运算实现方法,通常为 <span class="arithmatex">\(O(1)\)</span><span class="arithmatex">\(O(\log n)\)</span></li> <li>在最大切分乘积问题中,我们先后推理出两个贪心策略:<span class="arithmatex">\(\geq 4\)</span> 的整数都应该继续切分、最优切分因子为 <span class="arithmatex">\(3\)</span> 。代码中包含幂运算,时间复杂度取决于幂运算实现方法,通常为 <span class="arithmatex">\(O(1)\)</span><span class="arithmatex">\(O(\log n)\)</span></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../max_product_cutting_problem/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 15.4 &amp;nbsp; 最大切分乘积问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
15.4 &nbsp; 最大切分乘积问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_appendix/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 16 章 &amp;nbsp; 附录"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 16 章 &nbsp; 附录
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3459,48 +3553,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../max_product_cutting_problem/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 15.4 &amp;nbsp; 最大切分乘积问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15.4 &nbsp; 最大切分乘积问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_appendix/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 16 章 &amp;nbsp; 附录" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 16 章 &nbsp; 附录
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3511,6 +3588,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+125 -46
View File
@@ -3458,7 +3458,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/hash_algorithm.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/hash_algorithm.md" title="编辑此页" class="md-content__button md-icon">
@@ -3469,11 +3494,18 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="63">6.3 &nbsp; 哈希算法<a class="headerlink" href="#63" title="Permanent link">&para;</a></h1> <h1 id="63">6.3 &nbsp; 哈希算法<a class="headerlink" href="#63" title="Permanent link">&para;</a></h1>
<p>在上两节中,我们了解了哈希表的工作原理和哈希冲突的处理方法。然而无论是开放寻址还是链地址法,<strong>它们只能保证哈希表可以在发生冲突时正常工作,但无法减少哈希冲突的发生</strong></p> <p>在上两节中,我们了解了哈希表的工作原理和哈希冲突的处理方法。然而无论是开放寻址还是链地址法,<strong>它们只能保证哈希表可以在发生冲突时正常工作,但无法减少哈希冲突的发生</strong></p>
<p>如果哈希冲突过于频繁,哈希表的性能则会急剧劣化。如图 6-7 所示,对于链地址哈希表,理想情况下键值对平均分布在各个桶中,达到最佳查询效率;最差情况下所有键值对都被存储到同一个桶中,时间复杂度退化至 <span class="arithmatex">\(O(n)\)</span></p> <p>如果哈希冲突过于频繁,哈希表的性能则会急剧劣化。如图 6-8 所示,对于链地址哈希表,理想情况下键值对平均分布在各个桶中,达到最佳查询效率;最差情况下所有键值对都被存储到同一个桶中,时间复杂度退化至 <span class="arithmatex">\(O(n)\)</span></p>
<p><img alt="哈希冲突的最佳与最差情况" src="../hash_algorithm.assets/hash_collision_best_worst_condition.png" /></p> <p><img alt="哈希冲突的最佳与最差情况" src="../hash_algorithm.assets/hash_collision_best_worst_condition.png" /></p>
<p align="center"> 图 6-7 &nbsp; 哈希冲突的最佳与最差情况 </p> <p align="center"> 图 6-8 &nbsp; 哈希冲突的最佳与最差情况 </p>
<p><strong>键值对的分布情况由哈希函数决定</strong>。回忆哈希函数的计算步骤,先计算哈希值,再对数组长度取模:</p> <p><strong>键值对的分布情况由哈希函数决定</strong>。回忆哈希函数的计算步骤,先计算哈希值,再对数组长度取模:</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="nv">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hash<span class="o">(</span>key<span class="o">)</span><span class="w"> </span>%<span class="w"> </span>capacity <div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="nv">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hash<span class="o">(</span>key<span class="o">)</span><span class="w"> </span>%<span class="w"> </span>capacity
@@ -4276,11 +4308,73 @@
<p>虽然自定义对象(比如链表节点)的成员变量是可变的,但它是可哈希的。<strong>这是因为对象的哈希值通常是基于内存地址生成的</strong>,即使对象的内容发生了变化,但它的内存地址不变,哈希值仍然是不变的。</p> <p>虽然自定义对象(比如链表节点)的成员变量是可变的,但它是可哈希的。<strong>这是因为对象的哈希值通常是基于内存地址生成的</strong>,即使对象的内容发生了变化,但它的内存地址不变,哈希值仍然是不变的。</p>
<p>细心的你可能发现在不同控制台中运行程序时,输出的哈希值是不同的。<strong>这是因为 Python 解释器在每次启动时,都会为字符串哈希函数加入一个随机的盐(Salt)值</strong>。这种做法可以有效防止 HashDoS 攻击,提升哈希算法的安全性。</p> <p>细心的你可能发现在不同控制台中运行程序时,输出的哈希值是不同的。<strong>这是因为 Python 解释器在每次启动时,都会为字符串哈希函数加入一个随机的盐(Salt)值</strong>。这种做法可以有效防止 HashDoS 攻击,提升哈希算法的安全性。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../hash_collision/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 6.2 &amp;nbsp; 哈希冲突"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
6.2 &nbsp; 哈希冲突
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 6.4 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4346,48 +4440,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../hash_collision/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 6.2 &amp;nbsp; 哈希冲突" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
6.2 &nbsp; 哈希冲突 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 6.4 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4398,6 +4475,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

+509 -364
View File
@@ -1303,7 +1303,14 @@
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#2" class="md-nav__link"> <a href="#2" class="md-nav__link">
2. &nbsp; 多次哈希 2. &nbsp; 平方探测
</a>
</li>
<li class="md-nav__item">
<a href="#3" class="md-nav__link">
3. &nbsp; 多次哈希
</a> </a>
</li> </li>
@@ -3455,7 +3462,14 @@
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#2" class="md-nav__link"> <a href="#2" class="md-nav__link">
2. &nbsp; 多次哈希 2. &nbsp; 平方探测
</a>
</li>
<li class="md-nav__item">
<a href="#3" class="md-nav__link">
3. &nbsp; 多次哈希
</a> </a>
</li> </li>
@@ -3484,7 +3498,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/hash_collision.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/hash_collision.md" title="编辑此页" class="md-content__button md-icon">
@@ -3495,8 +3534,15 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="62">6.2 &nbsp; 哈希冲突<a class="headerlink" href="#62" title="Permanent link">&para;</a></h1> <h1 id="62">6.2 &nbsp; 哈希冲突<a class="headerlink" href="#62" title="Permanent link">&para;</a></h1>
<p>上节提到,<strong>通常情况下哈希函数的输入空间远大于输出空间</strong>,因此理论上哈希冲突是不可避免的。比如,输入空间为全体整数,输出空间为数组容量大小,则必然有多个整数映射至同一数组索引。</p> <p>上节提到,<strong>通常情况下哈希函数的输入空间远大于输出空间</strong>,因此理论上哈希冲突是不可避免的。比如,输入空间为全体整数,输出空间为数组容量大小,则必然有多个整数映射至同一索引。</p>
<p>哈希冲突会导致查询结果错误,严重影响哈希表的可用性。为解决该问题,我们可以每当遇到哈希冲突时就进行哈希表扩容,直至冲突消失为止。此方法简单粗暴且有效,但效率太低,因为哈希表扩容需要进行大量的数据搬运与哈希值计算。为了提升效率,我们可以采用以下策略。</p> <p>哈希冲突会导致查询结果错误,严重影响哈希表的可用性。为解决该问题,我们可以每当遇到哈希冲突时就进行哈希表扩容,直至冲突消失为止。此方法简单粗暴且有效,但效率太低,因为哈希表扩容需要进行大量的数据搬运与哈希值计算。为了提升效率,我们可以采用以下策略。</p>
<ol> <ol>
<li>改良哈希表数据结构,<strong>使得哈希表可以在存在哈希冲突时正常工作</strong></li> <li>改良哈希表数据结构,<strong>使得哈希表可以在存在哈希冲突时正常工作</strong></li>
@@ -3508,9 +3554,9 @@
<p><img alt="链式地址哈希表" src="../hash_collision.assets/hash_table_chaining.png" /></p> <p><img alt="链式地址哈希表" src="../hash_collision.assets/hash_table_chaining.png" /></p>
<p align="center"> 图 6-5 &nbsp; 链式地址哈希表 </p> <p align="center"> 图 6-5 &nbsp; 链式地址哈希表 </p>
<p>哈希表在链式地址下的操作方法发生了一些变化。</p> <p>基于链式地址实现的哈希表的操作方法发生了以下变化。</p>
<ul> <ul>
<li><strong>查询元素</strong>:输入 <code>key</code> ,经过哈希函数得到数组索引,即可访问链表头节点,然后遍历链表并对比 <code>key</code> 以查找目标键值对。</li> <li><strong>查询元素</strong>:输入 <code>key</code> ,经过哈希函数得到索引,即可访问链表头节点,然后遍历链表并对比 <code>key</code> 以查找目标键值对。</li>
<li><strong>添加元素</strong>:先通过哈希函数访问链表头节点,然后将节点(即键值对)添加到链表中。</li> <li><strong>添加元素</strong>:先通过哈希函数访问链表头节点,然后将节点(即键值对)添加到链表中。</li>
<li><strong>删除元素</strong>:根据哈希函数的结果访问链表头部,接着遍历链表以查找目标节点,并将其删除。</li> <li><strong>删除元素</strong>:根据哈希函数的结果访问链表头部,接着遍历链表以查找目标节点,并将其删除。</li>
</ul> </ul>
@@ -4800,26 +4846,26 @@
<p>值得注意的是,当链表很长时,查询效率 <span class="arithmatex">\(O(n)\)</span> 很差。<strong>此时可以将链表转换为“AVL 树”或“红黑树”</strong>,从而将查询操作的时间复杂度优化至 <span class="arithmatex">\(O(\log n)\)</span></p> <p>值得注意的是,当链表很长时,查询效率 <span class="arithmatex">\(O(n)\)</span> 很差。<strong>此时可以将链表转换为“AVL 树”或“红黑树”</strong>,从而将查询操作的时间复杂度优化至 <span class="arithmatex">\(O(\log n)\)</span></p>
<h2 id="622">6.2.2 &nbsp; 开放寻址<a class="headerlink" href="#622" title="Permanent link">&para;</a></h2> <h2 id="622">6.2.2 &nbsp; 开放寻址<a class="headerlink" href="#622" title="Permanent link">&para;</a></h2>
<p>「开放寻址 open addressing」不引入额外的数据结构,而是通过“多次探测”来处理哈希冲突,探测方式主要包括线性探测、平方探测、多次哈希等。</p> <p>「开放寻址 open addressing」不引入额外的数据结构,而是通过“多次探测”来处理哈希冲突,探测方式主要包括线性探测、平方探测、多次哈希等。</p>
<p>下面将主要以线性探测为例,介绍开放寻址哈希表的工作机制与代码实现。</p>
<h3 id="1">1. &nbsp; 线性探测<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 线性探测<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<p>线性探测采用固定步长的线性搜索来进行探测,其操作方法与普通哈希表有所不同。</p> <p>线性探测采用固定步长的线性搜索来进行探测,其操作方法与普通哈希表有所不同。</p>
<ul> <ul>
<li><strong>插入元素</strong>:通过哈希函数计算数组索引,若发现桶内已有元素,则从冲突位置向后线性遍历(步长通常为 <span class="arithmatex">\(1\)</span> ),直至找到空,将元素插入其中。</li> <li><strong>插入元素</strong>:通过哈希函数计算索引,若发现桶内已有元素,则从冲突位置向后线性遍历(步长通常为 <span class="arithmatex">\(1\)</span> ),直至找到空,将元素插入其中。</li>
<li><strong>查找元素</strong>:若发现哈希冲突,则使用相同步长向后线性遍历,直到找到对应元素,返回 <code>value</code> 即可;如果遇到空,说明目标键值对不在哈希表中,返回 <span class="arithmatex">\(\text{None}\)</span></li> <li><strong>查找元素</strong>:若发现哈希冲突,则使用相同步长向后线性遍历,直到找到对应元素,返回 <code>value</code> 即可;如果遇到空,说明目标元素不在哈希表中,返回 <span class="arithmatex">\(\text{None}\)</span></li>
</ul> </ul>
<p>图 6-6 展示了一个在开放寻址(线性探测)下工作的哈希表。</p> <p>图 6-6 展示了开放寻址(线性探测)哈希表的键值对分布。根据此哈希函数,最后两位相同的 <code>key</code> 都会被映射到相同的桶。而通过线性探测,它们被依次存储在该桶以及之下的桶中</p>
<p><img alt="开放寻址和线性探测" src="../hash_collision.assets/hash_table_linear_probing.png" /></p> <p><img alt="开放寻址和线性探测" src="../hash_collision.assets/hash_table_linear_probing.png" /></p>
<p align="center"> 图 6-6 &nbsp; 开放寻址和线性探测 </p> <p align="center"> 图 6-6 &nbsp; 开放寻址和线性探测 </p>
<p>然而,线性探测存在以下缺陷</p> <p>然而,<strong>线性探测容易产生“聚集现象”</strong>。具体来说,数组中连续被占用的位置越长,这些连续位置发生哈希冲突的可能性越大,从而进一步促使该位置的聚堆生长,形成恶性循环,最终导致增删查改操作效率劣化</p>
<ul> <p>值得注意的是,<strong>我们不能在开放寻址哈希表中直接删除元素</strong>。这是因为删除元素会在数组内产生一个空桶 <span class="arithmatex">\(\text{None}\)</span> ,而当查询元素时,线性探测到该空桶就会返回,因此在该空桶之下的元素都无法再被访问到,程序可能误判这些元素不存在。</p>
<li><strong>不能直接删除元素</strong>。删除元素会在数组内产生一个空位,当查找该空位之后的元素时,该空位可能导致程序误判元素不存在。为此,通常需要借助一个标志位来标记已删除元素。</li> <p><img alt="在开放寻址中删除元素导致的查询问题" src="../hash_collision.assets/hash_table_open_addressing_deletion.png" /></p>
<li><strong>容易产生聚集</strong>。数组内连续被占用位置越长,这些连续位置发生哈希冲突的可能性越大,进一步促使这一位置的聚堆生长,形成恶性循环,最终导致增删查改操作效率劣化。</li> <p align="center"> 图 6-7 &nbsp; 在开放寻址中删除元素导致的查询问题 </p>
</ul>
<p>以下代码实现了一个简单的开放寻址(线性探测)哈希表</p> <p>为了解决该问题,我们可以采用「懒删除 lazy deletion」机制:它不直接从哈希表中移除元素,<strong>而是利用一个常量 <code>TOMBSTONE</code> 来标记这个桶</strong>。在该机制下,<span class="arithmatex">\(\text{None}\)</span><code>TOMBSTONE</code> 都代表空桶,都可以放置键值对。但不同的是,线性探测到 <code>TOMBSTONE</code> 时应该继续遍历,因为其之下可能还存在键值对</p>
<ul> <p>然而,<strong>懒删除可能会加速哈希表的性能退化</strong>。这是因为每次删除操作都会产生一个删除标记,随着 <code>TOMBSTONE</code> 的增加,搜索时间也会增加,因为线性探测可能需要跳过多个 <code>TOMBSTONE</code> 才能找到目标元素。</p>
<li>我们使用一个固定的键值对实例 <code>removed</code> 来标记已删除元素。也就是说,当一个桶内的元素为 <span class="arithmatex">\(\text{None}\)</span><code>removed</code> 时,说明这个桶是空的,可用于放置键值对</li> <p>为此,考虑在线性探测中记录遇到的首个 <code>TOMBSTONE</code> 的索引,并将搜索到的目标元素与该 <code>TOMBSTONE</code> 交换位置。这样做的好处是当每次查询或添加元素时,元素会被移动至距离理想位置(探测起始点)更近的桶,从而优化查询效率</p>
<li>在线性探测时,我们从当前索引 <code>index</code> 向后遍历;而当越过数组尾部时,需要回到头部继续遍历。</li> <p>以下代码实现了一个包含懒删除的开放寻址(线性探测)哈希表。为了更加充分地使用哈希表的空间,我们将哈希表表看作是一个“环形数组”,当越过数组尾部时,回到头部继续遍历。</p>
</ul>
<div class="tabbed-set tabbed-alternate" data-tabs="2:12"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Zig</label></div> <div class="tabbed-set tabbed-alternate" data-tabs="2:12"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Zig</label></div>
<div class="tabbed-content"> <div class="tabbed-content">
<div class="tabbed-block"> <div class="tabbed-block">
@@ -4833,7 +4879,7 @@
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a> <span class="bp">self</span><span class="o">.</span><span class="n">load_thres</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">/</span> <span class="mi">3</span> <span class="c1"># 触发扩容的负载因子阈值</span> <a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a> <span class="bp">self</span><span class="o">.</span><span class="n">load_thres</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">/</span> <span class="mi">3</span> <span class="c1"># 触发扩容的负载因子阈值</span>
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a> <span class="bp">self</span><span class="o">.</span><span class="n">extend_ratio</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># 扩容倍数</span> <a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a> <span class="bp">self</span><span class="o">.</span><span class="n">extend_ratio</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># 扩容倍数</span>
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">Pair</span> <span class="o">|</span> <span class="kc">None</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <span class="c1"># 桶数组</span> <a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">Pair</span> <span class="o">|</span> <span class="kc">None</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <span class="c1"># 桶数组</span>
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a> <span class="bp">self</span><span class="o">.</span><span class="n">removed</span> <span class="o">=</span> <span class="n">Pair</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;-1&quot;</span><span class="p">)</span> <span class="c1"># 删除标记</span> <a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a> <span class="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span> <span class="o">=</span> <span class="n">Pair</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;-1&quot;</span><span class="p">)</span> <span class="c1"># 删除标记</span>
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a> <a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a>
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a> <span class="k">def</span> <span class="nf">hash_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span> <a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a> <span class="k">def</span> <span class="nf">hash_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;哈希函数&quot;&quot;&quot;</span> <a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;哈希函数&quot;&quot;&quot;</span>
@@ -4843,320 +4889,353 @@
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;负载因子&quot;&quot;&quot;</span> <a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;负载因子&quot;&quot;&quot;</span>
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span>
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a> <a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a>
<a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a> <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span> <a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a> <span class="k">def</span> <span class="nf">find_bucket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;查询操作&quot;&quot;&quot;</span> <a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;搜索 key 对应的桶索引&quot;&quot;&quot;</span>
<a id="__codelineno-12-23" name="__codelineno-12-23" href="#__codelineno-12-23"></a> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hash_func</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <a id="__codelineno-12-23" name="__codelineno-12-23" href="#__codelineno-12-23"></a> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hash_func</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<a id="__codelineno-12-24" name="__codelineno-12-24" href="#__codelineno-12-24"></a> <span class="c1"># 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-12-24" name="__codelineno-12-24" href="#__codelineno-12-24"></a> <span class="n">first_tombstone</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
<a id="__codelineno-12-25" name="__codelineno-12-25" href="#__codelineno-12-25"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">capacity</span><span class="p">):</span> <a id="__codelineno-12-25" name="__codelineno-12-25" href="#__codelineno-12-25"></a> <span class="c1"># 线性探测,当遇到空桶时跳出</span>
<a id="__codelineno-12-26" name="__codelineno-12-26" href="#__codelineno-12-26"></a> <span class="c1"># 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-12-26" name="__codelineno-12-26" href="#__codelineno-12-26"></a> <span class="k">while</span> <span class="bp">self</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="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<a id="__codelineno-12-27" name="__codelineno-12-27" href="#__codelineno-12-27"></a> <span class="n">j</span> <span class="o">=</span> <span class="p">(</span><span class="n">index</span> <span class="o">+</span> <span class="n">i</span><span class="p">)</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <a id="__codelineno-12-27" name="__codelineno-12-27" href="#__codelineno-12-27"></a> <span class="c1"># 若遇到 key ,返回对应桶索引</span>
<a id="__codelineno-12-28" name="__codelineno-12-28" href="#__codelineno-12-28"></a> <span class="c1"># 若遇到空桶,说明无此 key ,则返回 None</span> <a id="__codelineno-12-28" name="__codelineno-12-28" href="#__codelineno-12-28"></a> <span class="k">if</span> <span class="bp">self</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">key</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span>
<a id="__codelineno-12-29" name="__codelineno-12-29" href="#__codelineno-12-29"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <a id="__codelineno-12-29" name="__codelineno-12-29" href="#__codelineno-12-29"></a> <span class="c1"># 若之前遇到了删除标记,则将键值对移动至该索引</span>
<a id="__codelineno-12-30" name="__codelineno-12-30" href="#__codelineno-12-30"></a> <span class="k">return</span> <span class="kc">None</span> <a id="__codelineno-12-30" name="__codelineno-12-30" href="#__codelineno-12-30"></a> <span class="k">if</span> <span class="n">first_tombstone</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
<a id="__codelineno-12-31" name="__codelineno-12-31" href="#__codelineno-12-31"></a> <span class="c1"># 若遇到指定 key ,则返回对应 val</span> <a id="__codelineno-12-31" name="__codelineno-12-31" href="#__codelineno-12-31"></a> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">[</span><span class="n">first_tombstone</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</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-12-32" name="__codelineno-12-32" href="#__codelineno-12-32"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</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="n">key</span> <span class="o">==</span> <span class="n">key</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</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="bp">self</span><span class="o">.</span><span class="n">removed</span><span class="p">:</span> <a id="__codelineno-12-32" name="__codelineno-12-32" href="#__codelineno-12-32"></a> <span class="bp">self</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="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span>
<a id="__codelineno-12-33" name="__codelineno-12-33" href="#__codelineno-12-33"></a> <span class="k">return</span> <span class="bp">self</span><span class="o">.</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="n">val</span> <a id="__codelineno-12-33" name="__codelineno-12-33" href="#__codelineno-12-33"></a> <span class="k">return</span> <span class="n">first_tombstone</span> <span class="c1"># 返回移动后的桶索引</span>
<a id="__codelineno-12-34" name="__codelineno-12-34" href="#__codelineno-12-34"></a> <a id="__codelineno-12-34" name="__codelineno-12-34" href="#__codelineno-12-34"></a> <span class="k">return</span> <span class="n">index</span> <span class="c1"># 返回桶索引</span>
<a id="__codelineno-12-35" name="__codelineno-12-35" href="#__codelineno-12-35"></a> <span class="k">def</span> <span class="nf">put</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span> <a id="__codelineno-12-35" name="__codelineno-12-35" href="#__codelineno-12-35"></a> <span class="c1"># 记录遇到的首个删除标记</span>
<a id="__codelineno-12-36" name="__codelineno-12-36" href="#__codelineno-12-36"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;添加操作&quot;&quot;&quot;</span> <a id="__codelineno-12-36" name="__codelineno-12-36" href="#__codelineno-12-36"></a> <span class="k">if</span> <span class="n">first_tombstone</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">and</span> <span class="bp">self</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="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span><span class="p">:</span>
<a id="__codelineno-12-37" name="__codelineno-12-37" href="#__codelineno-12-37"></a> <span class="c1"># 当负载因子超过阈值时,执行扩容</span> <a id="__codelineno-12-37" name="__codelineno-12-37" href="#__codelineno-12-37"></a> <span class="n">first_tombstone</span> <span class="o">=</span> <span class="n">index</span>
<a id="__codelineno-12-38" name="__codelineno-12-38" href="#__codelineno-12-38"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_factor</span><span class="p">()</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_thres</span><span class="p">:</span> <a id="__codelineno-12-38" name="__codelineno-12-38" href="#__codelineno-12-38"></a> <span class="c1"># 计算桶索引,越过尾部返回头部</span>
<a id="__codelineno-12-39" name="__codelineno-12-39" href="#__codelineno-12-39"></a> <span class="bp">self</span><span class="o">.</span><span class="n">extend</span><span class="p">()</span> <a id="__codelineno-12-39" name="__codelineno-12-39" href="#__codelineno-12-39"></a> <span class="n">index</span> <span class="o">=</span> <span class="p">(</span><span class="n">index</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span>
<a id="__codelineno-12-40" name="__codelineno-12-40" href="#__codelineno-12-40"></a> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hash_func</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <a id="__codelineno-12-40" name="__codelineno-12-40" href="#__codelineno-12-40"></a> <span class="c1"># 若 key 不存在,则返回添加点的索引</span>
<a id="__codelineno-12-41" name="__codelineno-12-41" href="#__codelineno-12-41"></a> <span class="c1"># 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-12-41" name="__codelineno-12-41" href="#__codelineno-12-41"></a> <span class="k">return</span> <span class="n">index</span> <span class="k">if</span> <span class="n">first_tombstone</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="k">else</span> <span class="n">first_tombstone</span>
<a id="__codelineno-12-42" name="__codelineno-12-42" href="#__codelineno-12-42"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">capacity</span><span class="p">):</span> <a id="__codelineno-12-42" name="__codelineno-12-42" href="#__codelineno-12-42"></a>
<a id="__codelineno-12-43" name="__codelineno-12-43" href="#__codelineno-12-43"></a> <span class="c1"># 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-12-43" name="__codelineno-12-43" href="#__codelineno-12-43"></a> <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<a id="__codelineno-12-44" name="__codelineno-12-44" href="#__codelineno-12-44"></a> <span class="n">j</span> <span class="o">=</span> <span class="p">(</span><span class="n">index</span> <span class="o">+</span> <span class="n">i</span><span class="p">)</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <a id="__codelineno-12-44" name="__codelineno-12-44" href="#__codelineno-12-44"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;查询操作&quot;&quot;&quot;</span>
<a id="__codelineno-12-45" name="__codelineno-12-45" href="#__codelineno-12-45"></a> <span class="c1"># 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶</span> <a id="__codelineno-12-45" name="__codelineno-12-45" href="#__codelineno-12-45"></a> <span class="c1"># 搜索 key 对应的桶索引</span>
<a id="__codelineno-12-46" name="__codelineno-12-46" href="#__codelineno-12-46"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">removed</span><span class="p">]:</span> <a id="__codelineno-12-46" name="__codelineno-12-46" href="#__codelineno-12-46"></a> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">find_bucket</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<a id="__codelineno-12-47" name="__codelineno-12-47" href="#__codelineno-12-47"></a> <span class="bp">self</span><span class="o">.</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="n">Pair</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span> <a id="__codelineno-12-47" name="__codelineno-12-47" href="#__codelineno-12-47"></a> <span class="c1"># 若找到键值对,则返回对应 val</span>
<a id="__codelineno-12-48" name="__codelineno-12-48" href="#__codelineno-12-48"></a> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">+=</span> <span class="mi">1</span> <a id="__codelineno-12-48" name="__codelineno-12-48" href="#__codelineno-12-48"></a> <span class="k">if</span> <span class="bp">self</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="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span><span class="p">]:</span>
<a id="__codelineno-12-49" name="__codelineno-12-49" href="#__codelineno-12-49"></a> <span class="k">return</span> <a id="__codelineno-12-49" name="__codelineno-12-49" href="#__codelineno-12-49"></a> <span class="k">return</span> <span class="bp">self</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>
<a id="__codelineno-12-50" name="__codelineno-12-50" href="#__codelineno-12-50"></a> <span class="c1"># 若遇到指定 key ,则更新对应 val</span> <a id="__codelineno-12-50" name="__codelineno-12-50" href="#__codelineno-12-50"></a> <span class="c1"># 若键值对不存在,则返回 None</span>
<a id="__codelineno-12-51" name="__codelineno-12-51" href="#__codelineno-12-51"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</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="n">key</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span> <a id="__codelineno-12-51" name="__codelineno-12-51" href="#__codelineno-12-51"></a> <span class="k">return</span> <span class="kc">None</span>
<a id="__codelineno-12-52" name="__codelineno-12-52" href="#__codelineno-12-52"></a> <span class="bp">self</span><span class="o">.</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="n">val</span> <span class="o">=</span> <span class="n">val</span> <a id="__codelineno-12-52" name="__codelineno-12-52" href="#__codelineno-12-52"></a>
<a id="__codelineno-12-53" name="__codelineno-12-53" href="#__codelineno-12-53"></a> <span class="k">return</span> <a id="__codelineno-12-53" name="__codelineno-12-53" href="#__codelineno-12-53"></a> <span class="k">def</span> <span class="nf">put</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<a id="__codelineno-12-54" name="__codelineno-12-54" href="#__codelineno-12-54"></a> <a id="__codelineno-12-54" name="__codelineno-12-54" href="#__codelineno-12-54"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;添加操作&quot;&quot;&quot;</span>
<a id="__codelineno-12-55" name="__codelineno-12-55" href="#__codelineno-12-55"></a> <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span> <a id="__codelineno-12-55" name="__codelineno-12-55" href="#__codelineno-12-55"></a> <span class="c1"># 当负载因子超过阈值时,执行扩容</span>
<a id="__codelineno-12-56" name="__codelineno-12-56" href="#__codelineno-12-56"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;删除操作&quot;&quot;&quot;</span> <a id="__codelineno-12-56" name="__codelineno-12-56" href="#__codelineno-12-56"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_factor</span><span class="p">()</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_thres</span><span class="p">:</span>
<a id="__codelineno-12-57" name="__codelineno-12-57" href="#__codelineno-12-57"></a> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hash_func</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <a id="__codelineno-12-57" name="__codelineno-12-57" href="#__codelineno-12-57"></a> <span class="bp">self</span><span class="o">.</span><span class="n">extend</span><span class="p">()</span>
<a id="__codelineno-12-58" name="__codelineno-12-58" href="#__codelineno-12-58"></a> <span class="c1"># 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-12-58" name="__codelineno-12-58" href="#__codelineno-12-58"></a> <span class="c1"># 搜索 key 对应的桶索引</span>
<a id="__codelineno-12-59" name="__codelineno-12-59" href="#__codelineno-12-59"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">capacity</span><span class="p">):</span> <a id="__codelineno-12-59" name="__codelineno-12-59" href="#__codelineno-12-59"></a> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">find_bucket</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<a id="__codelineno-12-60" name="__codelineno-12-60" href="#__codelineno-12-60"></a> <span class="c1"># 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-12-60" name="__codelineno-12-60" href="#__codelineno-12-60"></a> <span class="c1"># 若找到键值对,则覆盖 val 并返回</span>
<a id="__codelineno-12-61" name="__codelineno-12-61" href="#__codelineno-12-61"></a> <span class="n">j</span> <span class="o">=</span> <span class="p">(</span><span class="n">index</span> <span class="o">+</span> <span class="n">i</span><span class="p">)</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <a id="__codelineno-12-61" name="__codelineno-12-61" href="#__codelineno-12-61"></a> <span class="k">if</span> <span class="bp">self</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="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span><span class="p">]:</span>
<a id="__codelineno-12-62" name="__codelineno-12-62" href="#__codelineno-12-62"></a> <span class="c1"># 若遇到空桶,说明无此 key ,则直接返回</span> <a id="__codelineno-12-62" name="__codelineno-12-62" href="#__codelineno-12-62"></a> <span class="bp">self</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="o">=</span> <span class="n">val</span>
<a id="__codelineno-12-63" name="__codelineno-12-63" href="#__codelineno-12-63"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <a id="__codelineno-12-63" name="__codelineno-12-63" href="#__codelineno-12-63"></a> <span class="k">return</span>
<a id="__codelineno-12-64" name="__codelineno-12-64" href="#__codelineno-12-64"></a> <span class="k">return</span> <a id="__codelineno-12-64" name="__codelineno-12-64" href="#__codelineno-12-64"></a> <span class="c1"># 若键值对不存在,则添加该键值对</span>
<a id="__codelineno-12-65" name="__codelineno-12-65" href="#__codelineno-12-65"></a> <span class="c1"># 若遇到指定 key ,则标记删除并返回</span> <a id="__codelineno-12-65" name="__codelineno-12-65" href="#__codelineno-12-65"></a> <span class="bp">self</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">Pair</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
<a id="__codelineno-12-66" name="__codelineno-12-66" href="#__codelineno-12-66"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</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="n">key</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span> <a id="__codelineno-12-66" name="__codelineno-12-66" href="#__codelineno-12-66"></a> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">+=</span> <span class="mi">1</span>
<a id="__codelineno-12-67" name="__codelineno-12-67" href="#__codelineno-12-67"></a> <span class="bp">self</span><span class="o">.</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="bp">self</span><span class="o">.</span><span class="n">removed</span> <a id="__codelineno-12-67" name="__codelineno-12-67" href="#__codelineno-12-67"></a>
<a id="__codelineno-12-68" name="__codelineno-12-68" href="#__codelineno-12-68"></a> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">-=</span> <span class="mi">1</span> <a id="__codelineno-12-68" name="__codelineno-12-68" href="#__codelineno-12-68"></a> <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-12-69" name="__codelineno-12-69" href="#__codelineno-12-69"></a> <span class="k">return</span> <a id="__codelineno-12-69" name="__codelineno-12-69" href="#__codelineno-12-69"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;删除操作&quot;&quot;&quot;</span>
<a id="__codelineno-12-70" name="__codelineno-12-70" href="#__codelineno-12-70"></a> <a id="__codelineno-12-70" name="__codelineno-12-70" href="#__codelineno-12-70"></a> <span class="c1"># 搜索 key 对应的桶索引</span>
<a id="__codelineno-12-71" name="__codelineno-12-71" href="#__codelineno-12-71"></a> <span class="k">def</span> <span class="nf">extend</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <a id="__codelineno-12-71" name="__codelineno-12-71" href="#__codelineno-12-71"></a> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">find_bucket</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<a id="__codelineno-12-72" name="__codelineno-12-72" href="#__codelineno-12-72"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;扩容哈希表&quot;&quot;&quot;</span> <a id="__codelineno-12-72" name="__codelineno-12-72" href="#__codelineno-12-72"></a> <span class="c1"># 若找到键值对,则用删除标记覆盖它</span>
<a id="__codelineno-12-73" name="__codelineno-12-73" href="#__codelineno-12-73"></a> <span class="c1"># 暂存原哈希表</span> <a id="__codelineno-12-73" name="__codelineno-12-73" href="#__codelineno-12-73"></a> <span class="k">if</span> <span class="bp">self</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="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span><span class="p">]:</span>
<a id="__codelineno-12-74" name="__codelineno-12-74" href="#__codelineno-12-74"></a> <span class="n">buckets_tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span> <a id="__codelineno-12-74" name="__codelineno-12-74" href="#__codelineno-12-74"></a> <span class="bp">self</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="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span>
<a id="__codelineno-12-75" name="__codelineno-12-75" href="#__codelineno-12-75"></a> <span class="c1"># 初始化扩容后的新哈希表</span> <a id="__codelineno-12-75" name="__codelineno-12-75" href="#__codelineno-12-75"></a> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">-=</span> <span class="mi">1</span>
<a id="__codelineno-12-76" name="__codelineno-12-76" href="#__codelineno-12-76"></a> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <span class="o">*=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extend_ratio</span> <a id="__codelineno-12-76" name="__codelineno-12-76" href="#__codelineno-12-76"></a>
<a id="__codelineno-12-77" name="__codelineno-12-77" href="#__codelineno-12-77"></a> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <a id="__codelineno-12-77" name="__codelineno-12-77" href="#__codelineno-12-77"></a> <span class="k">def</span> <span class="nf">extend</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<a id="__codelineno-12-78" name="__codelineno-12-78" href="#__codelineno-12-78"></a> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="mi">0</span> <a id="__codelineno-12-78" name="__codelineno-12-78" href="#__codelineno-12-78"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;扩容哈希表&quot;&quot;&quot;</span>
<a id="__codelineno-12-79" name="__codelineno-12-79" href="#__codelineno-12-79"></a> <span class="c1"># 将键值对从原哈希表搬运至新哈希表</span> <a id="__codelineno-12-79" name="__codelineno-12-79" href="#__codelineno-12-79"></a> <span class="c1"># 暂存原哈希表</span>
<a id="__codelineno-12-80" name="__codelineno-12-80" href="#__codelineno-12-80"></a> <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="n">buckets_tmp</span><span class="p">:</span> <a id="__codelineno-12-80" name="__codelineno-12-80" href="#__codelineno-12-80"></a> <span class="n">buckets_tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span>
<a id="__codelineno-12-81" name="__codelineno-12-81" href="#__codelineno-12-81"></a> <span class="k">if</span> <span class="n">pair</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">removed</span><span class="p">]:</span> <a id="__codelineno-12-81" name="__codelineno-12-81" href="#__codelineno-12-81"></a> <span class="c1"># 初始化扩容后的新哈希表</span>
<a id="__codelineno-12-82" name="__codelineno-12-82" href="#__codelineno-12-82"></a> <span class="bp">self</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">key</span><span class="p">,</span> <span class="n">pair</span><span class="o">.</span><span class="n">val</span><span class="p">)</span> <a id="__codelineno-12-82" name="__codelineno-12-82" href="#__codelineno-12-82"></a> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span> <span class="o">*=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extend_ratio</span>
<a id="__codelineno-12-83" name="__codelineno-12-83" href="#__codelineno-12-83"></a> <a id="__codelineno-12-83" name="__codelineno-12-83" href="#__codelineno-12-83"></a> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span>
<a id="__codelineno-12-84" name="__codelineno-12-84" href="#__codelineno-12-84"></a> <span class="k">def</span> <span class="nf">print</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <a id="__codelineno-12-84" name="__codelineno-12-84" href="#__codelineno-12-84"></a> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="mi">0</span>
<a id="__codelineno-12-85" name="__codelineno-12-85" href="#__codelineno-12-85"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;打印哈希表&quot;&quot;&quot;</span> <a id="__codelineno-12-85" name="__codelineno-12-85" href="#__codelineno-12-85"></a> <span class="c1"># 将键值对从原哈希表搬运至新哈希表</span>
<a id="__codelineno-12-86" name="__codelineno-12-86" href="#__codelineno-12-86"></a> <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">:</span> <a id="__codelineno-12-86" name="__codelineno-12-86" href="#__codelineno-12-86"></a> <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="n">buckets_tmp</span><span class="p">:</span>
<a id="__codelineno-12-87" name="__codelineno-12-87" href="#__codelineno-12-87"></a> <span class="k">if</span> <span class="n">pair</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <a id="__codelineno-12-87" name="__codelineno-12-87" href="#__codelineno-12-87"></a> <span class="k">if</span> <span class="n">pair</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span><span class="p">]:</span>
<a id="__codelineno-12-88" name="__codelineno-12-88" href="#__codelineno-12-88"></a> <span class="nb">print</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">key</span><span class="p">,</span> <span class="s2">&quot;-&gt;&quot;</span><span class="p">,</span> <span class="n">pair</span><span class="o">.</span><span class="n">val</span><span class="p">)</span> <a id="__codelineno-12-88" name="__codelineno-12-88" href="#__codelineno-12-88"></a> <span class="bp">self</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">key</span><span class="p">,</span> <span class="n">pair</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>
<a id="__codelineno-12-89" name="__codelineno-12-89" href="#__codelineno-12-89"></a> <span class="k">else</span><span class="p">:</span> <a id="__codelineno-12-89" name="__codelineno-12-89" href="#__codelineno-12-89"></a>
<a id="__codelineno-12-90" name="__codelineno-12-90" href="#__codelineno-12-90"></a> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;None&quot;</span><span class="p">)</span> <a id="__codelineno-12-90" name="__codelineno-12-90" href="#__codelineno-12-90"></a> <span class="k">def</span> <span class="nf">print</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<a id="__codelineno-12-91" name="__codelineno-12-91" href="#__codelineno-12-91"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;打印哈希表&quot;&quot;&quot;</span>
<a id="__codelineno-12-92" name="__codelineno-12-92" href="#__codelineno-12-92"></a> <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">:</span>
<a id="__codelineno-12-93" name="__codelineno-12-93" href="#__codelineno-12-93"></a> <span class="k">if</span> <span class="n">pair</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<a id="__codelineno-12-94" name="__codelineno-12-94" href="#__codelineno-12-94"></a> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;None&quot;</span><span class="p">)</span>
<a id="__codelineno-12-95" name="__codelineno-12-95" href="#__codelineno-12-95"></a> <span class="k">elif</span> <span class="n">pair</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOMBSTONE</span><span class="p">:</span>
<a id="__codelineno-12-96" name="__codelineno-12-96" href="#__codelineno-12-96"></a> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;TOMBSTONE&quot;</span><span class="p">)</span>
<a id="__codelineno-12-97" name="__codelineno-12-97" href="#__codelineno-12-97"></a> <span class="k">else</span><span class="p">:</span>
<a id="__codelineno-12-98" name="__codelineno-12-98" href="#__codelineno-12-98"></a> <span class="nb">print</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">key</span><span class="p">,</span> <span class="s2">&quot;-&gt;&quot;</span><span class="p">,</span> <span class="n">pair</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">hash_map_open_addressing.cpp</span><pre><span></span><code><a id="__codelineno-13-1" name="__codelineno-13-1" href="#__codelineno-13-1"></a><span class="cm">/* 开放寻址哈希表 */</span> <div class="highlight"><span class="filename">hash_map_open_addressing.cpp</span><pre><span></span><code><a id="__codelineno-13-1" name="__codelineno-13-1" href="#__codelineno-13-1"></a><span class="cm">/**</span>
<a id="__codelineno-13-2" name="__codelineno-13-2" href="#__codelineno-13-2"></a><span class="k">class</span><span class="w"> </span><span class="nc">HashMapOpenAddressing</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-13-2" name="__codelineno-13-2" href="#__codelineno-13-2"></a><span class="cm"> * File: hash_map_open_addressing.cpp</span>
<a id="__codelineno-13-3" name="__codelineno-13-3" href="#__codelineno-13-3"></a><span class="w"> </span><span class="k">private</span><span class="o">:</span> <a id="__codelineno-13-3" name="__codelineno-13-3" href="#__codelineno-13-3"></a><span class="cm"> * Created Time: 2023-06-13</span>
<a id="__codelineno-13-4" name="__codelineno-13-4" href="#__codelineno-13-4"></a><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-13-4" name="__codelineno-13-4" href="#__codelineno-13-4"></a><span class="cm"> * Author: Krahets (krahets@163.com)</span>
<a id="__codelineno-13-5" name="__codelineno-13-5" href="#__codelineno-13-5"></a><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-13-5" name="__codelineno-13-5" href="#__codelineno-13-5"></a><span class="cm"> */</span>
<a id="__codelineno-13-6" name="__codelineno-13-6" href="#__codelineno-13-6"></a><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-13-6" name="__codelineno-13-6" href="#__codelineno-13-6"></a>
<a id="__codelineno-13-7" name="__codelineno-13-7" href="#__codelineno-13-7"></a><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-13-7" name="__codelineno-13-7" href="#__codelineno-13-7"></a><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;./array_hash_map.cpp&quot;</span>
<a id="__codelineno-13-8" name="__codelineno-13-8" href="#__codelineno-13-8"></a><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Pair</span><span class="w"> </span><span class="o">*&gt;</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-13-8" name="__codelineno-13-8" href="#__codelineno-13-8"></a>
<a id="__codelineno-13-9" name="__codelineno-13-9" href="#__codelineno-13-9"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">removed</span><span class="p">;</span><span class="w"> </span><span class="c1">// 删除标记</span> <a id="__codelineno-13-9" name="__codelineno-13-9" href="#__codelineno-13-9"></a><span class="k">class</span><span class="w"> </span><span class="nc">HashMapOpenAddressing</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-10" name="__codelineno-13-10" href="#__codelineno-13-10"></a> <a id="__codelineno-13-10" name="__codelineno-13-10" href="#__codelineno-13-10"></a><span class="w"> </span><span class="k">private</span><span class="o">:</span>
<a id="__codelineno-13-11" name="__codelineno-13-11" href="#__codelineno-13-11"></a><span class="w"> </span><span class="k">public</span><span class="o">:</span> <a id="__codelineno-13-11" name="__codelineno-13-11" href="#__codelineno-13-11"></a><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-13-12" name="__codelineno-13-12" href="#__codelineno-13-12"></a><span class="w"> </span><span class="cm">/* 构造方法 */</span> <a id="__codelineno-13-12" name="__codelineno-13-12" href="#__codelineno-13-12"></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="mi">4</span><span class="p">;</span><span class="w"> </span><span class="c1">// 哈希表容量</span>
<a id="__codelineno-13-13" name="__codelineno-13-13" href="#__codelineno-13-13"></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-13-13" name="__codelineno-13-13" href="#__codelineno-13-13"></a><span class="w"> </span><span class="k">const</span><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="mf">2.0</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="c1">// 触发扩容的负载因子阈值</span>
<a id="__codelineno-13-14" name="__codelineno-13-14" href="#__codelineno-13-14"></a><span class="w"> </span><span class="c1">// 构造方法</span> <a id="__codelineno-13-14" name="__codelineno-13-14" href="#__codelineno-13-14"></a><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">extendRatio</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="c1">// 扩容倍数</span>
<a id="__codelineno-13-15" name="__codelineno-13-15" href="#__codelineno-13-15"></a><span class="w"> </span><span class="n">size</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-13-15" name="__codelineno-13-15" href="#__codelineno-13-15"></a><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Pair</span><span class="w"> </span><span class="o">*&gt;</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-13-16" name="__codelineno-13-16" href="#__codelineno-13-16"></a><span class="w"> </span><span class="n">capacity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span> <a id="__codelineno-13-16" name="__codelineno-13-16" href="#__codelineno-13-16"></a><span class="w"> </span><span class="n">Pair</span><span class="w"> </span><span class="o">*</span><span class="n">TOMBSTONE</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="mi">-1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-1&quot;</span><span class="p">);</span><span class="w"> </span><span class="c1">// 删除标记</span>
<a id="__codelineno-13-17" name="__codelineno-13-17" href="#__codelineno-13-17"></a><span class="w"> </span><span class="n">loadThres</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2.0</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">3.0</span><span class="p">;</span> <a id="__codelineno-13-17" name="__codelineno-13-17" href="#__codelineno-13-17"></a>
<a id="__codelineno-13-18" name="__codelineno-13-18" href="#__codelineno-13-18"></a><span class="w"> </span><span class="n">extendRatio</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-13-18" name="__codelineno-13-18" href="#__codelineno-13-18"></a><span class="w"> </span><span class="k">public</span><span class="o">:</span>
<a id="__codelineno-13-19" name="__codelineno-13-19" href="#__codelineno-13-19"></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">vector</span><span class="o">&lt;</span><span class="n">Pair</span><span class="w"> </span><span class="o">*&gt;</span><span class="p">(</span><span class="n">capacity</span><span class="p">,</span><span class="w"> </span><span class="k">nullptr</span><span class="p">);</span> <a id="__codelineno-13-19" name="__codelineno-13-19" href="#__codelineno-13-19"></a><span class="w"> </span><span class="cm">/* 构造方法 */</span>
<a id="__codelineno-13-20" name="__codelineno-13-20" href="#__codelineno-13-20"></a><span class="w"> </span><span class="n">removed</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="mi">-1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-1&quot;</span><span class="p">);</span> <a id="__codelineno-13-20" name="__codelineno-13-20" href="#__codelineno-13-20"></a><span class="w"> </span><span class="n">HashMapOpenAddressing</span><span class="p">()</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span><span class="w"> </span><span class="n">buckets</span><span class="p">(</span><span class="n">capacity</span><span class="p">,</span><span class="w"> </span><span class="k">nullptr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-21" name="__codelineno-13-21" href="#__codelineno-13-21"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-21" name="__codelineno-13-21" href="#__codelineno-13-21"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-22" name="__codelineno-13-22" href="#__codelineno-13-22"></a> <a id="__codelineno-13-22" name="__codelineno-13-22" href="#__codelineno-13-22"></a>
<a id="__codelineno-13-23" name="__codelineno-13-23" href="#__codelineno-13-23"></a><span class="w"> </span><span class="cm">/* 哈希函数 */</span> <a id="__codelineno-13-23" name="__codelineno-13-23" href="#__codelineno-13-23"></a><span class="w"> </span><span class="cm">/* 析构方法 */</span>
<a id="__codelineno-13-24" name="__codelineno-13-24" href="#__codelineno-13-24"></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-13-24" name="__codelineno-13-24" href="#__codelineno-13-24"></a><span class="w"> </span><span class="o">~</span><span class="n">HashMapOpenAddressing</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-25" name="__codelineno-13-25" href="#__codelineno-13-25"></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-13-25" name="__codelineno-13-25" href="#__codelineno-13-25"></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="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">buckets</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-26" name="__codelineno-13-26" href="#__codelineno-13-26"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-26" name="__codelineno-13-26" href="#__codelineno-13-26"></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="k">nullptr</span><span class="w"> </span><span class="o">&amp;&amp;</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-13-27" name="__codelineno-13-27" href="#__codelineno-13-27"></a> <a id="__codelineno-13-27" name="__codelineno-13-27" href="#__codelineno-13-27"></a><span class="w"> </span><span class="k">delete</span><span class="w"> </span><span class="n">pair</span><span class="p">;</span>
<a id="__codelineno-13-28" name="__codelineno-13-28" href="#__codelineno-13-28"></a><span class="w"> </span><span class="cm">/* 负载因子 */</span> <a id="__codelineno-13-28" name="__codelineno-13-28" href="#__codelineno-13-28"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-29" name="__codelineno-13-29" href="#__codelineno-13-29"></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-13-29" name="__codelineno-13-29" href="#__codelineno-13-29"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-30" name="__codelineno-13-30" href="#__codelineno-13-30"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">static_cast</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;</span><span class="p">(</span><span class="n">size</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-13-30" name="__codelineno-13-30" href="#__codelineno-13-30"></a><span class="w"> </span><span class="k">delete</span><span class="w"> </span><span class="n">TOMBSTONE</span><span class="p">;</span>
<a id="__codelineno-13-31" name="__codelineno-13-31" href="#__codelineno-13-31"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-31" name="__codelineno-13-31" href="#__codelineno-13-31"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-32" name="__codelineno-13-32" href="#__codelineno-13-32"></a> <a id="__codelineno-13-32" name="__codelineno-13-32" href="#__codelineno-13-32"></a>
<a id="__codelineno-13-33" name="__codelineno-13-33" href="#__codelineno-13-33"></a><span class="w"> </span><span class="cm">/* 查询操作 */</span> <a id="__codelineno-13-33" name="__codelineno-13-33" href="#__codelineno-13-33"></a><span class="w"> </span><span class="cm">/* 哈希函数 */</span>
<a id="__codelineno-13-34" name="__codelineno-13-34" href="#__codelineno-13-34"></a><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">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-13-34" name="__codelineno-13-34" href="#__codelineno-13-34"></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-13-35" name="__codelineno-13-35" href="#__codelineno-13-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-13-35" name="__codelineno-13-35" href="#__codelineno-13-35"></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-13-36" name="__codelineno-13-36" href="#__codelineno-13-36"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-13-36" name="__codelineno-13-36" href="#__codelineno-13-36"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-37" name="__codelineno-13-37" href="#__codelineno-13-37"></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">&lt;</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-13-37" name="__codelineno-13-37" href="#__codelineno-13-37"></a>
<a id="__codelineno-13-38" name="__codelineno-13-38" href="#__codelineno-13-38"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-13-38" name="__codelineno-13-38" href="#__codelineno-13-38"></a><span class="w"> </span><span class="cm">/* 负载因子 */</span>
<a id="__codelineno-13-39" name="__codelineno-13-39" href="#__codelineno-13-39"></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-13-39" name="__codelineno-13-39" href="#__codelineno-13-39"></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-13-40" name="__codelineno-13-40" href="#__codelineno-13-40"></a><span class="w"> </span><span class="c1">// 若遇到空桶,说明无此 key ,则返回 nullptr</span> <a id="__codelineno-13-40" name="__codelineno-13-40" href="#__codelineno-13-40"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</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-13-41" name="__codelineno-13-41" href="#__codelineno-13-41"></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="k">nullptr</span><span class="p">)</span> <a id="__codelineno-13-41" name="__codelineno-13-41" href="#__codelineno-13-41"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-42" name="__codelineno-13-42" href="#__codelineno-13-42"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">nullptr</span><span class="p">;</span> <a id="__codelineno-13-42" name="__codelineno-13-42" href="#__codelineno-13-42"></a>
<a id="__codelineno-13-43" name="__codelineno-13-43" href="#__codelineno-13-43"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则返回对应 val</span> <a id="__codelineno-13-43" name="__codelineno-13-43" href="#__codelineno-13-43"></a><span class="w"> </span><span class="cm">/* 搜索 key 对应的桶索引 */</span>
<a id="__codelineno-13-44" name="__codelineno-13-44" href="#__codelineno-13-44"></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">-&gt;</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">&amp;&amp;</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-13-44" name="__codelineno-13-44" href="#__codelineno-13-44"></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-13-45" name="__codelineno-13-45" href="#__codelineno-13-45"></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">-&gt;</span><span class="n">val</span><span class="p">;</span> <a id="__codelineno-13-45" name="__codelineno-13-45" href="#__codelineno-13-45"></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-13-46" name="__codelineno-13-46" href="#__codelineno-13-46"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-46" name="__codelineno-13-46" href="#__codelineno-13-46"></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="mi">-1</span><span class="p">;</span>
<a id="__codelineno-13-47" name="__codelineno-13-47" href="#__codelineno-13-47"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">nullptr</span><span class="p">;</span> <a id="__codelineno-13-47" name="__codelineno-13-47" href="#__codelineno-13-47"></a><span class="w"> </span><span class="c1">// 线性探测,当遇到空桶时跳出</span>
<a id="__codelineno-13-48" name="__codelineno-13-48" href="#__codelineno-13-48"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-48" name="__codelineno-13-48" href="#__codelineno-13-48"></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="k">nullptr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-49" name="__codelineno-13-49" href="#__codelineno-13-49"></a> <a id="__codelineno-13-49" name="__codelineno-13-49" href="#__codelineno-13-49"></a><span class="w"> </span><span class="c1">// 若遇到 key ,返回对应桶索引</span>
<a id="__codelineno-13-50" name="__codelineno-13-50" href="#__codelineno-13-50"></a><span class="w"> </span><span class="cm">/* 添加操作 */</span> <a id="__codelineno-13-50" name="__codelineno-13-50" href="#__codelineno-13-50"></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">-&gt;</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-13-51" name="__codelineno-13-51" href="#__codelineno-13-51"></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="n">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-13-51" name="__codelineno-13-51" href="#__codelineno-13-51"></a><span class="w"> </span><span class="c1">// 若之前遇到了删除标记,则将键值对移动至该索引</span>
<a id="__codelineno-13-52" name="__codelineno-13-52" href="#__codelineno-13-52"></a><span class="w"> </span><span class="c1">// 当负载因子超过阈值时,执行扩容</span> <a id="__codelineno-13-52" name="__codelineno-13-52" href="#__codelineno-13-52"></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="mi">-1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-53" name="__codelineno-13-53" href="#__codelineno-13-53"></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">&gt;</span><span class="w"> </span><span class="n">loadThres</span><span class="p">)</span> <a id="__codelineno-13-53" name="__codelineno-13-53" href="#__codelineno-13-53"></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-13-54" name="__codelineno-13-54" href="#__codelineno-13-54"></a><span class="w"> </span><span class="n">extend</span><span class="p">();</span> <a id="__codelineno-13-54" name="__codelineno-13-54" href="#__codelineno-13-54"></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-13-55" name="__codelineno-13-55" href="#__codelineno-13-55"></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-13-55" name="__codelineno-13-55" href="#__codelineno-13-55"></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-13-56" name="__codelineno-13-56" href="#__codelineno-13-56"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-13-56" name="__codelineno-13-56" href="#__codelineno-13-56"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-57" name="__codelineno-13-57" href="#__codelineno-13-57"></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">&lt;</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-13-57" name="__codelineno-13-57" href="#__codelineno-13-57"></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-13-58" name="__codelineno-13-58" href="#__codelineno-13-58"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-13-58" name="__codelineno-13-58" href="#__codelineno-13-58"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-59" name="__codelineno-13-59" href="#__codelineno-13-59"></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-13-59" name="__codelineno-13-59" href="#__codelineno-13-59"></a><span class="w"> </span><span class="c1">// 记录遇到的首个删除标记</span>
<a id="__codelineno-13-60" name="__codelineno-13-60" href="#__codelineno-13-60"></a><span class="w"> </span><span class="c1">// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶</span> <a id="__codelineno-13-60" name="__codelineno-13-60" href="#__codelineno-13-60"></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="mi">-1</span><span class="w"> </span><span class="o">&amp;&amp;</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-13-61" name="__codelineno-13-61" href="#__codelineno-13-61"></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="k">nullptr</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-13-61" name="__codelineno-13-61" href="#__codelineno-13-61"></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-13-62" name="__codelineno-13-62" href="#__codelineno-13-62"></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-13-62" name="__codelineno-13-62" href="#__codelineno-13-62"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-63" name="__codelineno-13-63" href="#__codelineno-13-63"></a><span class="w"> </span><span class="n">size</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-13-63" name="__codelineno-13-63" href="#__codelineno-13-63"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span>
<a id="__codelineno-13-64" name="__codelineno-13-64" href="#__codelineno-13-64"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-13-64" name="__codelineno-13-64" href="#__codelineno-13-64"></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="mi">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-13-65" name="__codelineno-13-65" href="#__codelineno-13-65"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-65" name="__codelineno-13-65" href="#__codelineno-13-65"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-66" name="__codelineno-13-66" href="#__codelineno-13-66"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则更新对应 val</span> <a id="__codelineno-13-66" name="__codelineno-13-66" href="#__codelineno-13-66"></a><span class="w"> </span><span class="c1">// 若 key 不存在,则返回添加点的索引</span>
<a id="__codelineno-13-67" name="__codelineno-13-67" href="#__codelineno-13-67"></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">-&gt;</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-13-67" name="__codelineno-13-67" href="#__codelineno-13-67"></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="mi">-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-13-68" name="__codelineno-13-68" href="#__codelineno-13-68"></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">-&gt;</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-13-68" name="__codelineno-13-68" href="#__codelineno-13-68"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-69" name="__codelineno-13-69" href="#__codelineno-13-69"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-13-69" name="__codelineno-13-69" href="#__codelineno-13-69"></a>
<a id="__codelineno-13-70" name="__codelineno-13-70" href="#__codelineno-13-70"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-70" name="__codelineno-13-70" href="#__codelineno-13-70"></a><span class="w"> </span><span class="cm">/* 查询操作 */</span>
<a id="__codelineno-13-71" name="__codelineno-13-71" href="#__codelineno-13-71"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-71" name="__codelineno-13-71" href="#__codelineno-13-71"></a><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">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-13-72" name="__codelineno-13-72" href="#__codelineno-13-72"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-72" name="__codelineno-13-72" href="#__codelineno-13-72"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
<a id="__codelineno-13-73" name="__codelineno-13-73" href="#__codelineno-13-73"></a> <a id="__codelineno-13-73" name="__codelineno-13-73" href="#__codelineno-13-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">findBucket</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
<a id="__codelineno-13-74" name="__codelineno-13-74" href="#__codelineno-13-74"></a><span class="w"> </span><span class="cm">/* 删除操作 */</span> <a id="__codelineno-13-74" name="__codelineno-13-74" href="#__codelineno-13-74"></a><span class="w"> </span><span class="c1">// 若找到键值对,则返回对应 val</span>
<a id="__codelineno-13-75" name="__codelineno-13-75" href="#__codelineno-13-75"></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-13-75" name="__codelineno-13-75" href="#__codelineno-13-75"></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="k">nullptr</span><span class="w"> </span><span class="o">&amp;&amp;</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-13-76" name="__codelineno-13-76" href="#__codelineno-13-76"></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-13-76" name="__codelineno-13-76" href="#__codelineno-13-76"></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">-&gt;</span><span class="n">val</span><span class="p">;</span>
<a id="__codelineno-13-77" name="__codelineno-13-77" href="#__codelineno-13-77"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-13-77" name="__codelineno-13-77" href="#__codelineno-13-77"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-78" name="__codelineno-13-78" href="#__codelineno-13-78"></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">&lt;</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-13-78" name="__codelineno-13-78" href="#__codelineno-13-78"></a><span class="w"> </span><span class="c1">// 若键值对不存在,则返回空字符串</span>
<a id="__codelineno-13-79" name="__codelineno-13-79" href="#__codelineno-13-79"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-13-79" name="__codelineno-13-79" href="#__codelineno-13-79"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="p">;</span>
<a id="__codelineno-13-80" name="__codelineno-13-80" href="#__codelineno-13-80"></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-13-80" name="__codelineno-13-80" href="#__codelineno-13-80"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-81" name="__codelineno-13-81" href="#__codelineno-13-81"></a><span class="w"> </span><span class="c1">// 若遇到空桶,说明无此 key ,则直接返回</span> <a id="__codelineno-13-81" name="__codelineno-13-81" href="#__codelineno-13-81"></a>
<a id="__codelineno-13-82" name="__codelineno-13-82" href="#__codelineno-13-82"></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="k">nullptr</span><span class="p">)</span> <a id="__codelineno-13-82" name="__codelineno-13-82" href="#__codelineno-13-82"></a><span class="w"> </span><span class="cm">/* 添加操作 */</span>
<a id="__codelineno-13-83" name="__codelineno-13-83" href="#__codelineno-13-83"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-13-83" name="__codelineno-13-83" href="#__codelineno-13-83"></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="n">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-13-84" name="__codelineno-13-84" href="#__codelineno-13-84"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则标记删除并返回</span> <a id="__codelineno-13-84" name="__codelineno-13-84" href="#__codelineno-13-84"></a><span class="w"> </span><span class="c1">// 当负载因子超过阈值时,执行扩容</span>
<a id="__codelineno-13-85" name="__codelineno-13-85" href="#__codelineno-13-85"></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">-&gt;</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-13-85" name="__codelineno-13-85" href="#__codelineno-13-85"></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">&gt;</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-13-86" name="__codelineno-13-86" href="#__codelineno-13-86"></a><span class="w"> </span><span class="k">delete</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="c1">// 释放内存</span> <a id="__codelineno-13-86" name="__codelineno-13-86" href="#__codelineno-13-86"></a><span class="w"> </span><span class="n">extend</span><span class="p">();</span>
<a id="__codelineno-13-87" name="__codelineno-13-87" href="#__codelineno-13-87"></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-13-87" name="__codelineno-13-87" href="#__codelineno-13-87"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-88" name="__codelineno-13-88" href="#__codelineno-13-88"></a><span class="w"> </span><span class="n">size</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-13-88" name="__codelineno-13-88" href="#__codelineno-13-88"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
<a id="__codelineno-13-89" name="__codelineno-13-89" href="#__codelineno-13-89"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-13-89" name="__codelineno-13-89" href="#__codelineno-13-89"></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-13-90" name="__codelineno-13-90" href="#__codelineno-13-90"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-90" name="__codelineno-13-90" href="#__codelineno-13-90"></a><span class="w"> </span><span class="c1">// 若找到键值对,则覆盖 val 并返回</span>
<a id="__codelineno-13-91" name="__codelineno-13-91" href="#__codelineno-13-91"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-91" name="__codelineno-13-91" href="#__codelineno-13-91"></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="k">nullptr</span><span class="w"> </span><span class="o">&amp;&amp;</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-13-92" name="__codelineno-13-92" href="#__codelineno-13-92"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-92" name="__codelineno-13-92" href="#__codelineno-13-92"></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">-&gt;</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-13-93" name="__codelineno-13-93" href="#__codelineno-13-93"></a> <a id="__codelineno-13-93" name="__codelineno-13-93" href="#__codelineno-13-93"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
<a id="__codelineno-13-94" name="__codelineno-13-94" href="#__codelineno-13-94"></a><span class="w"> </span><span class="cm">/* 扩容哈希表 */</span> <a id="__codelineno-13-94" name="__codelineno-13-94" href="#__codelineno-13-94"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-95" name="__codelineno-13-95" href="#__codelineno-13-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-13-95" name="__codelineno-13-95" href="#__codelineno-13-95"></a><span class="w"> </span><span class="c1">// 若键值对不存在,则添加该键值对</span>
<a id="__codelineno-13-96" name="__codelineno-13-96" href="#__codelineno-13-96"></a><span class="w"> </span><span class="c1">// 暂存原哈希表</span> <a id="__codelineno-13-96" name="__codelineno-13-96" href="#__codelineno-13-96"></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-13-97" name="__codelineno-13-97" href="#__codelineno-13-97"></a><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Pair</span><span class="w"> </span><span class="o">*&gt;</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-13-97" name="__codelineno-13-97" href="#__codelineno-13-97"></a><span class="w"> </span><span class="n">size</span><span class="o">++</span><span class="p">;</span>
<a id="__codelineno-13-98" name="__codelineno-13-98" href="#__codelineno-13-98"></a><span class="w"> </span><span class="c1">// 初始化扩容后的新哈希表</span> <a id="__codelineno-13-98" name="__codelineno-13-98" href="#__codelineno-13-98"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-99" name="__codelineno-13-99" href="#__codelineno-13-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-13-99" name="__codelineno-13-99" href="#__codelineno-13-99"></a>
<a id="__codelineno-13-100" name="__codelineno-13-100" href="#__codelineno-13-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">vector</span><span class="o">&lt;</span><span class="n">Pair</span><span class="w"> </span><span class="o">*&gt;</span><span class="p">(</span><span class="n">capacity</span><span class="p">,</span><span class="w"> </span><span class="k">nullptr</span><span class="p">);</span> <a id="__codelineno-13-100" name="__codelineno-13-100" href="#__codelineno-13-100"></a><span class="w"> </span><span class="cm">/* 删除操作 */</span>
<a id="__codelineno-13-101" name="__codelineno-13-101" href="#__codelineno-13-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="mi">0</span><span class="p">;</span> <a id="__codelineno-13-101" name="__codelineno-13-101" href="#__codelineno-13-101"></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-13-102" name="__codelineno-13-102" href="#__codelineno-13-102"></a><span class="w"> </span><span class="c1">// 将键值对从原哈希表搬运至新哈希表</span> <a id="__codelineno-13-102" name="__codelineno-13-102" href="#__codelineno-13-102"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
<a id="__codelineno-13-103" name="__codelineno-13-103" href="#__codelineno-13-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="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">bucketsTmp</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-13-103" name="__codelineno-13-103" href="#__codelineno-13-103"></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-13-104" name="__codelineno-13-104" href="#__codelineno-13-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="k">nullptr</span><span class="w"> </span><span class="o">&amp;&amp;</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-13-104" name="__codelineno-13-104" href="#__codelineno-13-104"></a><span class="w"> </span><span class="c1">// 若找到键值对,则用删除标记覆盖它</span>
<a id="__codelineno-13-105" name="__codelineno-13-105" href="#__codelineno-13-105"></a><span class="w"> </span><span class="n">put</span><span class="p">(</span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">val</span><span class="p">);</span> <a id="__codelineno-13-105" name="__codelineno-13-105" href="#__codelineno-13-105"></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="k">nullptr</span><span class="w"> </span><span class="o">&amp;&amp;</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-13-106" name="__codelineno-13-106" href="#__codelineno-13-106"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-106" name="__codelineno-13-106" href="#__codelineno-13-106"></a><span class="w"> </span><span class="k">delete</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-13-107" name="__codelineno-13-107" href="#__codelineno-13-107"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-107" name="__codelineno-13-107" href="#__codelineno-13-107"></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-13-108" name="__codelineno-13-108" href="#__codelineno-13-108"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-108" name="__codelineno-13-108" href="#__codelineno-13-108"></a><span class="w"> </span><span class="n">size</span><span class="o">--</span><span class="p">;</span>
<a id="__codelineno-13-109" name="__codelineno-13-109" href="#__codelineno-13-109"></a> <a id="__codelineno-13-109" name="__codelineno-13-109" href="#__codelineno-13-109"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-110" name="__codelineno-13-110" href="#__codelineno-13-110"></a><span class="w"> </span><span class="cm">/* 打印哈希表 */</span> <a id="__codelineno-13-110" name="__codelineno-13-110" href="#__codelineno-13-110"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-111" name="__codelineno-13-111" href="#__codelineno-13-111"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-13-111" name="__codelineno-13-111" href="#__codelineno-13-111"></a>
<a id="__codelineno-13-112" name="__codelineno-13-112" href="#__codelineno-13-112"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="o">&amp;</span><span class="n">pair</span><span class="w"> </span><span class="o">:</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-13-112" name="__codelineno-13-112" href="#__codelineno-13-112"></a><span class="w"> </span><span class="cm">/* 扩容哈希表 */</span>
<a id="__codelineno-13-113" name="__codelineno-13-113" href="#__codelineno-13-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="k">nullptr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-13-113" name="__codelineno-13-113" href="#__codelineno-13-113"></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-13-114" name="__codelineno-13-114" href="#__codelineno-13-114"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">key</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot; -&gt; &quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">val</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span> <a id="__codelineno-13-114" name="__codelineno-13-114" href="#__codelineno-13-114"></a><span class="w"> </span><span class="c1">// 暂存原哈希表</span>
<a id="__codelineno-13-115" name="__codelineno-13-115" href="#__codelineno-13-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="p">{</span> <a id="__codelineno-13-115" name="__codelineno-13-115" href="#__codelineno-13-115"></a><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Pair</span><span class="w"> </span><span class="o">*&gt;</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-13-116" name="__codelineno-13-116" href="#__codelineno-13-116"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;nullptr&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span> <a id="__codelineno-13-116" name="__codelineno-13-116" href="#__codelineno-13-116"></a><span class="w"> </span><span class="c1">// 初始化扩容后的新哈希表</span>
<a id="__codelineno-13-117" name="__codelineno-13-117" href="#__codelineno-13-117"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-117" name="__codelineno-13-117" href="#__codelineno-13-117"></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-13-118" name="__codelineno-13-118" href="#__codelineno-13-118"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-118" name="__codelineno-13-118" href="#__codelineno-13-118"></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">vector</span><span class="o">&lt;</span><span class="n">Pair</span><span class="w"> </span><span class="o">*&gt;</span><span class="p">(</span><span class="n">capacity</span><span class="p">,</span><span class="w"> </span><span class="k">nullptr</span><span class="p">);</span>
<a id="__codelineno-13-119" name="__codelineno-13-119" href="#__codelineno-13-119"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-13-119" name="__codelineno-13-119" href="#__codelineno-13-119"></a><span class="w"> </span><span class="n">size</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-13-120" name="__codelineno-13-120" href="#__codelineno-13-120"></a><span class="p">};</span> <a id="__codelineno-13-120" name="__codelineno-13-120" href="#__codelineno-13-120"></a><span class="w"> </span><span class="c1">// 将键值对从原哈希表搬运至新哈希表</span>
<a id="__codelineno-13-121" name="__codelineno-13-121" href="#__codelineno-13-121"></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="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">bucketsTmp</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-122" name="__codelineno-13-122" href="#__codelineno-13-122"></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="k">nullptr</span><span class="w"> </span><span class="o">&amp;&amp;</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-13-123" name="__codelineno-13-123" href="#__codelineno-13-123"></a><span class="w"> </span><span class="n">put</span><span class="p">(</span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">val</span><span class="p">);</span>
<a id="__codelineno-13-124" name="__codelineno-13-124" href="#__codelineno-13-124"></a><span class="w"> </span><span class="k">delete</span><span class="w"> </span><span class="n">pair</span><span class="p">;</span>
<a id="__codelineno-13-125" name="__codelineno-13-125" href="#__codelineno-13-125"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-126" name="__codelineno-13-126" href="#__codelineno-13-126"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-127" name="__codelineno-13-127" href="#__codelineno-13-127"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-128" name="__codelineno-13-128" href="#__codelineno-13-128"></a>
<a id="__codelineno-13-129" name="__codelineno-13-129" href="#__codelineno-13-129"></a><span class="w"> </span><span class="cm">/* 打印哈希表 */</span>
<a id="__codelineno-13-130" name="__codelineno-13-130" href="#__codelineno-13-130"></a><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-131" name="__codelineno-13-131" href="#__codelineno-13-131"></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="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">buckets</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-132" name="__codelineno-13-132" href="#__codelineno-13-132"></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="k">nullptr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-133" name="__codelineno-13-133" href="#__codelineno-13-133"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;nullptr&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
<a id="__codelineno-13-134" name="__codelineno-13-134" href="#__codelineno-13-134"></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-13-135" name="__codelineno-13-135" href="#__codelineno-13-135"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;TOMBSTONE&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
<a id="__codelineno-13-136" name="__codelineno-13-136" href="#__codelineno-13-136"></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-13-137" name="__codelineno-13-137" href="#__codelineno-13-137"></a><span class="w"> </span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">key</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot; -&gt; &quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">pair</span><span class="o">-&gt;</span><span class="n">val</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">endl</span><span class="p">;</span>
<a id="__codelineno-13-138" name="__codelineno-13-138" href="#__codelineno-13-138"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-139" name="__codelineno-13-139" href="#__codelineno-13-139"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-140" name="__codelineno-13-140" href="#__codelineno-13-140"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-13-141" name="__codelineno-13-141" href="#__codelineno-13-141"></a><span class="p">};</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">hash_map_open_addressing.java</span><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1" href="#__codelineno-14-1"></a><span class="cm">/* 开放寻址哈希表 */</span> <div class="highlight"><span class="filename">hash_map_open_addressing.java</span><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1" href="#__codelineno-14-1"></a><span class="cm">/* 开放寻址哈希表 */</span>
<a id="__codelineno-14-2" name="__codelineno-14-2" href="#__codelineno-14-2"></a><span class="kd">class</span> <span class="nc">HashMapOpenAddressing</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-2" name="__codelineno-14-2" href="#__codelineno-14-2"></a><span class="kd">class</span> <span class="nc">HashMapOpenAddressing</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-3" name="__codelineno-14-3" href="#__codelineno-14-3"></a><span class="w"> </span><span class="kd">private</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-14-3" name="__codelineno-14-3" href="#__codelineno-14-3"></a><span class="w"> </span><span class="kd">private</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-14-4" name="__codelineno-14-4" href="#__codelineno-14-4"></a><span class="w"> </span><span class="kd">private</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-14-4" name="__codelineno-14-4" href="#__codelineno-14-4"></a><span class="w"> </span><span class="kd">private</span><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="mi">4</span><span class="p">;</span><span class="w"> </span><span class="c1">// 哈希表容量</span>
<a id="__codelineno-14-5" name="__codelineno-14-5" href="#__codelineno-14-5"></a><span class="w"> </span><span class="kd">private</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-14-5" name="__codelineno-14-5" href="#__codelineno-14-5"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">final</span><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="mf">2.0</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="c1">// 触发扩容的负载因子阈值</span>
<a id="__codelineno-14-6" name="__codelineno-14-6" href="#__codelineno-14-6"></a><span class="w"> </span><span class="kd">private</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-14-6" name="__codelineno-14-6" href="#__codelineno-14-6"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">final</span><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="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 扩容倍数</span>
<a id="__codelineno-14-7" name="__codelineno-14-7" href="#__codelineno-14-7"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </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-14-7" name="__codelineno-14-7" href="#__codelineno-14-7"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </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-14-8" name="__codelineno-14-8" href="#__codelineno-14-8"></a><span class="w"> </span><span class="kd">private</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-14-8" name="__codelineno-14-8" href="#__codelineno-14-8"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">final</span><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="k">new</span><span class="w"> </span><span class="n">Pair</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-1&quot;</span><span class="p">);</span><span class="w"> </span><span class="c1">// 删除标记</span>
<a id="__codelineno-14-9" name="__codelineno-14-9" href="#__codelineno-14-9"></a> <a id="__codelineno-14-9" name="__codelineno-14-9" href="#__codelineno-14-9"></a>
<a id="__codelineno-14-10" name="__codelineno-14-10" href="#__codelineno-14-10"></a><span class="w"> </span><span class="cm">/* 构造方法 */</span> <a id="__codelineno-14-10" name="__codelineno-14-10" href="#__codelineno-14-10"></a><span class="w"> </span><span class="cm">/* 构造方法 */</span>
<a id="__codelineno-14-11" name="__codelineno-14-11" href="#__codelineno-14-11"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">HashMapOpenAddressing</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-11" name="__codelineno-14-11" href="#__codelineno-14-11"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">HashMapOpenAddressing</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-12" name="__codelineno-14-12" href="#__codelineno-14-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="mi">0</span><span class="p">;</span> <a id="__codelineno-14-12" name="__codelineno-14-12" href="#__codelineno-14-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="mi">0</span><span class="p">;</span>
<a id="__codelineno-14-13" name="__codelineno-14-13" href="#__codelineno-14-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="mi">4</span><span class="p">;</span> <a id="__codelineno-14-13" name="__codelineno-14-13" href="#__codelineno-14-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="k">new</span><span class="w"> </span><span class="n">Pair</span><span class="o">[</span><span class="n">capacity</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-14-14" name="__codelineno-14-14" href="#__codelineno-14-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="mf">2.0</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">3.0</span><span class="p">;</span> <a id="__codelineno-14-14" name="__codelineno-14-14" href="#__codelineno-14-14"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-15" name="__codelineno-14-15" href="#__codelineno-14-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="mi">2</span><span class="p">;</span> <a id="__codelineno-14-15" name="__codelineno-14-15" href="#__codelineno-14-15"></a>
<a id="__codelineno-14-16" name="__codelineno-14-16" href="#__codelineno-14-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="k">new</span><span class="w"> </span><span class="n">Pair</span><span class="o">[</span><span class="n">capacity</span><span class="o">]</span><span class="p">;</span> <a id="__codelineno-14-16" name="__codelineno-14-16" href="#__codelineno-14-16"></a><span class="w"> </span><span class="cm">/* 哈希函数 */</span>
<a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-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="k">new</span><span class="w"> </span><span class="n">Pair</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-1&quot;</span><span class="p">);</span> <a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-17"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">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-14-18" name="__codelineno-14-18" href="#__codelineno-14-18"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-18" name="__codelineno-14-18" href="#__codelineno-14-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-14-19" name="__codelineno-14-19" href="#__codelineno-14-19"></a> <a id="__codelineno-14-19" name="__codelineno-14-19" href="#__codelineno-14-19"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-20" name="__codelineno-14-20" href="#__codelineno-14-20"></a><span class="w"> </span><span class="cm">/* 哈希函数 */</span> <a id="__codelineno-14-20" name="__codelineno-14-20" href="#__codelineno-14-20"></a>
<a id="__codelineno-14-21" name="__codelineno-14-21" href="#__codelineno-14-21"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">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-14-21" name="__codelineno-14-21" href="#__codelineno-14-21"></a><span class="w"> </span><span class="cm">/* 负载因子 */</span>
<a id="__codelineno-14-22" name="__codelineno-14-22" href="#__codelineno-14-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-14-22" name="__codelineno-14-22" href="#__codelineno-14-22"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="nf">loadFactor</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-23" name="__codelineno-14-23" href="#__codelineno-14-23"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-23" name="__codelineno-14-23" href="#__codelineno-14-23"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</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-14-24" name="__codelineno-14-24" href="#__codelineno-14-24"></a> <a id="__codelineno-14-24" name="__codelineno-14-24" href="#__codelineno-14-24"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-25" name="__codelineno-14-25" href="#__codelineno-14-25"></a><span class="w"> </span><span class="cm">/* 负载因子 */</span> <a id="__codelineno-14-25" name="__codelineno-14-25" href="#__codelineno-14-25"></a>
<a id="__codelineno-14-26" name="__codelineno-14-26" href="#__codelineno-14-26"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="nf">loadFactor</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-26" name="__codelineno-14-26" href="#__codelineno-14-26"></a><span class="w"> </span><span class="cm">/* 搜索 key 对应的桶索引 */</span>
<a id="__codelineno-14-27" name="__codelineno-14-27" href="#__codelineno-14-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</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-14-27" name="__codelineno-14-27" href="#__codelineno-14-27"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">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-14-28" name="__codelineno-14-28" href="#__codelineno-14-28"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-28" name="__codelineno-14-28" href="#__codelineno-14-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-14-29" name="__codelineno-14-29" href="#__codelineno-14-29"></a> <a id="__codelineno-14-29" name="__codelineno-14-29" href="#__codelineno-14-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="mi">1</span><span class="p">;</span>
<a id="__codelineno-14-30" name="__codelineno-14-30" href="#__codelineno-14-30"></a><span class="w"> </span><span class="cm">/* 查询操作 */</span> <a id="__codelineno-14-30" name="__codelineno-14-30" href="#__codelineno-14-30"></a><span class="w"> </span><span class="c1">// 线性探测,当遇到空桶时跳出</span>
<a id="__codelineno-14-31" name="__codelineno-14-31" href="#__codelineno-14-31"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">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-14-31" name="__codelineno-14-31" href="#__codelineno-14-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="o">[</span><span class="n">index</span><span class="o">]</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-14-32" name="__codelineno-14-32" href="#__codelineno-14-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-14-32" name="__codelineno-14-32" href="#__codelineno-14-32"></a><span class="w"> </span><span class="c1">// 若遇到 key ,返回对应桶索引</span>
<a id="__codelineno-14-33" name="__codelineno-14-33" href="#__codelineno-14-33"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-14-33" name="__codelineno-14-33" href="#__codelineno-14-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="o">[</span><span class="n">index</span><span class="o">]</span><span class="p">.</span><span class="na">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-14-34" name="__codelineno-14-34" href="#__codelineno-14-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="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</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-14-34" name="__codelineno-14-34" href="#__codelineno-14-34"></a><span class="w"> </span><span class="c1">// 若之前遇到了删除标记,则将键值对移动至该索引</span>
<a id="__codelineno-14-35" name="__codelineno-14-35" href="#__codelineno-14-35"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-14-35" name="__codelineno-14-35" href="#__codelineno-14-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="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-36" name="__codelineno-14-36" href="#__codelineno-14-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-14-36" name="__codelineno-14-36" href="#__codelineno-14-36"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">firstTombstone</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-14-37" name="__codelineno-14-37" href="#__codelineno-14-37"></a><span class="w"> </span><span class="c1">// 若遇到空桶,说明无此 key ,则返回 null</span> <a id="__codelineno-14-37" name="__codelineno-14-37" href="#__codelineno-14-37"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</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-14-38" name="__codelineno-14-38" href="#__codelineno-14-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="o">[</span><span class="n">j</span><span class="o">]</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-14-38" name="__codelineno-14-38" href="#__codelineno-14-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-14-39" name="__codelineno-14-39" href="#__codelineno-14-39"></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-14-39" name="__codelineno-14-39" href="#__codelineno-14-39"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-40" name="__codelineno-14-40" href="#__codelineno-14-40"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则返回对应 val</span> <a id="__codelineno-14-40" name="__codelineno-14-40" href="#__codelineno-14-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-14-41" name="__codelineno-14-41" href="#__codelineno-14-41"></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="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">.</span><span class="na">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">&amp;&amp;</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">j</span><span class="o">]</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-14-41" name="__codelineno-14-41" href="#__codelineno-14-41"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-42" name="__codelineno-14-42" href="#__codelineno-14-42"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">.</span><span class="na">val</span><span class="p">;</span> <a id="__codelineno-14-42" name="__codelineno-14-42" href="#__codelineno-14-42"></a><span class="w"> </span><span class="c1">// 记录遇到的首个删除标记</span>
<a id="__codelineno-14-43" name="__codelineno-14-43" href="#__codelineno-14-43"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-43" name="__codelineno-14-43" href="#__codelineno-14-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="mi">1</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</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-14-44" name="__codelineno-14-44" href="#__codelineno-14-44"></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-14-44" name="__codelineno-14-44" href="#__codelineno-14-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-14-45" name="__codelineno-14-45" href="#__codelineno-14-45"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-45" name="__codelineno-14-45" href="#__codelineno-14-45"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-46" name="__codelineno-14-46" href="#__codelineno-14-46"></a> <a id="__codelineno-14-46" name="__codelineno-14-46" href="#__codelineno-14-46"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span>
<a id="__codelineno-14-47" name="__codelineno-14-47" href="#__codelineno-14-47"></a><span class="w"> </span><span class="cm">/* 添加操作 */</span> <a id="__codelineno-14-47" name="__codelineno-14-47" href="#__codelineno-14-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="mi">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-14-48" name="__codelineno-14-48" href="#__codelineno-14-48"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">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="n">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-14-48" name="__codelineno-14-48" href="#__codelineno-14-48"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-49" name="__codelineno-14-49" href="#__codelineno-14-49"></a><span class="w"> </span><span class="c1">// 当负载因子超过阈值时,执行扩容</span> <a id="__codelineno-14-49" name="__codelineno-14-49" href="#__codelineno-14-49"></a><span class="w"> </span><span class="c1">// 若 key 不存在,则返回添加点的索引</span>
<a id="__codelineno-14-50" name="__codelineno-14-50" href="#__codelineno-14-50"></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">&gt;</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-14-50" name="__codelineno-14-50" href="#__codelineno-14-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="mi">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="p">:</span><span class="w"> </span><span class="n">firstTombstone</span><span class="p">;</span>
<a id="__codelineno-14-51" name="__codelineno-14-51" href="#__codelineno-14-51"></a><span class="w"> </span><span class="n">extend</span><span class="p">();</span> <a id="__codelineno-14-51" name="__codelineno-14-51" href="#__codelineno-14-51"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-52" name="__codelineno-14-52" href="#__codelineno-14-52"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-52" name="__codelineno-14-52" href="#__codelineno-14-52"></a>
<a id="__codelineno-14-53" name="__codelineno-14-53" href="#__codelineno-14-53"></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-14-53" name="__codelineno-14-53" href="#__codelineno-14-53"></a><span class="w"> </span><span class="cm">/* 查询操作 */</span>
<a id="__codelineno-14-54" name="__codelineno-14-54" href="#__codelineno-14-54"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-14-54" name="__codelineno-14-54" href="#__codelineno-14-54"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">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-14-55" name="__codelineno-14-55" href="#__codelineno-14-55"></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">&lt;</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-14-55" name="__codelineno-14-55" href="#__codelineno-14-55"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
<a id="__codelineno-14-56" name="__codelineno-14-56" href="#__codelineno-14-56"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-14-56" name="__codelineno-14-56" href="#__codelineno-14-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-14-57" name="__codelineno-14-57" href="#__codelineno-14-57"></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-14-57" name="__codelineno-14-57" href="#__codelineno-14-57"></a><span class="w"> </span><span class="c1">// 若找到键值对,则返回对应 val</span>
<a id="__codelineno-14-58" name="__codelineno-14-58" href="#__codelineno-14-58"></a><span class="w"> </span><span class="c1">// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶</span> <a id="__codelineno-14-58" name="__codelineno-14-58" href="#__codelineno-14-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="o">[</span><span class="n">index</span><span class="o">]</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">&amp;&amp;</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</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-14-59" name="__codelineno-14-59" href="#__codelineno-14-59"></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="o">[</span><span class="n">j</span><span class="o">]</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="o">[</span><span class="n">j</span><span class="o">]</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-14-59" name="__codelineno-14-59" href="#__codelineno-14-59"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</span><span class="p">.</span><span class="na">val</span><span class="p">;</span>
<a id="__codelineno-14-60" name="__codelineno-14-60" href="#__codelineno-14-60"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">j</span><span class="o">]</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-14-60" name="__codelineno-14-60" href="#__codelineno-14-60"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-61" name="__codelineno-14-61" href="#__codelineno-14-61"></a><span class="w"> </span><span class="n">size</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-14-61" name="__codelineno-14-61" href="#__codelineno-14-61"></a><span class="w"> </span><span class="c1">// 若键值对不存在,则返回 null</span>
<a id="__codelineno-14-62" name="__codelineno-14-62" href="#__codelineno-14-62"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-14-62" name="__codelineno-14-62" href="#__codelineno-14-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-14-63" name="__codelineno-14-63" href="#__codelineno-14-63"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-63" name="__codelineno-14-63" href="#__codelineno-14-63"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-64" name="__codelineno-14-64" href="#__codelineno-14-64"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则更新对应 val</span> <a id="__codelineno-14-64" name="__codelineno-14-64" href="#__codelineno-14-64"></a>
<a id="__codelineno-14-65" name="__codelineno-14-65" href="#__codelineno-14-65"></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="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">.</span><span class="na">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-14-65" name="__codelineno-14-65" href="#__codelineno-14-65"></a><span class="w"> </span><span class="cm">/* 添加操作 */</span>
<a id="__codelineno-14-66" name="__codelineno-14-66" href="#__codelineno-14-66"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">.</span><span class="na">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-14-66" name="__codelineno-14-66" href="#__codelineno-14-66"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">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="n">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-14-67" name="__codelineno-14-67" href="#__codelineno-14-67"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-14-67" name="__codelineno-14-67" href="#__codelineno-14-67"></a><span class="w"> </span><span class="c1">// 当负载因子超过阈值时,执行扩容</span>
<a id="__codelineno-14-68" name="__codelineno-14-68" href="#__codelineno-14-68"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-68" name="__codelineno-14-68" href="#__codelineno-14-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">&gt;</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-14-69" name="__codelineno-14-69" href="#__codelineno-14-69"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-69" name="__codelineno-14-69" href="#__codelineno-14-69"></a><span class="w"> </span><span class="n">extend</span><span class="p">();</span>
<a id="__codelineno-14-70" name="__codelineno-14-70" href="#__codelineno-14-70"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-70" name="__codelineno-14-70" href="#__codelineno-14-70"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-71" name="__codelineno-14-71" href="#__codelineno-14-71"></a> <a id="__codelineno-14-71" name="__codelineno-14-71" href="#__codelineno-14-71"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
<a id="__codelineno-14-72" name="__codelineno-14-72" href="#__codelineno-14-72"></a><span class="w"> </span><span class="cm">/* 删除操作 */</span> <a id="__codelineno-14-72" name="__codelineno-14-72" href="#__codelineno-14-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-14-73" name="__codelineno-14-73" href="#__codelineno-14-73"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">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-14-73" name="__codelineno-14-73" href="#__codelineno-14-73"></a><span class="w"> </span><span class="c1">// 若找到键值对,则覆盖 val 并返回</span>
<a id="__codelineno-14-74" name="__codelineno-14-74" href="#__codelineno-14-74"></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-14-74" name="__codelineno-14-74" href="#__codelineno-14-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="o">[</span><span class="n">index</span><span class="o">]</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">&amp;&amp;</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</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-14-75" name="__codelineno-14-75" href="#__codelineno-14-75"></a><span class="w"> </span><span class="c1">// 线性探测,从 index 开始向后遍历</span> <a id="__codelineno-14-75" name="__codelineno-14-75" href="#__codelineno-14-75"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</span><span class="p">.</span><span class="na">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-14-76" name="__codelineno-14-76" href="#__codelineno-14-76"></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">&lt;</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-14-76" name="__codelineno-14-76" href="#__codelineno-14-76"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
<a id="__codelineno-14-77" name="__codelineno-14-77" href="#__codelineno-14-77"></a><span class="w"> </span><span class="c1">// 计算桶索引,越过尾部返回头部</span> <a id="__codelineno-14-77" name="__codelineno-14-77" href="#__codelineno-14-77"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-78" name="__codelineno-14-78" href="#__codelineno-14-78"></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-14-78" name="__codelineno-14-78" href="#__codelineno-14-78"></a><span class="w"> </span><span class="c1">// 若键值对不存在,则添加该键值对</span>
<a id="__codelineno-14-79" name="__codelineno-14-79" href="#__codelineno-14-79"></a><span class="w"> </span><span class="c1">// 若遇到空桶,说明无此 key ,则直接返回</span> <a id="__codelineno-14-79" name="__codelineno-14-79" href="#__codelineno-14-79"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</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-14-80" name="__codelineno-14-80" href="#__codelineno-14-80"></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="o">[</span><span class="n">j</span><span class="o">]</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-14-80" name="__codelineno-14-80" href="#__codelineno-14-80"></a><span class="w"> </span><span class="n">size</span><span class="o">++</span><span class="p">;</span>
<a id="__codelineno-14-81" name="__codelineno-14-81" href="#__codelineno-14-81"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-14-81" name="__codelineno-14-81" href="#__codelineno-14-81"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-82" name="__codelineno-14-82" href="#__codelineno-14-82"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-82" name="__codelineno-14-82" href="#__codelineno-14-82"></a>
<a id="__codelineno-14-83" name="__codelineno-14-83" href="#__codelineno-14-83"></a><span class="w"> </span><span class="c1">// 若遇到指定 key ,则标记删除并返回</span> <a id="__codelineno-14-83" name="__codelineno-14-83" href="#__codelineno-14-83"></a><span class="w"> </span><span class="cm">/* 删除操作 */</span>
<a id="__codelineno-14-84" name="__codelineno-14-84" href="#__codelineno-14-84"></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="o">[</span><span class="n">j</span><span class="o">]</span><span class="p">.</span><span class="na">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-14-84" name="__codelineno-14-84" href="#__codelineno-14-84"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">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-14-85" name="__codelineno-14-85" href="#__codelineno-14-85"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">j</span><span class="o">]</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-14-85" name="__codelineno-14-85" href="#__codelineno-14-85"></a><span class="w"> </span><span class="c1">// 搜索 key 对应的桶索引</span>
<a id="__codelineno-14-86" name="__codelineno-14-86" href="#__codelineno-14-86"></a><span class="w"> </span><span class="n">size</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-14-86" name="__codelineno-14-86" href="#__codelineno-14-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-14-87" name="__codelineno-14-87" href="#__codelineno-14-87"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-14-87" name="__codelineno-14-87" href="#__codelineno-14-87"></a><span class="w"> </span><span class="c1">// 若找到键值对,则用删除标记覆盖它</span>
<a id="__codelineno-14-88" name="__codelineno-14-88" href="#__codelineno-14-88"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-88" name="__codelineno-14-88" href="#__codelineno-14-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="o">[</span><span class="n">index</span><span class="o">]</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">&amp;&amp;</span><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</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-14-89" name="__codelineno-14-89" href="#__codelineno-14-89"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-89" name="__codelineno-14-89" href="#__codelineno-14-89"></a><span class="w"> </span><span class="n">buckets</span><span class="o">[</span><span class="n">index</span><span class="o">]</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-14-90" name="__codelineno-14-90" href="#__codelineno-14-90"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-90" name="__codelineno-14-90" href="#__codelineno-14-90"></a><span class="w"> </span><span class="n">size</span><span class="o">--</span><span class="p">;</span>
<a id="__codelineno-14-91" name="__codelineno-14-91" href="#__codelineno-14-91"></a> <a id="__codelineno-14-91" name="__codelineno-14-91" href="#__codelineno-14-91"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-92" name="__codelineno-14-92" href="#__codelineno-14-92"></a><span class="w"> </span><span class="cm">/* 扩容哈希表 */</span> <a id="__codelineno-14-92" name="__codelineno-14-92" href="#__codelineno-14-92"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-93" name="__codelineno-14-93" href="#__codelineno-14-93"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">extend</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-93" name="__codelineno-14-93" href="#__codelineno-14-93"></a>
<a id="__codelineno-14-94" name="__codelineno-14-94" href="#__codelineno-14-94"></a><span class="w"> </span><span class="c1">// 暂存原哈希表</span> <a id="__codelineno-14-94" name="__codelineno-14-94" href="#__codelineno-14-94"></a><span class="w"> </span><span class="cm">/* 扩容哈希表 */</span>
<a id="__codelineno-14-95" name="__codelineno-14-95" href="#__codelineno-14-95"></a><span class="w"> </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-14-95" name="__codelineno-14-95" href="#__codelineno-14-95"></a><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">extend</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-96" name="__codelineno-14-96" href="#__codelineno-14-96"></a><span class="w"> </span><span class="c1">// 初始化扩容后的新哈希表</span> <a id="__codelineno-14-96" name="__codelineno-14-96" href="#__codelineno-14-96"></a><span class="w"> </span><span class="c1">// 暂存原哈希表</span>
<a id="__codelineno-14-97" name="__codelineno-14-97" href="#__codelineno-14-97"></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-14-97" name="__codelineno-14-97" href="#__codelineno-14-97"></a><span class="w"> </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-14-98" name="__codelineno-14-98" href="#__codelineno-14-98"></a><span class="w"> </span><span class="n">buckets</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="o">[</span><span class="n">capacity</span><span class="o">]</span><span class="p">;</span> <a id="__codelineno-14-98" name="__codelineno-14-98" href="#__codelineno-14-98"></a><span class="w"> </span><span class="c1">// 初始化扩容后的新哈希表</span>
<a id="__codelineno-14-99" name="__codelineno-14-99" href="#__codelineno-14-99"></a><span class="w"> </span><span class="n">size</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-14-99" name="__codelineno-14-99" href="#__codelineno-14-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-14-100" name="__codelineno-14-100" href="#__codelineno-14-100"></a><span class="w"> </span><span class="c1">// 将键值对从原哈希表搬运至新哈希表</span> <a id="__codelineno-14-100" name="__codelineno-14-100" href="#__codelineno-14-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="k">new</span><span class="w"> </span><span class="n">Pair</span><span class="o">[</span><span class="n">capacity</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-14-101" name="__codelineno-14-101" href="#__codelineno-14-101"></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="w"> </span><span class="n">pair</span><span class="w"> </span><span class="p">:</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-14-101" name="__codelineno-14-101" href="#__codelineno-14-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="mi">0</span><span class="p">;</span>
<a id="__codelineno-14-102" name="__codelineno-14-102" href="#__codelineno-14-102"></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">&amp;&amp;</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-14-102" name="__codelineno-14-102" href="#__codelineno-14-102"></a><span class="w"> </span><span class="c1">// 将键值对从原哈希表搬运至新哈希表</span>
<a id="__codelineno-14-103" name="__codelineno-14-103" href="#__codelineno-14-103"></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="na">key</span><span class="p">,</span><span class="w"> </span><span class="n">pair</span><span class="p">.</span><span class="na">val</span><span class="p">);</span> <a id="__codelineno-14-103" name="__codelineno-14-103" href="#__codelineno-14-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="w"> </span><span class="n">pair</span><span class="w"> </span><span class="p">:</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-14-104" name="__codelineno-14-104" href="#__codelineno-14-104"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-104" name="__codelineno-14-104" href="#__codelineno-14-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">&amp;&amp;</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-14-105" name="__codelineno-14-105" href="#__codelineno-14-105"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-105" name="__codelineno-14-105" href="#__codelineno-14-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="na">key</span><span class="p">,</span><span class="w"> </span><span class="n">pair</span><span class="p">.</span><span class="na">val</span><span class="p">);</span>
<a id="__codelineno-14-106" name="__codelineno-14-106" href="#__codelineno-14-106"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-106" name="__codelineno-14-106" href="#__codelineno-14-106"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-107" name="__codelineno-14-107" href="#__codelineno-14-107"></a> <a id="__codelineno-14-107" name="__codelineno-14-107" href="#__codelineno-14-107"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-108" name="__codelineno-14-108" href="#__codelineno-14-108"></a><span class="w"> </span><span class="cm">/* 打印哈希表 */</span> <a id="__codelineno-14-108" name="__codelineno-14-108" href="#__codelineno-14-108"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-109" name="__codelineno-14-109" href="#__codelineno-14-109"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-109" name="__codelineno-14-109" href="#__codelineno-14-109"></a>
<a id="__codelineno-14-110" name="__codelineno-14-110" href="#__codelineno-14-110"></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="w"> </span><span class="n">pair</span><span class="w"> </span><span class="p">:</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-14-110" name="__codelineno-14-110" href="#__codelineno-14-110"></a><span class="w"> </span><span class="cm">/* 打印哈希表 */</span>
<a id="__codelineno-14-111" name="__codelineno-14-111" href="#__codelineno-14-111"></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-14-111" name="__codelineno-14-111" href="#__codelineno-14-111"></a><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-112" name="__codelineno-14-112" href="#__codelineno-14-112"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">pair</span><span class="p">.</span><span class="na">key</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot; -&gt; &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">pair</span><span class="p">.</span><span class="na">val</span><span class="p">);</span> <a id="__codelineno-14-112" name="__codelineno-14-112" href="#__codelineno-14-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="w"> </span><span class="n">pair</span><span class="w"> </span><span class="p">:</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-14-113" name="__codelineno-14-113" href="#__codelineno-14-113"></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-14-113" name="__codelineno-14-113" href="#__codelineno-14-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-14-114" name="__codelineno-14-114" href="#__codelineno-14-114"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">&quot;null&quot;</span><span class="p">);</span> <a id="__codelineno-14-114" name="__codelineno-14-114" href="#__codelineno-14-114"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">&quot;null&quot;</span><span class="p">);</span>
<a id="__codelineno-14-115" name="__codelineno-14-115" href="#__codelineno-14-115"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-115" name="__codelineno-14-115" href="#__codelineno-14-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-14-116" name="__codelineno-14-116" href="#__codelineno-14-116"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-116" name="__codelineno-14-116" href="#__codelineno-14-116"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">&quot;TOMBSTONE&quot;</span><span class="p">);</span>
<a id="__codelineno-14-117" name="__codelineno-14-117" href="#__codelineno-14-117"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-117" name="__codelineno-14-117" href="#__codelineno-14-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-14-118" name="__codelineno-14-118" href="#__codelineno-14-118"></a><span class="p">}</span> <a id="__codelineno-14-118" name="__codelineno-14-118" href="#__codelineno-14-118"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">pair</span><span class="p">.</span><span class="na">key</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot; -&gt; &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">pair</span><span class="p">.</span><span class="na">val</span><span class="p">);</span>
<a id="__codelineno-14-119" name="__codelineno-14-119" href="#__codelineno-14-119"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-120" name="__codelineno-14-120" href="#__codelineno-14-120"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-121" name="__codelineno-14-121" href="#__codelineno-14-121"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-122" name="__codelineno-14-122" href="#__codelineno-14-122"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -6062,23 +6141,104 @@
</div> </div>
</div> </div>
</div> </div>
<h3 id="2">2. &nbsp; 多次哈希<a class="headerlink" href="#2" title="Permanent link">&para;</a></h3> <h3 id="2">2. &nbsp; 平方探测<a class="headerlink" href="#2" title="Permanent link">&para;</a></h3>
<p>顾名思义,多次哈希方法是使用多个哈希函数 <span class="arithmatex">\(f_1(x)\)</span><span class="arithmatex">\(f_2(x)\)</span><span class="arithmatex">\(f_3(x)\)</span><span class="arithmatex">\(\dots\)</span> 进行探测</p> <p>平方探测与线性探测类似,都是开放寻址的常见策略之一。当发生冲突时,平方探测不是简单地跳过一个固定的步数,而是跳过“探测次数的平方”的步数,即 <span class="arithmatex">\(1, 4, 9, \dots\)</span> </p>
<p>平方探测通主要具有以下优势。</p>
<ul> <ul>
<li><strong>插入元素</strong>:若哈希函数 <span class="arithmatex">\(f_1(x)\)</span> 出现冲突,则尝试 <span class="arithmatex">\(f_2(x)\)</span> ,以此类推,直到找到空位后插入元素</li> <li>平方探测通过跳过平方的距离,试图缓解线性探测的聚集效应</li>
<li><strong>查找元素</strong>:在相同的哈希函数顺序下进行查找,直到找到目标元素时返回;或遇到空位或已尝试所有哈希函数,说明哈希表中不存在该元素,则返回 <span class="arithmatex">\(\text{None}\)</span> </li> <li>平方探测会跳过更大的距离来寻找空位置,有助于数据分布得更加均匀</li>
</ul>
<p>然而,平方探测也并不是完美的。</p>
<ul>
<li>仍然存在聚集现象,即某些位置比其他位置更容易被占用。</li>
<li>由于平方的增长,平方探测可能不会探测整个哈希表,这意味着即使哈希表中有空桶,平方探测也可能无法访问到它。</li>
</ul>
<h3 id="3">3. &nbsp; 多次哈希<a class="headerlink" href="#3" title="Permanent link">&para;</a></h3>
<p>多次哈希使用多个哈希函数 <span class="arithmatex">\(f_1(x)\)</span><span class="arithmatex">\(f_2(x)\)</span><span class="arithmatex">\(f_3(x)\)</span><span class="arithmatex">\(\dots\)</span> 进行探测。</p>
<ul>
<li><strong>插入元素</strong>:若哈希函数 <span class="arithmatex">\(f_1(x)\)</span> 出现冲突,则尝试 <span class="arithmatex">\(f_2(x)\)</span> ,以此类推,直到找到空桶后插入元素。</li>
<li><strong>查找元素</strong>:在相同的哈希函数顺序下进行查找,直到找到目标元素时返回;或当遇到空桶或已尝试所有哈希函数,说明哈希表中不存在该元素,则返回 <span class="arithmatex">\(\text{None}\)</span></li>
</ul> </ul>
<p>与线性探测相比,多次哈希方法不易产生聚集,但多个哈希函数会增加额外的计算量。</p> <p>与线性探测相比,多次哈希方法不易产生聚集,但多个哈希函数会增加额外的计算量。</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>请注意,开放寻址(线性探测、平方探测和多次哈希)哈希表都存在“不能直接删除元素”的问题。</p>
</div>
<h2 id="623">6.2.3 &nbsp; 编程语言的选择<a class="headerlink" href="#623" title="Permanent link">&para;</a></h2> <h2 id="623">6.2.3 &nbsp; 编程语言的选择<a class="headerlink" href="#623" title="Permanent link">&para;</a></h2>
<p>Java 采用链式地址。自 JDK 1.8 以来,当 HashMap 内数组长度达到 64 且链表长度达到 8 时,链表会被转换为红黑树以提升查找性能</p> <p>各个编程语言采取了不同的哈希表实现策略,以下举几个例子</p>
<p>Python 采用开放寻址。字典 dict 使用伪随机数进行探测。</p> <ul>
<p>Golang 采用链式地址。Go 规定每个桶最多存储 8 个键值对,超出容量则连接一个溢出桶;当溢出桶过多时,会执行一次特殊的等量扩容操作,以确保性能。</p> <li>Java 采用链式地址。自 JDK 1.8 以来,当 HashMap 内数组长度达到 64 且链表长度达到 8 时,链表会被转换为红黑树以提升查找性能。</li>
<li>Python 采用开放寻址。字典 dict 使用伪随机数进行探测。</li>
<li>Golang 采用链式地址。Go 规定每个桶最多存储 8 个键值对,超出容量则连接一个溢出桶。当溢出桶过多时,会执行一次特殊的等量扩容操作,以确保性能。</li>
</ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
<h2 id="__comments">评论</h2> >
<!-- Link to previous page -->
<a
href="../hash_map/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 6.1 &amp;nbsp; 哈希表"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
6.1 &nbsp; 哈希表
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../hash_algorithm/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 6.3 &amp;nbsp; 哈希算法"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.3 &nbsp; 哈希算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -6144,48 +6304,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../hash_map/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 6.1 &amp;nbsp; 哈希表" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
6.1 &nbsp; 哈希表 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../hash_algorithm/" class="md-footer__link md-footer__link--next" aria-label="下一页: 6.3 &amp;nbsp; 哈希算法" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.3 &nbsp; 哈希算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -6196,6 +6339,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+131 -54
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/hash_map.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/hash_map.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="61">6.1 &nbsp; 哈希表<a class="headerlink" href="#61" title="Permanent link">&para;</a></h1> <h1 id="61">6.1 &nbsp; 哈希表<a class="headerlink" href="#61" title="Permanent link">&para;</a></h1>
<p>「哈希表 hash table」,又称「散列表」,其通过建立键 <code>key</code> 与值 <code>value</code> 之间的映射,实现高效的元素查询。具体而言,我们向哈希表输入一个键 <code>key</code> ,则可以在 <span class="arithmatex">\(O(1)\)</span> 时间内获取对应的值 <code>value</code></p> <p>「哈希表 hash table」,又称「散列表」,其通过建立键 <code>key</code> 与值 <code>value</code> 之间的映射,实现高效的元素查询。具体而言,我们向哈希表输入一个键 <code>key</code> ,则可以在 <span class="arithmatex">\(O(1)\)</span> 时间内获取对应的值 <code>value</code></p>
<p>如图 6-1 所示,给定 <span class="arithmatex">\(n\)</span> 个学生,每个学生都有“姓名”和“学号”两项数据。假如我们希望实现“输入一个学号,返回对应的姓名”的查询功能,则可以采用图 6-1 所示的哈希表来实现。</p> <p>如图 6-1 所示,给定 <span class="arithmatex">\(n\)</span> 个学生,每个学生都有“姓名”和“学号”两项数据。假如我们希望实现“输入一个学号,返回对应的姓名”的查询功能,则可以采用图 6-1 所示的哈希表来实现。</p>
@@ -4543,11 +4575,10 @@
<a id="__codelineno-31-77" name="__codelineno-31-77" href="#__codelineno-31-77"></a><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-31-77" name="__codelineno-31-77" href="#__codelineno-31-77"></a><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-31-78" name="__codelineno-31-78" href="#__codelineno-31-78"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">pairSet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">entries</span><span class="p">();</span> <a id="__codelineno-31-78" name="__codelineno-31-78" href="#__codelineno-31-78"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">pairSet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">entries</span><span class="p">();</span>
<a id="__codelineno-31-79" name="__codelineno-31-79" href="#__codelineno-31-79"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">pair</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">pairSet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-31-79" name="__codelineno-31-79" href="#__codelineno-31-79"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">pair</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">pairSet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-31-80" name="__codelineno-31-80" href="#__codelineno-31-80"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">pair</span><span class="p">)</span><span class="w"> </span><span class="k">continue</span><span class="p">;</span> <a id="__codelineno-31-80" name="__codelineno-31-80" href="#__codelineno-31-80"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">key</span><span class="si">}</span><span class="sb"> -&gt; </span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">val</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span>
<a id="__codelineno-31-81" name="__codelineno-31-81" href="#__codelineno-31-81"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">key</span><span class="si">}</span><span class="sb"> -&gt; </span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">val</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span> <a id="__codelineno-31-81" name="__codelineno-31-81" href="#__codelineno-31-81"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-31-82" name="__codelineno-31-82" href="#__codelineno-31-82"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-31-82" name="__codelineno-31-82" href="#__codelineno-31-82"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-31-83" name="__codelineno-31-83" href="#__codelineno-31-83"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-31-83" name="__codelineno-31-83" href="#__codelineno-31-83"></a><span class="p">}</span>
<a id="__codelineno-31-84" name="__codelineno-31-84" href="#__codelineno-31-84"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -4634,11 +4665,10 @@
<a id="__codelineno-32-81" name="__codelineno-32-81" href="#__codelineno-32-81"></a><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-32-81" name="__codelineno-32-81" href="#__codelineno-32-81"></a><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-32-82" name="__codelineno-32-82" href="#__codelineno-32-82"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">pairSet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">entries</span><span class="p">();</span> <a id="__codelineno-32-82" name="__codelineno-32-82" href="#__codelineno-32-82"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">pairSet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">entries</span><span class="p">();</span>
<a id="__codelineno-32-83" name="__codelineno-32-83" href="#__codelineno-32-83"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">pair</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">pairSet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-32-83" name="__codelineno-32-83" href="#__codelineno-32-83"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">pair</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">pairSet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-32-84" name="__codelineno-32-84" href="#__codelineno-32-84"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">pair</span><span class="p">)</span><span class="w"> </span><span class="k">continue</span><span class="p">;</span> <a id="__codelineno-32-84" name="__codelineno-32-84" href="#__codelineno-32-84"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">key</span><span class="si">}</span><span class="sb"> -&gt; </span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">val</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span>
<a id="__codelineno-32-85" name="__codelineno-32-85" href="#__codelineno-32-85"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="sb">`</span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">key</span><span class="si">}</span><span class="sb"> -&gt; </span><span class="si">${</span><span class="nx">pair</span><span class="p">.</span><span class="nx">val</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span> <a id="__codelineno-32-85" name="__codelineno-32-85" href="#__codelineno-32-85"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-32-86" name="__codelineno-32-86" href="#__codelineno-32-86"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-32-86" name="__codelineno-32-86" href="#__codelineno-32-86"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-32-87" name="__codelineno-32-87" href="#__codelineno-32-87"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-32-87" name="__codelineno-32-87" href="#__codelineno-32-87"></a><span class="p">}</span>
<a id="__codelineno-32-88" name="__codelineno-32-88" href="#__codelineno-32-88"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -4937,11 +4967,73 @@
<p>类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,非常耗时。并且由于哈希表容量 <code>capacity</code> 改变,我们需要通过哈希函数来重新计算所有键值对的存储位置,这进一步提高了扩容过程的计算开销。为此,编程语言通常会预留足够大的哈希表容量,防止频繁扩容。</p> <p>类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,非常耗时。并且由于哈希表容量 <code>capacity</code> 改变,我们需要通过哈希函数来重新计算所有键值对的存储位置,这进一步提高了扩容过程的计算开销。为此,编程语言通常会预留足够大的哈希表容量,防止频繁扩容。</p>
<p>「负载因子 load factor」是哈希表的一个重要概念,其定义为哈希表的元素数量除以桶数量,用于衡量哈希冲突的严重程度,<strong>也常被作为哈希表扩容的触发条件</strong>。例如在 Java 中,当负载因子超过 <span class="arithmatex">\(0.75\)</span> 时,系统会将哈希表容量扩展为原先的 <span class="arithmatex">\(2\)</span> 倍。</p> <p>「负载因子 load factor」是哈希表的一个重要概念,其定义为哈希表的元素数量除以桶数量,用于衡量哈希冲突的严重程度,<strong>也常被作为哈希表扩容的触发条件</strong>。例如在 Java 中,当负载因子超过 <span class="arithmatex">\(0.75\)</span> 时,系统会将哈希表容量扩展为原先的 <span class="arithmatex">\(2\)</span> 倍。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 6 章 &amp;nbsp; 哈希表"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 6 章 &nbsp; 哈希表
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../hash_collision/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 6.2 &amp;nbsp; 哈希冲突"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.2 &nbsp; 哈希冲突
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5007,48 +5099,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 6 章 &amp;nbsp; 哈希表" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 6 章 &nbsp; 哈希表 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../hash_collision/" class="md-footer__link md-footer__link--next" aria-label="下一页: 6.2 &amp;nbsp; 哈希冲突" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.2 &nbsp; 哈希冲突
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5059,6 +5134,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="6">第 6 章 &nbsp; 哈希表<a class="headerlink" href="#6" title="Permanent link">&para;</a></h1> <h1 id="6">第 6 章 &nbsp; 哈希表<a class="headerlink" href="#6" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="哈希表" src="../assets/covers/chapter_hashing.jpg" width="600" /></p> <p><img alt="哈希表" src="../assets/covers/chapter_hashing.jpg" width="600" /></p>
@@ -3397,11 +3429,73 @@
<li><a href="https://www.hello-algo.com/chapter_hashing/summary/">6.4 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_hashing/summary/">6.4 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_stack_and_queue/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 5.4 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
5.4 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="hash_map/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 6.1 &amp;nbsp; 哈希表"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.1 &nbsp; 哈希表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3467,48 +3561,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_stack_and_queue/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 5.4 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5.4 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="hash_map/" class="md-footer__link md-footer__link--next" aria-label="下一页: 6.1 &amp;nbsp; 哈希表" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
6.1 &nbsp; 哈希表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3519,6 +3596,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_hashing/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="64">6.4 &nbsp; 小结<a class="headerlink" href="#64" title="Permanent link">&para;</a></h1> <h1 id="64">6.4 &nbsp; 小结<a class="headerlink" href="#64" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3486,11 +3518,73 @@
<p>哈希函数的最后一步往往是对数组长度 <span class="arithmatex">\(n\)</span> 取余,让输出值落入在数组索引范围;在扩容后,数组长度 <span class="arithmatex">\(n\)</span> 发生变化,而 <code>key</code> 对应的索引也可能发生变化。原先落在同一个桶的多个 <code>key</code> ,在扩容后可能会被分配到多个桶中,从而实现哈希冲突的缓解。</p> <p>哈希函数的最后一步往往是对数组长度 <span class="arithmatex">\(n\)</span> 取余,让输出值落入在数组索引范围;在扩容后,数组长度 <span class="arithmatex">\(n\)</span> 发生变化,而 <code>key</code> 对应的索引也可能发生变化。原先落在同一个桶的多个 <code>key</code> ,在扩容后可能会被分配到多个桶中,从而实现哈希冲突的缓解。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../hash_algorithm/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 6.3 &amp;nbsp; 哈希算法"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
6.3 &nbsp; 哈希算法
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_tree/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 7 章 &amp;nbsp; 树"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 7 章 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3556,48 +3650,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../hash_algorithm/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 6.3 &amp;nbsp; 哈希算法" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
6.3 &nbsp; 哈希算法 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_tree/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 7 章 &amp;nbsp; 树" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 7 章 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3608,6 +3685,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/build_heap.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/build_heap.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="82">8.2 &nbsp; 建堆操作<a class="headerlink" href="#82" title="Permanent link">&para;</a></h1> <h1 id="82">8.2 &nbsp; 建堆操作<a class="headerlink" href="#82" title="Permanent link">&para;</a></h1>
<p>在某些情况下,我们希望使用一个列表的所有元素来构建一个堆,这个过程被称为“建堆操作”。</p> <p>在某些情况下,我们希望使用一个列表的所有元素来构建一个堆,这个过程被称为“建堆操作”。</p>
<h2 id="821">8.2.1 &nbsp; 借助入堆操作实现<a class="headerlink" href="#821" title="Permanent link">&para;</a></h2> <h2 id="821">8.2.1 &nbsp; 借助入堆操作实现<a class="headerlink" href="#821" title="Permanent link">&para;</a></h2>
@@ -3661,11 +3693,73 @@ T(h) &amp; = 2 \frac{1 - 2^h}{1 - 2} - h \newline
\]</div> \]</div>
<p>进一步地,高度为 <span class="arithmatex">\(h\)</span> 的完美二叉树的节点数量为 <span class="arithmatex">\(n = 2^{h+1} - 1\)</span> ,易得复杂度为 <span class="arithmatex">\(O(2^h) = O(n)\)</span> 。以上推算表明,<strong>输入列表并建堆的时间复杂度为 <span class="arithmatex">\(O(n)\)</span> ,非常高效</strong></p> <p>进一步地,高度为 <span class="arithmatex">\(h\)</span> 的完美二叉树的节点数量为 <span class="arithmatex">\(n = 2^{h+1} - 1\)</span> ,易得复杂度为 <span class="arithmatex">\(O(2^h) = O(n)\)</span> 。以上推算表明,<strong>输入列表并建堆的时间复杂度为 <span class="arithmatex">\(O(n)\)</span> ,非常高效</strong></p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../heap/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 8.1 &amp;nbsp; 堆"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
8.1 &nbsp;
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../top_k/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 8.3 &amp;nbsp; Top-K 问题"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.3 &nbsp; Top-K 问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3731,48 +3825,31 @@ T(h) &amp; = 2 \frac{1 - 2^h}{1 - 2} - h \newline
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../heap/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.1 &amp;nbsp; 堆" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8.1 &nbsp; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../top_k/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.3 &amp;nbsp; Top-K 问题" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.3 &nbsp; Top-K 问题
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3783,6 +3860,8 @@ T(h) &amp; = 2 \frac{1 - 2^h}{1 - 2} - h \newline
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+135 -57
View File
@@ -3512,7 +3512,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/heap.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/heap.md" title="编辑此页" class="md-content__button md-icon">
@@ -3523,6 +3548,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="81">8.1 &nbsp;<a class="headerlink" href="#81" title="Permanent link">&para;</a></h1> <h1 id="81">8.1 &nbsp;<a class="headerlink" href="#81" title="Permanent link">&para;</a></h1>
<p>「堆 heap」是一种满足特定条件的完全二叉树,主要可分为图 8-1 所示的两种类型。</p> <p>「堆 heap」是一种满足特定条件的完全二叉树,主要可分为图 8-1 所示的两种类型。</p>
<ul> <ul>
@@ -4600,19 +4632,18 @@
<a id="__codelineno-49-17" name="__codelineno-49-17" href="#__codelineno-49-17"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nb">true</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-49-17" name="__codelineno-49-17" href="#__codelineno-49-17"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nb">true</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-49-18" name="__codelineno-49-18" href="#__codelineno-49-18"></a><span class="w"> </span><span class="c1">// 判断节点 i, l, r 中值最大的节点,记为 ma</span> <a id="__codelineno-49-18" name="__codelineno-49-18" href="#__codelineno-49-18"></a><span class="w"> </span><span class="c1">// 判断节点 i, l, r 中值最大的节点,记为 ma</span>
<a id="__codelineno-49-19" name="__codelineno-49-19" href="#__codelineno-49-19"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">left</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">right</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span> <a id="__codelineno-49-19" name="__codelineno-49-19" href="#__codelineno-49-19"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">left</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">right</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<a id="__codelineno-49-20" name="__codelineno-49-20" href="#__codelineno-49-20"></a><span class="w"> </span><span class="c1">// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出</span> <a id="__codelineno-49-20" name="__codelineno-49-20" href="#__codelineno-49-20"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">l</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">l</span><span class="p">]</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">ma</span><span class="p">])</span>
<a id="__codelineno-49-21" name="__codelineno-49-21" href="#__codelineno-49-21"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">l</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">l</span><span class="p">]</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">ma</span><span class="p">])</span> <a id="__codelineno-49-21" name="__codelineno-49-21" href="#__codelineno-49-21"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">l</span><span class="p">;</span>
<a id="__codelineno-49-22" name="__codelineno-49-22" href="#__codelineno-49-22"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">l</span><span class="p">;</span> <a id="__codelineno-49-22" name="__codelineno-49-22" href="#__codelineno-49-22"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">r</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">r</span><span class="p">]</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">ma</span><span class="p">])</span>
<a id="__codelineno-49-23" name="__codelineno-49-23" href="#__codelineno-49-23"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">r</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">r</span><span class="p">]</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">ma</span><span class="p">])</span> <a id="__codelineno-49-23" name="__codelineno-49-23" href="#__codelineno-49-23"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">r</span><span class="p">;</span>
<a id="__codelineno-49-24" name="__codelineno-49-24" href="#__codelineno-49-24"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">r</span><span class="p">;</span> <a id="__codelineno-49-24" name="__codelineno-49-24" href="#__codelineno-49-24"></a><span class="w"> </span><span class="c1">// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出</span>
<a id="__codelineno-49-25" name="__codelineno-49-25" href="#__codelineno-49-25"></a><span class="w"> </span><span class="c1">// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出</span> <a id="__codelineno-49-25" name="__codelineno-49-25" href="#__codelineno-49-25"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">ma</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">i</span><span class="p">)</span>
<a id="__codelineno-49-26" name="__codelineno-49-26" href="#__codelineno-49-26"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">ma</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">i</span><span class="p">)</span> <a id="__codelineno-49-26" name="__codelineno-49-26" href="#__codelineno-49-26"></a><span class="w"> </span><span class="k">break</span><span class="p">;</span>
<a id="__codelineno-49-27" name="__codelineno-49-27" href="#__codelineno-49-27"></a><span class="w"> </span><span class="k">break</span><span class="p">;</span> <a id="__codelineno-49-27" name="__codelineno-49-27" href="#__codelineno-49-27"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">maxHeap</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">ma</span><span class="p">]);</span>
<a id="__codelineno-49-28" name="__codelineno-49-28" href="#__codelineno-49-28"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">maxHeap</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">maxHeap</span><span class="p">[</span><span class="n">ma</span><span class="p">]);</span> <a id="__codelineno-49-28" name="__codelineno-49-28" href="#__codelineno-49-28"></a><span class="w"> </span><span class="c1">// 循环向下堆化</span>
<a id="__codelineno-49-29" name="__codelineno-49-29" href="#__codelineno-49-29"></a><span class="w"> </span><span class="c1">// 循环向下堆化</span> <a id="__codelineno-49-29" name="__codelineno-49-29" href="#__codelineno-49-29"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ma</span><span class="p">;</span>
<a id="__codelineno-49-30" name="__codelineno-49-30" href="#__codelineno-49-30"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ma</span><span class="p">;</span> <a id="__codelineno-49-30" name="__codelineno-49-30" href="#__codelineno-49-30"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-49-31" name="__codelineno-49-31" href="#__codelineno-49-31"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-49-31" name="__codelineno-49-31" href="#__codelineno-49-31"></a><span class="p">}</span>
<a id="__codelineno-49-32" name="__codelineno-49-32" href="#__codelineno-49-32"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -5004,11 +5035,73 @@
<li><strong>获取最大的 <span class="arithmatex">\(k\)</span> 个元素</strong>:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。</li> <li><strong>获取最大的 <span class="arithmatex">\(k\)</span> 个元素</strong>:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 8 章 &amp;nbsp; 堆"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 8 章 &nbsp;
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../build_heap/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 8.2 &amp;nbsp; 建堆操作"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.2 &nbsp; 建堆操作
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5074,48 +5167,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 8 章 &amp;nbsp; 堆" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 8 章 &nbsp; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../build_heap/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.2 &amp;nbsp; 建堆操作" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.2 &nbsp; 建堆操作
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5126,6 +5202,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="8">第 8 章 &nbsp;<a class="headerlink" href="#8" title="Permanent link">&para;</a></h1> <h1 id="8">第 8 章 &nbsp;<a class="headerlink" href="#8" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="堆" src="../assets/covers/chapter_heap.jpg" width="600" /></p> <p><img alt="堆" src="../assets/covers/chapter_heap.jpg" width="600" /></p>
@@ -3397,11 +3429,73 @@
<li><a href="https://www.hello-algo.com/chapter_heap/summary/">8.4 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_heap/summary/">8.4 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_tree/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 7.6 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
7.6 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="heap/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 8.1 &amp;nbsp; 堆"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.1 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3467,48 +3561,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_tree/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 7.6 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7.6 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="heap/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.1 &amp;nbsp; 堆" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.1 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3519,6 +3596,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="84">8.4 &nbsp; 小结<a class="headerlink" href="#84" title="Permanent link">&para;</a></h1> <h1 id="84">8.4 &nbsp; 小结<a class="headerlink" href="#84" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3458,11 +3490,73 @@
<p>两者不是同一个概念,只是碰巧都叫堆。计算机系统内存中的堆是动态内存分配的一部分,程序在运行时可以使用它来存储数据。程序可以请求一定量的堆内存,用于存储如对象和数组等复杂结构。当这些数据不再需要时,程序需要释放这些内存,以防止内存泄露。相较于栈内存,堆内存的管理和使用需要更谨慎,不恰当的使用可能会导致内存泄露和野指针等问题。</p> <p>两者不是同一个概念,只是碰巧都叫堆。计算机系统内存中的堆是动态内存分配的一部分,程序在运行时可以使用它来存储数据。程序可以请求一定量的堆内存,用于存储如对象和数组等复杂结构。当这些数据不再需要时,程序需要释放这些内存,以防止内存泄露。相较于栈内存,堆内存的管理和使用需要更谨慎,不恰当的使用可能会导致内存泄露和野指针等问题。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../top_k/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 8.3 &amp;nbsp; Top-K 问题"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
8.3 &nbsp; Top-K 问题
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_graph/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 9 章 &amp;nbsp; 图"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 9 章 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3528,48 +3622,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../top_k/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.3 &amp;nbsp; Top-K 问题" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8.3 &nbsp; Top-K 问题 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_graph/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 9 章 &amp;nbsp; 图" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 9 章 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3580,6 +3657,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/top_k.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_heap/top_k.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="83-top-k">8.3 &nbsp; Top-K 问题<a class="headerlink" href="#83-top-k" title="Permanent link">&para;</a></h1> <h1 id="83-top-k">8.3 &nbsp; Top-K 问题<a class="headerlink" href="#83-top-k" title="Permanent link">&para;</a></h1>
<div class="admonition question"> <div class="admonition question">
<p class="admonition-title">Question</p> <p class="admonition-title">Question</p>
@@ -3731,11 +3763,73 @@
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../build_heap/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 8.2 &amp;nbsp; 建堆操作"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
8.2 &nbsp; 建堆操作
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 8.4 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3801,48 +3895,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../build_heap/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.2 &amp;nbsp; 建堆操作" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8.2 &nbsp; 建堆操作 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.4 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3853,6 +3930,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/algorithms_are_everywhere.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/algorithms_are_everywhere.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="11">1.1 &nbsp; 算法无处不在<a class="headerlink" href="#11" title="Permanent link">&para;</a></h1> <h1 id="11">1.1 &nbsp; 算法无处不在<a class="headerlink" href="#11" title="Permanent link">&para;</a></h1>
<p>当我们听到“算法”这个词时,很自然地会想到数学。然而实际上,许多算法并不涉及复杂数学,而是更多地依赖于基本逻辑,这些逻辑在我们的日常生活中处处可见。</p> <p>当我们听到“算法”这个词时,很自然地会想到数学。然而实际上,许多算法并不涉及复杂数学,而是更多地依赖于基本逻辑,这些逻辑在我们的日常生活中处处可见。</p>
<p>在正式探讨算法之前,有一个有趣的事实值得分享:<strong>你已经在不知不觉中学会了许多算法,并习惯将它们应用到日常生活中了</strong>。下面,我将举几个具体例子来证实这一点。</p> <p>在正式探讨算法之前,有一个有趣的事实值得分享:<strong>你已经在不知不觉中学会了许多算法,并习惯将它们应用到日常生活中了</strong>。下面,我将举几个具体例子来证实这一点。</p>
@@ -3434,11 +3466,73 @@
<p>如果你对数据结构、算法、数组和二分查找等概念仍感到一知半解,请继续往下阅读,这本书将引导你迈入数据结构与算法的知识殿堂。</p> <p>如果你对数据结构、算法、数组和二分查找等概念仍感到一知半解,请继续往下阅读,这本书将引导你迈入数据结构与算法的知识殿堂。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 1 章 &amp;nbsp; 初识算法"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 1 章 &nbsp; 初识算法
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../what_is_dsa/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 1.2 &amp;nbsp; 算法是什么"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
1.2 &nbsp; 算法是什么
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3504,48 +3598,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 1 章 &amp;nbsp; 初识算法" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 1 章 &nbsp; 初识算法 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../what_is_dsa/" class="md-footer__link md-footer__link--next" aria-label="下一页: 1.2 &amp;nbsp; 算法是什么" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
1.2 &nbsp; 算法是什么
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3556,6 +3633,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="1">第 1 章 &nbsp; 初识算法<a class="headerlink" href="#1" title="Permanent link">&para;</a></h1> <h1 id="1">第 1 章 &nbsp; 初识算法<a class="headerlink" href="#1" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="初识算法" src="../assets/covers/chapter_introduction.jpg" width="600" /></p> <p><img alt="初识算法" src="../assets/covers/chapter_introduction.jpg" width="600" /></p>
@@ -3396,11 +3428,73 @@
<li><a href="https://www.hello-algo.com/chapter_introduction/summary/">1.3 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_introduction/summary/">1.3 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_preface/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 0.3 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
0.3 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="algorithms_are_everywhere/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 1.1 &amp;nbsp; 算法无处不在"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
1.1 &nbsp; 算法无处不在
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3466,48 +3560,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_preface/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 0.3 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0.3 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="algorithms_are_everywhere/" class="md-footer__link md-footer__link--next" aria-label="下一页: 1.1 &amp;nbsp; 算法无处不在" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
1.1 &nbsp; 算法无处不在
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3518,6 +3595,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="13">1.3 &nbsp; 小结<a class="headerlink" href="#13" title="Permanent link">&para;</a></h1> <h1 id="13">1.3 &nbsp; 小结<a class="headerlink" href="#13" title="Permanent link">&para;</a></h1>
<ul> <ul>
<li>算法在日常生活中无处不在,并不是遥不可及的高深知识。实际上,我们已经在不知不觉中学会了许多算法,用以解决生活中的大小问题。</li> <li>算法在日常生活中无处不在,并不是遥不可及的高深知识。实际上,我们已经在不知不觉中学会了许多算法,用以解决生活中的大小问题。</li>
@@ -3386,11 +3418,73 @@
<li>我们可以将数据结构与算法类比为拼装积木,积木代表数据,积木的形状和连接方式代表数据结构,拼装积木的步骤则对应算法。</li> <li>我们可以将数据结构与算法类比为拼装积木,积木代表数据,积木的形状和连接方式代表数据结构,拼装积木的步骤则对应算法。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../what_is_dsa/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 1.2 &amp;nbsp; 算法是什么"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
1.2 &nbsp; 算法是什么
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_computational_complexity/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 2 章 &amp;nbsp; 复杂度分析"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 2 章 &nbsp; 复杂度分析
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3456,48 +3550,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../what_is_dsa/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 1.2 &amp;nbsp; 算法是什么" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.2 &nbsp; 算法是什么 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_computational_complexity/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 2 章 &amp;nbsp; 复杂度分析" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 2 章 &nbsp; 复杂度分析
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3508,6 +3585,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/what_is_dsa.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_introduction/what_is_dsa.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="12">1.2 &nbsp; 算法是什么<a class="headerlink" href="#12" title="Permanent link">&para;</a></h1> <h1 id="12">1.2 &nbsp; 算法是什么<a class="headerlink" href="#12" title="Permanent link">&para;</a></h1>
<h2 id="121">1.2.1 &nbsp; 算法定义<a class="headerlink" href="#121" title="Permanent link">&para;</a></h2> <h2 id="121">1.2.1 &nbsp; 算法定义<a class="headerlink" href="#121" title="Permanent link">&para;</a></h2>
<p>「算法 algorithm」是在有限时间内解决特定问题的一组指令或操作步骤,它具有以下特性。</p> <p>「算法 algorithm」是在有限时间内解决特定问题的一组指令或操作步骤,它具有以下特性。</p>
@@ -3526,11 +3558,73 @@
<p>在实际讨论时,我们通常会将“数据结构与算法”简称为“算法”。比如众所周知的 LeetCode 算法题目,实际上同时考察了数据结构和算法两方面的知识。</p> <p>在实际讨论时,我们通常会将“数据结构与算法”简称为“算法”。比如众所周知的 LeetCode 算法题目,实际上同时考察了数据结构和算法两方面的知识。</p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../algorithms_are_everywhere/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 1.1 &amp;nbsp; 算法无处不在"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
1.1 &nbsp; 算法无处不在
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 1.3 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
1.3 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3596,48 +3690,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../algorithms_are_everywhere/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 1.1 &amp;nbsp; 算法无处不在" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.1 &nbsp; 算法无处不在 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 1.3 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
1.3 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3648,6 +3725,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/about_the_book.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/about_the_book.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="01">0.1 &nbsp; 关于本书<a class="headerlink" href="#01" title="Permanent link">&para;</a></h1> <h1 id="01">0.1 &nbsp; 关于本书<a class="headerlink" href="#01" title="Permanent link">&para;</a></h1>
<p>本项目旨在创建一本开源免费、新手友好的数据结构与算法入门教程。</p> <p>本项目旨在创建一本开源免费、新手友好的数据结构与算法入门教程。</p>
<ul> <ul>
@@ -3493,11 +3525,73 @@
<p>本书倡导手脑并用的学习方式,在这一点上深受<a href="https://github.com/d2l-ai/d2l-zh">《动手学深度学习》</a>的启发。在此向各位读者强烈推荐这本优秀著作。</p> <p>本书倡导手脑并用的学习方式,在这一点上深受<a href="https://github.com/d2l-ai/d2l-zh">《动手学深度学习》</a>的启发。在此向各位读者强烈推荐这本优秀著作。</p>
<p>衷心感谢我的父母,正是你们一直以来的支持与鼓励,让我有机会做这件富有趣味的事。</p> <p>衷心感谢我的父母,正是你们一直以来的支持与鼓励,让我有机会做这件富有趣味的事。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 0 章 &amp;nbsp; 前言"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 0 章 &nbsp; 前言
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../suggestions/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 0.2 &amp;nbsp; 如何使用本书"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
0.2 &nbsp; 如何使用本书
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3563,48 +3657,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 0 章 &amp;nbsp; 前言" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 0 章 &nbsp; 前言 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../suggestions/" class="md-footer__link md-footer__link--next" aria-label="下一页: 0.2 &amp;nbsp; 如何使用本书" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
0.2 &nbsp; 如何使用本书
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3615,6 +3692,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+102 -28
View File
@@ -3367,7 +3367,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3378,6 +3403,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="0">第 0 章 &nbsp; 前言<a class="headerlink" href="#0" title="Permanent link">&para;</a></h1> <h1 id="0">第 0 章 &nbsp; 前言<a class="headerlink" href="#0" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="前言" src="../assets/covers/chapter_preface.jpg" width="600" /></p> <p><img alt="前言" src="../assets/covers/chapter_preface.jpg" width="600" /></p>
@@ -3394,11 +3426,52 @@
<li><a href="https://www.hello-algo.com/chapter_preface/summary/">0.3 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_preface/summary/">0.3 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<!-- Link to next page -->
<a
href="about_the_book/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 0.1 &amp;nbsp; 关于本书"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
0.1 &nbsp; 关于本书
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3464,32 +3537,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
<a href="about_the_book/" class="md-footer__link md-footer__link--next" aria-label="下一页: 0.1 &amp;nbsp; 关于本书" rel="next"> furnished to do so, subject to the following conditions:
<div class="md-footer__title">
<span class="md-footer__direction"> The above copyright notice and this permission notice shall be included in
下一页 all copies or substantial portions of the Software.
</span>
<div class="md-ellipsis"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0.1 &nbsp; 关于本书 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</div> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
</div> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
<div class="md-footer__button md-icon"> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg> IN THE SOFTWARE.
</div> -->
</a>
<!-- Footer -->
</nav> <footer class="md-footer">
<!-- Further information -->
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3500,6 +3572,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3472,7 +3472,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/suggestions.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/suggestions.md" title="编辑此页" class="md-content__button md-icon">
@@ -3483,6 +3508,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="02">0.2 &nbsp; 如何使用本书<a class="headerlink" href="#02" title="Permanent link">&para;</a></h1> <h1 id="02">0.2 &nbsp; 如何使用本书<a class="headerlink" href="#02" title="Permanent link">&para;</a></h1>
<div class="admonition tip"> <div class="admonition tip">
<p class="admonition-title">Tip</p> <p class="admonition-title">Tip</p>
@@ -3674,11 +3706,73 @@
<p><img alt="算法学习路线" src="../suggestions.assets/learning_route.png" /></p> <p><img alt="算法学习路线" src="../suggestions.assets/learning_route.png" /></p>
<p align="center"> 图 0-7 &nbsp; 算法学习路线 </p> <p align="center"> 图 0-7 &nbsp; 算法学习路线 </p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../about_the_book/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 0.1 &amp;nbsp; 关于本书"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
0.1 &nbsp; 关于本书
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 0.3 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
0.3 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3744,48 +3838,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../about_the_book/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 0.1 &amp;nbsp; 关于本书" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0.1 &nbsp; 关于本书 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 0.3 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
0.3 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3796,6 +3873,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_preface/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="03">0.3 &nbsp; 小结<a class="headerlink" href="#03" title="Permanent link">&para;</a></h1> <h1 id="03">0.3 &nbsp; 小结<a class="headerlink" href="#03" title="Permanent link">&para;</a></h1>
<ul> <ul>
<li>本书的主要受众是算法初学者。如果已有一定基础,本书能帮助您系统回顾算法知识,书内源代码也可作为“刷题工具库”使用。</li> <li>本书的主要受众是算法初学者。如果已有一定基础,本书能帮助您系统回顾算法知识,书内源代码也可作为“刷题工具库”使用。</li>
@@ -3385,11 +3417,73 @@
<li>本书网页版的每个章节都设有讨论区,欢迎随时分享你的疑惑与见解。</li> <li>本书网页版的每个章节都设有讨论区,欢迎随时分享你的疑惑与见解。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../suggestions/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 0.2 &amp;nbsp; 如何使用本书"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
0.2 &nbsp; 如何使用本书
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_introduction/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 1 章 &amp;nbsp; 初识算法"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 1 章 &nbsp; 初识算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3455,48 +3549,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../suggestions/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 0.2 &amp;nbsp; 如何使用本书" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0.2 &nbsp; 如何使用本书 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_introduction/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 1 章 &amp;nbsp; 初识算法" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 1 章 &nbsp; 初识算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3507,6 +3584,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+101 -27
View File
@@ -3352,7 +3352,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_reference/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_reference/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3363,6 +3388,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="_1">参考文献<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h1> <h1 id="_1">参考文献<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h1>
<p>[1] Thomas H. Cormen, et al. Introduction to Algorithms (3<sup>rd</sup> Edition).</p> <p>[1] Thomas H. Cormen, et al. Introduction to Algorithms (3<sup>rd</sup> Edition).</p>
<p>[2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1<sup>st</sup> Edition).</p> <p>[2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1<sup>st</sup> Edition).</p>
@@ -3374,10 +3406,51 @@
<p>[8] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6<sup>th</sup> Edition). </p> <p>[8] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6<sup>th</sup> Edition). </p>
<p>[9] Aston Zhang, et al. Dive into Deep Learning.</p> <p>[9] Aston Zhang, et al. Dive into Deep Learning.</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_appendix/contribution/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 16.2 &amp;nbsp; 一起参与创作"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
16.2 &nbsp; 一起参与创作
</div>
</div>
</a>
<!-- Link to next page -->
</nav>
<!-- Comment system -->
</article> </article>
</div> </div>
@@ -3395,32 +3468,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_appendix/contribution/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 16.2 &amp;nbsp; 一起参与创作" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16.2 &nbsp; 一起参与创作 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
</nav> <footer class="md-footer">
<!-- Further information -->
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3431,6 +3503,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/binary_search.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/binary_search.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="101">10.1 &nbsp; 二分查找<a class="headerlink" href="#101" title="Permanent link">&para;</a></h1> <h1 id="101">10.1 &nbsp; 二分查找<a class="headerlink" href="#101" title="Permanent link">&para;</a></h1>
<p>「二分查找 binary search」是一种基于分治策略的高效搜索算法。它利用数据的有序性,每轮减少一半搜索范围,直至找到目标元素或搜索区间为空为止。</p> <p>「二分查找 binary search」是一种基于分治策略的高效搜索算法。它利用数据的有序性,每轮减少一半搜索范围,直至找到目标元素或搜索区间为空为止。</p>
<div class="admonition question"> <div class="admonition question">
@@ -4035,11 +4067,73 @@
<li>小数据量下,线性查找性能更佳。在线性查找中,每轮只需要 1 次判断操作;而在二分查找中,需要 1 次加法、1 次除法、1 ~ 3 次判断操作、1 次加法(减法),共 4 ~ 6 个单元操作;因此,当数据量 <span class="arithmatex">\(n\)</span> 较小时,线性查找反而比二分查找更快。</li> <li>小数据量下,线性查找性能更佳。在线性查找中,每轮只需要 1 次判断操作;而在二分查找中,需要 1 次加法、1 次除法、1 ~ 3 次判断操作、1 次加法(减法),共 4 ~ 6 个单元操作;因此,当数据量 <span class="arithmatex">\(n\)</span> 较小时,线性查找反而比二分查找更快。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 10 章 &amp;nbsp; 搜索"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 10 章 &nbsp; 搜索
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../binary_search_insertion/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 10.2 &amp;nbsp; 二分查找插入点"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.2 &nbsp; 二分查找插入点
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4105,48 +4199,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 10 章 &amp;nbsp; 搜索" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 10 章 &nbsp; 搜索 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../binary_search_insertion/" class="md-footer__link md-footer__link--next" aria-label="下一页: 10.2 &amp;nbsp; 二分查找插入点" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.2 &nbsp; 二分查找插入点
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4157,6 +4234,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3478,7 +3478,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/binary_search_edge.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/binary_search_edge.md" title="编辑此页" class="md-content__button md-icon">
@@ -3489,6 +3514,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="103">10.3 &nbsp; 二分查找边界<a class="headerlink" href="#103" title="Permanent link">&para;</a></h1> <h1 id="103">10.3 &nbsp; 二分查找边界<a class="headerlink" href="#103" title="Permanent link">&para;</a></h1>
<h2 id="1031">10.3.1 &nbsp; 查找左边界<a class="headerlink" href="#1031" title="Permanent link">&para;</a></h2> <h2 id="1031">10.3.1 &nbsp; 查找左边界<a class="headerlink" href="#1031" title="Permanent link">&para;</a></h2>
<div class="admonition question"> <div class="admonition question">
@@ -3870,11 +3902,73 @@
<li>因为该方法引入了小数,所以需要将函数中的变量 <code>target</code> 改为浮点数类型。</li> <li>因为该方法引入了小数,所以需要将函数中的变量 <code>target</code> 改为浮点数类型。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../binary_search_insertion/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 10.2 &amp;nbsp; 二分查找插入点"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
10.2 &nbsp; 二分查找插入点
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../replace_linear_by_hashing/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 10.4 &amp;nbsp; 哈希优化策略"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.4 &nbsp; 哈希优化策略
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3940,48 +4034,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../binary_search_insertion/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 10.2 &amp;nbsp; 二分查找插入点" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10.2 &nbsp; 二分查找插入点 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../replace_linear_by_hashing/" class="md-footer__link md-footer__link--next" aria-label="下一页: 10.4 &amp;nbsp; 哈希优化策略" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.4 &nbsp; 哈希优化策略
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3992,6 +4069,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3438,7 +3438,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/binary_search_insertion.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/binary_search_insertion.md" title="编辑此页" class="md-content__button md-icon">
@@ -3449,6 +3474,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="102">10.2 &nbsp; 二分查找插入点<a class="headerlink" href="#102" title="Permanent link">&para;</a></h1> <h1 id="102">10.2 &nbsp; 二分查找插入点<a class="headerlink" href="#102" title="Permanent link">&para;</a></h1>
<p>二分查找不仅可用于搜索目标元素,还具有许多变种问题,比如搜索目标元素的插入位置。</p> <p>二分查找不仅可用于搜索目标元素,还具有许多变种问题,比如搜索目标元素的插入位置。</p>
<h2 id="1021">10.2.1 &nbsp; 无重复元素的情况<a class="headerlink" href="#1021" title="Permanent link">&para;</a></h2> <h2 id="1021">10.2.1 &nbsp; 无重复元素的情况<a class="headerlink" href="#1021" title="Permanent link">&para;</a></h2>
@@ -3970,11 +4002,73 @@
<p>总的来看,二分查找无非就是给指针 <span class="arithmatex">\(i\)</span><span class="arithmatex">\(j\)</span> 分别设定搜索目标,目标可能是一个具体的元素(例如 <code>target</code> ),也可能是一个元素范围(例如小于 <code>target</code> 的元素)。</p> <p>总的来看,二分查找无非就是给指针 <span class="arithmatex">\(i\)</span><span class="arithmatex">\(j\)</span> 分别设定搜索目标,目标可能是一个具体的元素(例如 <code>target</code> ),也可能是一个元素范围(例如小于 <code>target</code> 的元素)。</p>
<p>在不断的循环二分中,指针 <span class="arithmatex">\(i\)</span><span class="arithmatex">\(j\)</span> 都逐渐逼近预先设定的目标。最终,它们或是成功找到答案,或是越过边界后停止。</p> <p>在不断的循环二分中,指针 <span class="arithmatex">\(i\)</span><span class="arithmatex">\(j\)</span> 都逐渐逼近预先设定的目标。最终,它们或是成功找到答案,或是越过边界后停止。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../binary_search/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 10.1 &amp;nbsp; 二分查找"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
10.1 &nbsp; 二分查找
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../binary_search_edge/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 10.3 &amp;nbsp; 二分查找边界"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.3 &nbsp; 二分查找边界
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4040,48 +4134,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../binary_search/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 10.1 &amp;nbsp; 二分查找" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10.1 &nbsp; 二分查找 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../binary_search_edge/" class="md-footer__link md-footer__link--next" aria-label="下一页: 10.3 &amp;nbsp; 二分查找边界" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.3 &nbsp; 二分查找边界
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4092,6 +4169,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="10">第 10 章 &nbsp; 搜索<a class="headerlink" href="#10" title="Permanent link">&para;</a></h1> <h1 id="10">第 10 章 &nbsp; 搜索<a class="headerlink" href="#10" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="搜索" src="../assets/covers/chapter_searching.jpg" width="600" /></p> <p><img alt="搜索" src="../assets/covers/chapter_searching.jpg" width="600" /></p>
@@ -3399,11 +3431,73 @@
<li><a href="https://www.hello-algo.com/chapter_searching/summary/">10.6 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_searching/summary/">10.6 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_graph/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 9.4 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
9.4 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="binary_search/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 10.1 &amp;nbsp; 二分查找"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.1 &nbsp; 二分查找
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3469,48 +3563,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_graph/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 9.4 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9.4 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="binary_search/" class="md-footer__link md-footer__link--next" aria-label="下一页: 10.1 &amp;nbsp; 二分查找" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.1 &nbsp; 二分查找
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3521,6 +3598,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/replace_linear_by_hashing.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/replace_linear_by_hashing.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="104">10.4 &nbsp; 哈希优化策略<a class="headerlink" href="#104" title="Permanent link">&para;</a></h1> <h1 id="104">10.4 &nbsp; 哈希优化策略<a class="headerlink" href="#104" title="Permanent link">&para;</a></h1>
<p>在算法题中,<strong>我们常通过将线性查找替换为哈希查找来降低算法的时间复杂度</strong>。我们借助一个算法题来加深理解。</p> <p>在算法题中,<strong>我们常通过将线性查找替换为哈希查找来降低算法的时间复杂度</strong>。我们借助一个算法题来加深理解。</p>
<div class="admonition question"> <div class="admonition question">
@@ -3901,11 +3933,73 @@
<p>此方法通过哈希查找将时间复杂度从 <span class="arithmatex">\(O(n^2)\)</span> 降低至 <span class="arithmatex">\(O(n)\)</span> ,大幅提升运行效率。</p> <p>此方法通过哈希查找将时间复杂度从 <span class="arithmatex">\(O(n^2)\)</span> 降低至 <span class="arithmatex">\(O(n)\)</span> ,大幅提升运行效率。</p>
<p>由于需要维护一个额外的哈希表,因此空间复杂度为 <span class="arithmatex">\(O(n)\)</span><strong>尽管如此,该方法的整体时空效率更为均衡,因此它是本题的最优解法</strong></p> <p>由于需要维护一个额外的哈希表,因此空间复杂度为 <span class="arithmatex">\(O(n)\)</span><strong>尽管如此,该方法的整体时空效率更为均衡,因此它是本题的最优解法</strong></p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../binary_search_edge/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 10.3 &amp;nbsp; 二分查找边界"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
10.3 &nbsp; 二分查找边界
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../searching_algorithm_revisited/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 10.5 &amp;nbsp; 重识搜索算法"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.5 &nbsp; 重识搜索算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3971,48 +4065,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../binary_search_edge/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 10.3 &amp;nbsp; 二分查找边界" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10.3 &nbsp; 二分查找边界 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../searching_algorithm_revisited/" class="md-footer__link md-footer__link--next" aria-label="下一页: 10.5 &amp;nbsp; 重识搜索算法" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.5 &nbsp; 重识搜索算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4023,6 +4100,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/searching_algorithm_revisited.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/searching_algorithm_revisited.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="105">10.5 &nbsp; 重识搜索算法<a class="headerlink" href="#105" title="Permanent link">&para;</a></h1> <h1 id="105">10.5 &nbsp; 重识搜索算法<a class="headerlink" href="#105" title="Permanent link">&para;</a></h1>
<p>「搜索算法 searching algorithm」用于在数据结构(例如数组、链表、树或图)中搜索一个或一组满足特定条件的元素。</p> <p>「搜索算法 searching algorithm」用于在数据结构(例如数组、链表、树或图)中搜索一个或一组满足特定条件的元素。</p>
<p>搜索算法可根据实现思路分为以下两类。</p> <p>搜索算法可根据实现思路分为以下两类。</p>
@@ -3577,11 +3609,73 @@
<li>若使用 AVL 树或红黑树,则各项操作可在 <span class="arithmatex">\(O(\log n)\)</span> 效率下稳定运行,但维护树平衡的操作会增加额外开销。</li> <li>若使用 AVL 树或红黑树,则各项操作可在 <span class="arithmatex">\(O(\log n)\)</span> 效率下稳定运行,但维护树平衡的操作会增加额外开销。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../replace_linear_by_hashing/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 10.4 &amp;nbsp; 哈希优化策略"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
10.4 &nbsp; 哈希优化策略
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 10.6 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.6 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3647,48 +3741,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../replace_linear_by_hashing/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 10.4 &amp;nbsp; 哈希优化策略" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10.4 &nbsp; 哈希优化策略 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 10.6 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
10.6 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3699,6 +3776,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3364,7 +3364,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_searching/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3375,6 +3400,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="106">10.6 &nbsp; 小结<a class="headerlink" href="#106" title="Permanent link">&para;</a></h1> <h1 id="106">10.6 &nbsp; 小结<a class="headerlink" href="#106" title="Permanent link">&para;</a></h1>
<ul> <ul>
<li>二分查找依赖于数据的有序性,通过循环逐步缩减一半搜索区间来实现查找。它要求输入数据有序,且仅适用于数组或基于数组实现的数据结构。</li> <li>二分查找依赖于数据的有序性,通过循环逐步缩减一半搜索区间来实现查找。它要求输入数据有序,且仅适用于数组或基于数组实现的数据结构。</li>
@@ -3385,11 +3417,73 @@
<li>用哈希查找替换线性查找是一种常用的优化运行时间的策略,可将时间复杂度从 <span class="arithmatex">\(O(n)\)</span> 降低至 <span class="arithmatex">\(O(1)\)</span></li> <li>用哈希查找替换线性查找是一种常用的优化运行时间的策略,可将时间复杂度从 <span class="arithmatex">\(O(n)\)</span> 降低至 <span class="arithmatex">\(O(1)\)</span></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../searching_algorithm_revisited/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 10.5 &amp;nbsp; 重识搜索算法"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
10.5 &nbsp; 重识搜索算法
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_sorting/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 11 章 &amp;nbsp; 排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 11 章 &nbsp; 排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3455,48 +3549,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../searching_algorithm_revisited/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 10.5 &amp;nbsp; 重识搜索算法" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10.5 &nbsp; 重识搜索算法 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_sorting/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 11 章 &amp;nbsp; 排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 11 章 &nbsp; 排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3507,6 +3584,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/bubble_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/bubble_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="113">11.3 &nbsp; 冒泡排序<a class="headerlink" href="#113" title="Permanent link">&para;</a></h1> <h1 id="113">11.3 &nbsp; 冒泡排序<a class="headerlink" href="#113" title="Permanent link">&para;</a></h1>
<p>「冒泡排序 bubble sort」通过连续地比较与交换相邻元素实现排序。这个过程就像气泡从底部升到顶部一样,因此得名冒泡排序。</p> <p>「冒泡排序 bubble sort」通过连续地比较与交换相邻元素实现排序。这个过程就像气泡从底部升到顶部一样,因此得名冒泡排序。</p>
<p>如图 11-4 所示,冒泡过程可以利用元素交换操作来模拟:从数组最左端开始向右遍历,依次比较相邻元素大小,如果“左元素 &gt; 右元素”就交换它俩。遍历完成后,最大的元素会被移动到数组的最右端。</p> <p>如图 11-4 所示,冒泡过程可以利用元素交换操作来模拟:从数组最左端开始向右遍历,依次比较相邻元素大小,如果“左元素 &gt; 右元素”就交换它俩。遍历完成后,最大的元素会被移动到数组的最右端。</p>
@@ -3975,11 +4007,73 @@
<li><strong>稳定排序</strong>:由于在“冒泡”中遇到相等元素不交换。</li> <li><strong>稳定排序</strong>:由于在“冒泡”中遇到相等元素不交换。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../selection_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.2 &amp;nbsp; 选择排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.2 &nbsp; 选择排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../insertion_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.4 &amp;nbsp; 插入排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.4 &nbsp; 插入排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4045,48 +4139,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../selection_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.2 &amp;nbsp; 选择排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.2 &nbsp; 选择排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../insertion_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.4 &amp;nbsp; 插入排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.4 &nbsp; 插入排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4097,6 +4174,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/bucket_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/bucket_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="118">11.8 &nbsp; 桶排序<a class="headerlink" href="#118" title="Permanent link">&para;</a></h1> <h1 id="118">11.8 &nbsp; 桶排序<a class="headerlink" href="#118" title="Permanent link">&para;</a></h1>
<p>前述的几种排序算法都属于“基于比较的排序算法”,它们通过比较元素间的大小来实现排序。此类排序算法的时间复杂度无法超越 <span class="arithmatex">\(O(n \log n)\)</span> 。接下来,我们将探讨几种“非比较排序算法”,它们的时间复杂度可以达到线性阶。</p> <p>前述的几种排序算法都属于“基于比较的排序算法”,它们通过比较元素间的大小来实现排序。此类排序算法的时间复杂度无法超越 <span class="arithmatex">\(O(n \log n)\)</span> 。接下来,我们将探讨几种“非比较排序算法”,它们的时间复杂度可以达到线性阶。</p>
<p>「桶排序 bucket sort」是分治策略的一个典型应用。它通过设置一些具有大小顺序的桶,每个桶对应一个数据范围,将数据平均分配到各个桶中;然后,在每个桶内部分别执行排序;最终按照桶的顺序将所有数据合并。</p> <p>「桶排序 bucket sort」是分治策略的一个典型应用。它通过设置一些具有大小顺序的桶,每个桶对应一个数据范围,将数据平均分配到各个桶中;然后,在每个桶内部分别执行排序;最终按照桶的顺序将所有数据合并。</p>
@@ -3840,11 +3872,73 @@
<p><img alt="根据概率分布划分桶" src="../bucket_sort.assets/scatter_in_buckets_distribution.png" /></p> <p><img alt="根据概率分布划分桶" src="../bucket_sort.assets/scatter_in_buckets_distribution.png" /></p>
<p align="center"> 图 11-15 &nbsp; 根据概率分布划分桶 </p> <p align="center"> 图 11-15 &nbsp; 根据概率分布划分桶 </p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../heap_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.7 &amp;nbsp; 堆排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.7 &nbsp; 堆排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../counting_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.9 &amp;nbsp; 计数排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.9 &nbsp; 计数排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3910,48 +4004,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../heap_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.7 &amp;nbsp; 堆排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.7 &nbsp; 堆排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../counting_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.9 &amp;nbsp; 计数排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.9 &nbsp; 计数排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3962,6 +4039,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3458,7 +3458,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/counting_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/counting_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3469,6 +3494,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="119">11.9 &nbsp; 计数排序<a class="headerlink" href="#119" title="Permanent link">&para;</a></h1> <h1 id="119">11.9 &nbsp; 计数排序<a class="headerlink" href="#119" title="Permanent link">&para;</a></h1>
<p>「计数排序 counting sort」通过统计元素数量来实现排序,通常应用于整数数组。</p> <p>「计数排序 counting sort」通过统计元素数量来实现排序,通常应用于整数数组。</p>
<h2 id="1191">11.9.1 &nbsp; 简单实现<a class="headerlink" href="#1191" title="Permanent link">&para;</a></h2> <h2 id="1191">11.9.1 &nbsp; 简单实现<a class="headerlink" href="#1191" title="Permanent link">&para;</a></h2>
@@ -4204,11 +4236,73 @@
<p><strong>计数排序只适用于非负整数</strong>。若想要将其用于其他类型的数据,需要确保这些数据可以被转换为非负整数,并且在转换过程中不能改变各个元素之间的相对大小关系。例如,对于包含负数的整数数组,可以先给所有数字加上一个常数,将全部数字转化为正数,排序完成后再转换回去即可。</p> <p><strong>计数排序只适用于非负整数</strong>。若想要将其用于其他类型的数据,需要确保这些数据可以被转换为非负整数,并且在转换过程中不能改变各个元素之间的相对大小关系。例如,对于包含负数的整数数组,可以先给所有数字加上一个常数,将全部数字转化为正数,排序完成后再转换回去即可。</p>
<p><strong>计数排序适用于数据量大但数据范围较小的情况</strong>。比如,在上述示例中 <span class="arithmatex">\(m\)</span> 不能太大,否则会占用过多空间。而当 <span class="arithmatex">\(n \ll m\)</span> 时,计数排序使用 <span class="arithmatex">\(O(m)\)</span> 时间,可能比 <span class="arithmatex">\(O(n \log n)\)</span> 的排序算法还要慢。</p> <p><strong>计数排序适用于数据量大但数据范围较小的情况</strong>。比如,在上述示例中 <span class="arithmatex">\(m\)</span> 不能太大,否则会占用过多空间。而当 <span class="arithmatex">\(n \ll m\)</span> 时,计数排序使用 <span class="arithmatex">\(O(m)\)</span> 时间,可能比 <span class="arithmatex">\(O(n \log n)\)</span> 的排序算法还要慢。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../bucket_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.8 &amp;nbsp; 桶排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.8 &nbsp; 桶排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../radix_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.10 &amp;nbsp; 基数排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.10 &nbsp; 基数排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4274,48 +4368,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../bucket_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.8 &amp;nbsp; 桶排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.8 &nbsp; 桶排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../radix_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.10 &amp;nbsp; 基数排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.10 &nbsp; 基数排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4326,6 +4403,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/heap_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/heap_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="117">11.7 &nbsp; 堆排序<a class="headerlink" href="#117" title="Permanent link">&para;</a></h1> <h1 id="117">11.7 &nbsp; 堆排序<a class="headerlink" href="#117" title="Permanent link">&para;</a></h1>
<div class="admonition tip"> <div class="admonition tip">
<p class="admonition-title">Tip</p> <p class="admonition-title">Tip</p>
@@ -3968,11 +4000,73 @@
<li><strong>非稳定排序</strong>:在交换堆顶元素和堆底元素时,相等元素的相对位置可能发生变化。</li> <li><strong>非稳定排序</strong>:在交换堆顶元素和堆底元素时,相等元素的相对位置可能发生变化。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../merge_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.6 &amp;nbsp; 归并排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.6 &nbsp; 归并排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../bucket_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.8 &amp;nbsp; 桶排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.8 &nbsp; 桶排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4038,48 +4132,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../merge_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.6 &amp;nbsp; 归并排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.6 &nbsp; 归并排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../bucket_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.8 &amp;nbsp; 桶排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.8 &nbsp; 桶排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4090,6 +4167,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="11">第 11 章 &nbsp; 排序<a class="headerlink" href="#11" title="Permanent link">&para;</a></h1> <h1 id="11">第 11 章 &nbsp; 排序<a class="headerlink" href="#11" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="排序" src="../assets/covers/chapter_sorting.jpg" width="600" /></p> <p><img alt="排序" src="../assets/covers/chapter_sorting.jpg" width="600" /></p>
@@ -3404,11 +3436,73 @@
<li><a href="https://www.hello-algo.com/chapter_sorting/summary/">11.11 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_sorting/summary/">11.11 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_searching/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 10.6 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
10.6 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="sorting_algorithm/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.1 &amp;nbsp; 排序算法"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.1 &nbsp; 排序算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3474,48 +3568,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_searching/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 10.6 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10.6 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="sorting_algorithm/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.1 &amp;nbsp; 排序算法" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.1 &nbsp; 排序算法
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3526,6 +3603,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/insertion_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/insertion_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="114">11.4 &nbsp; 插入排序<a class="headerlink" href="#114" title="Permanent link">&para;</a></h1> <h1 id="114">11.4 &nbsp; 插入排序<a class="headerlink" href="#114" title="Permanent link">&para;</a></h1>
<p>「插入排序 insertion sort」是一种简单的排序算法,它的工作原理与手动整理一副牌的过程非常相似。</p> <p>「插入排序 insertion sort」是一种简单的排序算法,它的工作原理与手动整理一副牌的过程非常相似。</p>
<p>具体来说,我们在未排序区间选择一个基准元素,将该元素与其左侧已排序区间的元素逐一比较大小,并将该元素插入到正确的位置。</p> <p>具体来说,我们在未排序区间选择一个基准元素,将该元素与其左侧已排序区间的元素逐一比较大小,并将该元素插入到正确的位置。</p>
@@ -3691,11 +3723,73 @@
<li>选择排序不稳定,无法应用于多级排序。</li> <li>选择排序不稳定,无法应用于多级排序。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../bubble_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.3 &amp;nbsp; 冒泡排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.3 &nbsp; 冒泡排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../quick_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.5 &amp;nbsp; 快速排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.5 &nbsp; 快速排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3761,48 +3855,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../bubble_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.3 &amp;nbsp; 冒泡排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.3 &nbsp; 冒泡排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../quick_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.5 &amp;nbsp; 快速排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.5 &nbsp; 快速排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3813,6 +3890,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/merge_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/merge_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="116">11.6 &nbsp; 归并排序<a class="headerlink" href="#116" title="Permanent link">&para;</a></h1> <h1 id="116">11.6 &nbsp; 归并排序<a class="headerlink" href="#116" title="Permanent link">&para;</a></h1>
<p>「归并排序 merge sort」是一种基于分治策略的排序算法,包含图 11-10 所示的“划分”和“合并”阶段。</p> <p>「归并排序 merge sort」是一种基于分治策略的排序算法,包含图 11-10 所示的“划分”和“合并”阶段。</p>
<ol> <ol>
@@ -4072,11 +4104,73 @@
</ul> </ul>
<p>具体实现细节比较复杂,有兴趣的同学可以查阅相关资料进行学习。</p> <p>具体实现细节比较复杂,有兴趣的同学可以查阅相关资料进行学习。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../quick_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.5 &amp;nbsp; 快速排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.5 &nbsp; 快速排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../heap_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.7 &amp;nbsp; 堆排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.7 &nbsp; 堆排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4142,48 +4236,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../quick_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.5 &amp;nbsp; 快速排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.5 &nbsp; 快速排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../heap_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.7 &amp;nbsp; 堆排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.7 &nbsp; 堆排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4194,6 +4271,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3472,7 +3472,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/quick_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/quick_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3483,6 +3508,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="115">11.5 &nbsp; 快速排序<a class="headerlink" href="#115" title="Permanent link">&para;</a></h1> <h1 id="115">11.5 &nbsp; 快速排序<a class="headerlink" href="#115" title="Permanent link">&para;</a></h1>
<p>「快速排序 quick sort」是一种基于分治策略的排序算法,运行高效,应用广泛。</p> <p>「快速排序 quick sort」是一种基于分治策略的排序算法,运行高效,应用广泛。</p>
<p>快速排序的核心操作是“哨兵划分”,其目标是:选择数组中的某个元素作为“基准数”,将所有小于基准数的元素移到其左侧,而大于基准数的元素移到其右侧。具体来说,哨兵划分的流程如图 11-8 所示。</p> <p>快速排序的核心操作是“哨兵划分”,其目标是:选择数组中的某个元素作为“基准数”,将所有小于基准数的元素移到其左侧,而大于基准数的元素移到其右侧。具体来说,哨兵划分的流程如图 11-8 所示。</p>
@@ -4707,11 +4739,73 @@
</div> </div>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../insertion_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.4 &amp;nbsp; 插入排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.4 &nbsp; 插入排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../merge_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.6 &amp;nbsp; 归并排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.6 &nbsp; 归并排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4777,48 +4871,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../insertion_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.4 &amp;nbsp; 插入排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.4 &nbsp; 插入排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../merge_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.6 &amp;nbsp; 归并排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.6 &nbsp; 归并排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4829,6 +4906,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/radix_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/radix_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="1110">11.10 &nbsp; 基数排序<a class="headerlink" href="#1110" title="Permanent link">&para;</a></h1> <h1 id="1110">11.10 &nbsp; 基数排序<a class="headerlink" href="#1110" title="Permanent link">&para;</a></h1>
<p>上一节我们介绍了计数排序,它适用于数据量 <span class="arithmatex">\(n\)</span> 较大但数据范围 <span class="arithmatex">\(m\)</span> 较小的情况。假设我们需要对 <span class="arithmatex">\(n = 10^6\)</span> 个学号进行排序,而学号是一个 <span class="arithmatex">\(8\)</span> 位数字,这意味着数据范围 <span class="arithmatex">\(m = 10^8\)</span> 非常大,使用计数排序需要分配大量内存空间,而基数排序可以避免这种情况。</p> <p>上一节我们介绍了计数排序,它适用于数据量 <span class="arithmatex">\(n\)</span> 较大但数据范围 <span class="arithmatex">\(m\)</span> 较小的情况。假设我们需要对 <span class="arithmatex">\(n = 10^6\)</span> 个学号进行排序,而学号是一个 <span class="arithmatex">\(8\)</span> 位数字,这意味着数据范围 <span class="arithmatex">\(m = 10^8\)</span> 非常大,使用计数排序需要分配大量内存空间,而基数排序可以避免这种情况。</p>
<p>「基数排序 radix sort」的核心思想与计数排序一致,也通过统计个数来实现排序。在此基础上,基数排序利用数字各位之间的递进关系,依次对每一位进行排序,从而得到最终的排序结果。</p> <p>「基数排序 radix sort」的核心思想与计数排序一致,也通过统计个数来实现排序。在此基础上,基数排序利用数字各位之间的递进关系,依次对每一位进行排序,从而得到最终的排序结果。</p>
@@ -4106,11 +4138,73 @@ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d
<li><strong>稳定排序</strong>:与计数排序相同。</li> <li><strong>稳定排序</strong>:与计数排序相同。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../counting_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.9 &amp;nbsp; 计数排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.9 &nbsp; 计数排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.11 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.11 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4176,48 +4270,31 @@ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../counting_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.9 &amp;nbsp; 计数排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.9 &nbsp; 计数排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.11 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.11 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4228,6 +4305,8 @@ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3416,7 +3416,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/selection_sort.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/selection_sort.md" title="编辑此页" class="md-content__button md-icon">
@@ -3427,6 +3452,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="112">11.2 &nbsp; 选择排序<a class="headerlink" href="#112" title="Permanent link">&para;</a></h1> <h1 id="112">11.2 &nbsp; 选择排序<a class="headerlink" href="#112" title="Permanent link">&para;</a></h1>
<p>「选择排序 selection sort」的工作原理非常直接:开启一个循环,每轮从未排序区间选择最小的元素,将其放到已排序区间的末尾。</p> <p>「选择排序 selection sort」的工作原理非常直接:开启一个循环,每轮从未排序区间选择最小的元素,将其放到已排序区间的末尾。</p>
<p>设数组的长度为 <span class="arithmatex">\(n\)</span> ,选择排序的算法流程如图 11-2 所示。</p> <p>设数组的长度为 <span class="arithmatex">\(n\)</span> ,选择排序的算法流程如图 11-2 所示。</p>
@@ -3699,11 +3731,73 @@
<p><img alt="选择排序非稳定示例" src="../selection_sort.assets/selection_sort_instability.png" /></p> <p><img alt="选择排序非稳定示例" src="../selection_sort.assets/selection_sort_instability.png" /></p>
<p align="center"> 图 11-3 &nbsp; 选择排序非稳定示例 </p> <p align="center"> 图 11-3 &nbsp; 选择排序非稳定示例 </p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../sorting_algorithm/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.1 &amp;nbsp; 排序算法"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.1 &nbsp; 排序算法
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../bubble_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.3 &amp;nbsp; 冒泡排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.3 &nbsp; 冒泡排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3769,48 +3863,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../sorting_algorithm/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.1 &amp;nbsp; 排序算法" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.1 &nbsp; 排序算法 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../bubble_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.3 &amp;nbsp; 冒泡排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.3 &nbsp; 冒泡排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3821,6 +3898,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/sorting_algorithm.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/sorting_algorithm.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="111">11.1 &nbsp; 排序算法<a class="headerlink" href="#111" title="Permanent link">&para;</a></h1> <h1 id="111">11.1 &nbsp; 排序算法<a class="headerlink" href="#111" title="Permanent link">&para;</a></h1>
<p>「排序算法 sorting algorithm」用于对一组数据按照特定顺序进行排列。排序算法有着广泛的应用,因为有序数据通常能够被更有效地查找、分析和处理。</p> <p>「排序算法 sorting algorithm」用于对一组数据按照特定顺序进行排列。排序算法有着广泛的应用,因为有序数据通常能够被更有效地查找、分析和处理。</p>
<p>如图 11-1 所示,排序算法中的数据类型可以是整数、浮点数、字符或字符串等。排序的判断规则可根据需求设定,如数字大小、字符 ASCII 码顺序或自定义规则。</p> <p>如图 11-1 所示,排序算法中的数据类型可以是整数、浮点数、字符或字符串等。排序的判断规则可根据需求设定,如数字大小、字符 ASCII 码顺序或自定义规则。</p>
@@ -3476,11 +3508,73 @@
<p><strong>运行快、原地、稳定、正向自适应、通用性好</strong>。显然,迄今为止尚未发现兼具以上所有特性的排序算法。因此,在选择排序算法时,需要根据具体的数据特点和问题需求来决定。</p> <p><strong>运行快、原地、稳定、正向自适应、通用性好</strong>。显然,迄今为止尚未发现兼具以上所有特性的排序算法。因此,在选择排序算法时,需要根据具体的数据特点和问题需求来决定。</p>
<p>接下来,我们将共同学习各种排序算法,并基于上述评价维度对各个排序算法的优缺点进行分析。</p> <p>接下来,我们将共同学习各种排序算法,并基于上述评价维度对各个排序算法的优缺点进行分析。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 11 章 &amp;nbsp; 排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 11 章 &nbsp; 排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../selection_sort/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 11.2 &amp;nbsp; 选择排序"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.2 &nbsp; 选择排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3546,48 +3640,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 11 章 &amp;nbsp; 排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 11 章 &nbsp; 排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../selection_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.2 &amp;nbsp; 选择排序" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
11.2 &nbsp; 选择排序
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3598,6 +3675,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_sorting/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="1111">11.11 &nbsp; 小结<a class="headerlink" href="#1111" title="Permanent link">&para;</a></h1> <h1 id="1111">11.11 &nbsp; 小结<a class="headerlink" href="#1111" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3485,11 +3517,73 @@
<p>最差情况下,所有元素被分至同一个桶中。如果我们采用一个 <span class="arithmatex">\(O(n^2)\)</span> 算法来排序这些元素,则时间复杂度为 <span class="arithmatex">\(O(n^2)\)</span></p> <p>最差情况下,所有元素被分至同一个桶中。如果我们采用一个 <span class="arithmatex">\(O(n^2)\)</span> 算法来排序这些元素,则时间复杂度为 <span class="arithmatex">\(O(n^2)\)</span></p>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../radix_sort/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 11.10 &amp;nbsp; 基数排序"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
11.10 &nbsp; 基数排序
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_divide_and_conquer/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 12 章 &amp;nbsp; 分治"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 12 章 &nbsp; 分治
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3555,48 +3649,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../radix_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.10 &amp;nbsp; 基数排序" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11.10 &nbsp; 基数排序 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_divide_and_conquer/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 12 章 &amp;nbsp; 分治" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 12 章 &nbsp; 分治
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3607,6 +3684,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3484,7 +3484,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/deque.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/deque.md" title="编辑此页" class="md-content__button md-icon">
@@ -3495,6 +3520,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="53">5.3 &nbsp; 双向队列<a class="headerlink" href="#53" title="Permanent link">&para;</a></h1> <h1 id="53">5.3 &nbsp; 双向队列<a class="headerlink" href="#53" title="Permanent link">&para;</a></h1>
<p>在队列中,我们仅能在头部删除或在尾部添加元素。如图 5-7 所示,「双向队列 deque」提供了更高的灵活性,允许在头部和尾部执行元素的添加或删除操作。</p> <p>在队列中,我们仅能在头部删除或在尾部添加元素。如图 5-7 所示,「双向队列 deque」提供了更高的灵活性,允许在头部和尾部执行元素的添加或删除操作。</p>
<p><img alt="双向队列的操作" src="../deque.assets/deque_operations.png" /></p> <p><img alt="双向队列的操作" src="../deque.assets/deque_operations.png" /></p>
@@ -6701,11 +6733,73 @@
<p>双向队列兼具栈与队列的逻辑,<strong>因此它可以实现这两者的所有应用场景,同时提供更高的自由度</strong></p> <p>双向队列兼具栈与队列的逻辑,<strong>因此它可以实现这两者的所有应用场景,同时提供更高的自由度</strong></p>
<p>我们知道,软件的“撤销”功能通常使用栈来实现:系统将每次更改操作 <code>push</code> 到栈中,然后通过 <code>pop</code> 实现撤销。然而,考虑到系统资源的限制,软件通常会限制撤销的步数(例如仅允许保存 <span class="arithmatex">\(50\)</span> 步)。当栈的长度超过 <span class="arithmatex">\(50\)</span> 时,软件需要在栈底(即队首)执行删除操作。<strong>但栈无法实现该功能,此时就需要使用双向队列来替代栈</strong>。请注意,“撤销”的核心逻辑仍然遵循栈的先入后出原则,只是双向队列能够更加灵活地实现一些额外逻辑。</p> <p>我们知道,软件的“撤销”功能通常使用栈来实现:系统将每次更改操作 <code>push</code> 到栈中,然后通过 <code>pop</code> 实现撤销。然而,考虑到系统资源的限制,软件通常会限制撤销的步数(例如仅允许保存 <span class="arithmatex">\(50\)</span> 步)。当栈的长度超过 <span class="arithmatex">\(50\)</span> 时,软件需要在栈底(即队首)执行删除操作。<strong>但栈无法实现该功能,此时就需要使用双向队列来替代栈</strong>。请注意,“撤销”的核心逻辑仍然遵循栈的先入后出原则,只是双向队列能够更加灵活地实现一些额外逻辑。</p>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../queue/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 5.2 &amp;nbsp; 队列"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
5.2 &nbsp; 队列
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 5.4 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -6771,48 +6865,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../queue/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 5.2 &amp;nbsp; 队列" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5.2 &nbsp; 队列 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 5.4 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -6823,6 +6900,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3369,7 +3369,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/index.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/index.md" title="编辑此页" class="md-content__button md-icon">
@@ -3380,6 +3405,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="5">第 5 章 &nbsp; 栈与队列<a class="headerlink" href="#5" title="Permanent link">&para;</a></h1> <h1 id="5">第 5 章 &nbsp; 栈与队列<a class="headerlink" href="#5" title="Permanent link">&para;</a></h1>
<div class="center-table"> <div class="center-table">
<p><img alt="栈与队列" src="../assets/covers/chapter_stack_and_queue.jpg" width="600" /></p> <p><img alt="栈与队列" src="../assets/covers/chapter_stack_and_queue.jpg" width="600" /></p>
@@ -3397,11 +3429,73 @@
<li><a href="https://www.hello-algo.com/chapter_stack_and_queue/summary/">5.4 &nbsp; 小结</a></li> <li><a href="https://www.hello-algo.com/chapter_stack_and_queue/summary/">5.4 &nbsp; 小结</a></li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../chapter_array_and_linkedlist/summary/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 4.4 &amp;nbsp; 小结"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
4.4 &nbsp; 小结
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="stack/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 5.1 &amp;nbsp; 栈"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.1 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3467,48 +3561,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../chapter_array_and_linkedlist/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 4.4 &amp;nbsp; 小结" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4.4 &nbsp; 小结 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="stack/" class="md-footer__link md-footer__link--next" aria-label="下一页: 5.1 &amp;nbsp; 栈" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.1 &nbsp;
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3519,6 +3596,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+124 -45
View File
@@ -3484,7 +3484,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/queue.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/queue.md" title="编辑此页" class="md-content__button md-icon">
@@ -3495,6 +3520,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="52">5.2 &nbsp; 队列<a class="headerlink" href="#52" title="Permanent link">&para;</a></h1> <h1 id="52">5.2 &nbsp; 队列<a class="headerlink" href="#52" title="Permanent link">&para;</a></h1>
<p>「队列 queue」是一种遵循先入先出规则的线性数据结构。顾名思义,队列模拟了排队现象,即新来的人不断加入队列的尾部,而位于队列头部的人逐个离开。</p> <p>「队列 queue」是一种遵循先入先出规则的线性数据结构。顾名思义,队列模拟了排队现象,即新来的人不断加入队列的尾部,而位于队列头部的人逐个离开。</p>
<p>如图 5-4 所示,我们将队列的头部称为“队首”,尾部称为“队尾”,将把元素加入队尾的操作称为“入队”,删除队首元素的操作称为“出队”。</p> <p>如图 5-4 所示,我们将队列的头部称为“队首”,尾部称为“队尾”,将把元素加入队尾的操作称为“入队”,删除队首元素的操作称为“出队”。</p>
@@ -4570,7 +4602,7 @@
<a id="__codelineno-22-75" name="__codelineno-22-75" href="#__codelineno-22-75"></a><span class="w"> </span><span class="c1">// 拷贝链表中的数据到数组</span> <a id="__codelineno-22-75" name="__codelineno-22-75" href="#__codelineno-22-75"></a><span class="w"> </span><span class="c1">// 拷贝链表中的数据到数组</span>
<a id="__codelineno-22-76" name="__codelineno-22-76" href="#__codelineno-22-76"></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-22-76" name="__codelineno-22-76" href="#__codelineno-22-76"></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-22-77" name="__codelineno-22-77" href="#__codelineno-22-77"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="o">*</span><span class="n">node</span><span class="p">;</span> <a id="__codelineno-22-77" name="__codelineno-22-77" href="#__codelineno-22-77"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="o">*</span><span class="n">node</span><span class="p">;</span>
<a id="__codelineno-22-78" name="__codelineno-22-78" href="#__codelineno-22-78"></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">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">queue</span><span class="o">-&gt;</span><span class="n">front</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">queue</span><span class="o">-&gt;</span><span class="n">queSize</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">queue</span><span class="o">-&gt;</span><span class="n">front</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">queue</span><span class="o">-&gt;</span><span class="n">rear</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-22-78" name="__codelineno-22-78" href="#__codelineno-22-78"></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">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">queue</span><span class="o">-&gt;</span><span class="n">front</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">queue</span><span class="o">-&gt;</span><span class="n">queSize</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-22-79" name="__codelineno-22-79" href="#__codelineno-22-79"></a><span class="w"> </span><span class="n">arr</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">node</span><span class="o">-&gt;</span><span class="n">val</span><span class="p">;</span> <a id="__codelineno-22-79" name="__codelineno-22-79" href="#__codelineno-22-79"></a><span class="w"> </span><span class="n">arr</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">node</span><span class="o">-&gt;</span><span class="n">val</span><span class="p">;</span>
<a id="__codelineno-22-80" name="__codelineno-22-80" href="#__codelineno-22-80"></a><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">node</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span> <a id="__codelineno-22-80" name="__codelineno-22-80" href="#__codelineno-22-80"></a><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">node</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
<a id="__codelineno-22-81" name="__codelineno-22-81" href="#__codelineno-22-81"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-22-81" name="__codelineno-22-81" href="#__codelineno-22-81"></a><span class="w"> </span><span class="p">}</span>
@@ -5574,11 +5606,73 @@
<li><strong>各类待办事项</strong>。任何需要实现“先来后到”功能的场景,例如打印机的任务队列、餐厅的出餐队列等。队列在这些场景中可以有效地维护处理顺序。</li> <li><strong>各类待办事项</strong>。任何需要实现“先来后到”功能的场景,例如打印机的任务队列、餐厅的出餐队列等。队列在这些场景中可以有效地维护处理顺序。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../stack/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 5.1 &amp;nbsp; 栈"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
5.1 &nbsp;
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../deque/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 5.3 &amp;nbsp; 双向队列"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.3 &nbsp; 双向队列
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5644,48 +5738,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../stack/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 5.1 &amp;nbsp; 栈" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5.1 &nbsp; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../deque/" class="md-footer__link md-footer__link--next" aria-label="下一页: 5.3 &amp;nbsp; 双向队列" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.3 &nbsp; 双向队列
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5696,6 +5773,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3498,7 +3498,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/stack.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/stack.md" title="编辑此页" class="md-content__button md-icon">
@@ -3509,6 +3534,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="51">5.1 &nbsp;<a class="headerlink" href="#51" title="Permanent link">&para;</a></h1> <h1 id="51">5.1 &nbsp;<a class="headerlink" href="#51" title="Permanent link">&para;</a></h1>
<p>「栈 stack」是一种遵循先入后出的逻辑的线性数据结构。</p> <p>「栈 stack」是一种遵循先入后出的逻辑的线性数据结构。</p>
<p>我们可以将栈类比为桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。我们将盘子替换为各种类型的元素(如整数、字符、对象等),就得到了栈数据结构。</p> <p>我们可以将栈类比为桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。我们将盘子替换为各种类型的元素(如整数、字符、对象等),就得到了栈数据结构。</p>
@@ -5170,11 +5202,73 @@
<li><strong>程序内存管理</strong>。每次调用函数时,系统都会在栈顶添加一个栈帧,用于记录函数的上下文信息。在递归函数中,向下递推阶段会不断执行入栈操作,而向上回溯阶段则会执行出栈操作。</li> <li><strong>程序内存管理</strong>。每次调用函数时,系统都会在栈顶添加一个栈帧,用于记录函数的上下文信息。在递归函数中,向下递推阶段会不断执行入栈操作,而向上回溯阶段则会执行出栈操作。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 5 章 &amp;nbsp; 栈与队列"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 5 章 &nbsp; 栈与队列
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../queue/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 5.2 &amp;nbsp; 队列"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.2 &nbsp; 队列
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5240,48 +5334,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 5 章 &amp;nbsp; 栈与队列" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 5 章 &nbsp; 栈与队列 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../queue/" class="md-footer__link md-footer__link--next" aria-label="下一页: 5.2 &amp;nbsp; 队列" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
5.2 &nbsp; 队列
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5292,6 +5369,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3430,7 +3430,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/summary.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_stack_and_queue/summary.md" title="编辑此页" class="md-content__button md-icon">
@@ -3441,6 +3466,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="54">5.4 &nbsp; 小结<a class="headerlink" href="#54" title="Permanent link">&para;</a></h1> <h1 id="54">5.4 &nbsp; 小结<a class="headerlink" href="#54" title="Permanent link">&para;</a></h1>
<h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3> <h3 id="1">1. &nbsp; 重点回顾<a class="headerlink" href="#1" title="Permanent link">&para;</a></h3>
<ul> <ul>
@@ -3473,11 +3505,73 @@
</ol> </ol>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../deque/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 5.3 &amp;nbsp; 双向队列"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
5.3 &nbsp; 双向队列
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../../chapter_hashing/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 第 6 章 &amp;nbsp; 哈希表"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 6 章 &nbsp; 哈希表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -3543,48 +3637,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../deque/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 5.3 &amp;nbsp; 双向队列" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5.3 &nbsp; 双向队列 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../../chapter_hashing/" class="md-footer__link md-footer__link--next" aria-label="下一页: 第 6 章 &amp;nbsp; 哈希表" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
第 6 章 &nbsp; 哈希表
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -3595,6 +3672,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
@@ -3444,7 +3444,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/array_representation_of_tree.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/array_representation_of_tree.md" title="编辑此页" class="md-content__button md-icon">
@@ -3455,6 +3480,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="73">7.3 &nbsp; 二叉树数组表示<a class="headerlink" href="#73" title="Permanent link">&para;</a></h1> <h1 id="73">7.3 &nbsp; 二叉树数组表示<a class="headerlink" href="#73" title="Permanent link">&para;</a></h1>
<p>在链表表示下,二叉树的存储单元为节点 <code>TreeNode</code> ,节点之间通过指针相连接。在上节中,我们学习了在链表表示下的二叉树的各项基本操作。</p> <p>在链表表示下,二叉树的存储单元为节点 <code>TreeNode</code> ,节点之间通过指针相连接。在上节中,我们学习了在链表表示下的二叉树的各项基本操作。</p>
<p>那么,我们能否用数组来表示二叉树呢?答案是肯定的。</p> <p>那么,我们能否用数组来表示二叉树呢?答案是肯定的。</p>
@@ -4567,11 +4599,73 @@
<li>当二叉树中存在大量 <span class="arithmatex">\(\text{None}\)</span> 时,数组中包含的节点数据比重较低,空间利用率较低。</li> <li>当二叉树中存在大量 <span class="arithmatex">\(\text{None}\)</span> 时,数组中包含的节点数据比重较低,空间利用率较低。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../binary_tree_traversal/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 7.2 &amp;nbsp; 二叉树遍历"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
7.2 &nbsp; 二叉树遍历
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../binary_search_tree/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 7.4 &amp;nbsp; 二叉搜索树"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.4 &nbsp; 二叉搜索树
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4637,48 +4731,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../binary_tree_traversal/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 7.2 &amp;nbsp; 二叉树遍历" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7.2 &nbsp; 二叉树遍历 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../binary_search_tree/" class="md-footer__link md-footer__link--next" aria-label="下一页: 7.4 &amp;nbsp; 二叉搜索树" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.4 &nbsp; 二叉搜索树
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4689,6 +4766,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+127 -48
View File
@@ -3634,7 +3634,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/avl_tree.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/avl_tree.md" title="编辑此页" class="md-content__button md-icon">
@@ -3645,6 +3670,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="75-avl">7.5 &nbsp; AVL 树 *<a class="headerlink" href="#75-avl" title="Permanent link">&para;</a></h1> <h1 id="75-avl">7.5 &nbsp; AVL 树 *<a class="headerlink" href="#75-avl" title="Permanent link">&para;</a></h1>
<p>在二叉搜索树章节中,我们提到了在多次插入和删除操作后,二叉搜索树可能退化为链表。这种情况下,所有操作的时间复杂度将从 <span class="arithmatex">\(O(\log n)\)</span> 恶化为 <span class="arithmatex">\(O(n)\)</span></p> <p>在二叉搜索树章节中,我们提到了在多次插入和删除操作后,二叉搜索树可能退化为链表。这种情况下,所有操作的时间复杂度将从 <span class="arithmatex">\(O(\log n)\)</span> 恶化为 <span class="arithmatex">\(O(n)\)</span></p>
<p>如图 7-24 所示,经过两次删除节点操作,这个二叉搜索树便会退化为链表。</p> <p>如图 7-24 所示,经过两次删除节点操作,这个二叉搜索树便会退化为链表。</p>
@@ -3666,10 +3698,10 @@
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">TreeNode</span><span class="p">:</span> <div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">TreeNode</span><span class="p">:</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;AVL 树节点类&quot;&quot;&quot;</span> <a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;AVL 树节点类&quot;&quot;&quot;</span>
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span> <a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span> <a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">height</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 节点高度</span> <a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">height</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 节点高度</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 左子节点引用</span> <a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">:</span> <span class="n">TreeNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 左子节点引用</span>
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 右子节点引用</span> <a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">:</span> <span class="n">TreeNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 右子节点引用</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -5907,11 +5939,73 @@
<li>红黑树在许多应用中比 AVL 树更受欢迎。这是因为红黑树的平衡条件相对宽松,在红黑树中插入与删除节点所需的旋转操作相对较少,其节点增删操作的平均效率更高。</li> <li>红黑树在许多应用中比 AVL 树更受欢迎。这是因为红黑树的平衡条件相对宽松,在红黑树中插入与删除节点所需的旋转操作相对较少,其节点增删操作的平均效率更高。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../binary_search_tree/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 7.4 &amp;nbsp; 二叉搜索树"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
7.4 &nbsp; 二叉搜索树
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 7.6 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.6 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5977,48 +6071,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../binary_search_tree/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 7.4 &amp;nbsp; 二叉搜索树" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7.4 &nbsp; 二叉搜索树 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 7.6 &amp;nbsp; 小结" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.6 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -6029,6 +6106,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+123 -44
View File
@@ -3512,7 +3512,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/binary_search_tree.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/binary_search_tree.md" title="编辑此页" class="md-content__button md-icon">
@@ -3523,6 +3548,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="74">7.4 &nbsp; 二叉搜索树<a class="headerlink" href="#74" title="Permanent link">&para;</a></h1> <h1 id="74">7.4 &nbsp; 二叉搜索树<a class="headerlink" href="#74" title="Permanent link">&para;</a></h1>
<p>如图 7-16 所示,「二叉搜索树 binary search tree」满足以下条件。</p> <p>如图 7-16 所示,「二叉搜索树 binary search tree」满足以下条件。</p>
<ol> <ol>
@@ -4957,11 +4989,73 @@
<li>用于存储数据流,以保持其有序状态。</li> <li>用于存储数据流,以保持其有序状态。</li>
</ul> </ul>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../array_representation_of_tree/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 7.3 &amp;nbsp; 二叉树数组表示"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
7.3 &nbsp; 二叉树数组表示
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../avl_tree/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 7.5 &amp;nbsp; AVL 树 *"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.5 &nbsp; AVL 树 *
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -5027,48 +5121,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../array_representation_of_tree/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 7.3 &amp;nbsp; 二叉树数组表示" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7.3 &nbsp; 二叉树数组表示 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../avl_tree/" class="md-footer__link md-footer__link--next" aria-label="下一页: 7.5 &amp;nbsp; AVL 树 *" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.5 &nbsp; AVL 树 *
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -5079,6 +5156,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">
+126 -47
View File
@@ -3566,7 +3566,32 @@
<div class="md-content" data-md-component="content"> <div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset"> <article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/binary_tree.md" title="编辑此页" class="md-content__button md-icon"> <a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_tree/binary_tree.md" title="编辑此页" class="md-content__button md-icon">
@@ -3577,6 +3602,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="71">7.1 &nbsp; 二叉树<a class="headerlink" href="#71" title="Permanent link">&para;</a></h1> <h1 id="71">7.1 &nbsp; 二叉树<a class="headerlink" href="#71" title="Permanent link">&para;</a></h1>
<p>「二叉树 binary tree」是一种非线性数据结构,代表着祖先与后代之间的派生关系,体现着“一分为二”的分治逻辑。与链表类似,二叉树的基本单元是节点,每个节点包含:值、左子节点引用、右子节点引用。</p> <p>「二叉树 binary tree」是一种非线性数据结构,代表着祖先与后代之间的派生关系,体现着“一分为二”的分治逻辑。与链表类似,二叉树的基本单元是节点,每个节点包含:值、左子节点引用、右子节点引用。</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:12"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Zig</label></div> <div class="tabbed-set tabbed-alternate" data-tabs="1:12"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Zig</label></div>
@@ -3585,9 +3617,9 @@
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">TreeNode</span><span class="p">:</span> <div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">TreeNode</span><span class="p">:</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;二叉树节点类&quot;&quot;&quot;</span> <a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;二叉树节点类&quot;&quot;&quot;</span>
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span> <a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span> <a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">val</span> <span class="c1"># 节点值</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 左子节点引用</span> <a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="p">:</span> <span class="n">TreeNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 左子节点引用</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 右子节点引用</span> <a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="p">:</span> <span class="n">TreeNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># 右子节点引用</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@@ -4095,11 +4127,73 @@
</table> </table>
</div> </div>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2> <!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 第 7 章 &amp;nbsp; 树"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
第 7 章 &nbsp;
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../binary_tree_traversal/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 7.2 &amp;nbsp; 二叉树遍历"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.2 &nbsp; 二叉树遍历
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here --> <!-- Insert generated snippet here -->
<script <script
src="https://giscus.app/client.js" src="https://giscus.app/client.js"
@@ -4165,48 +4259,31 @@
</main> </main>
<footer class="md-footer"> <!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
<nav class="md-footer__inner md-grid" aria-label="页脚" > of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="上一页: 第 7 章 &amp;nbsp; 树" rel="prev"> sell copies of the Software, and to permit persons to whom the Software is
<div class="md-footer__button md-icon"> furnished to do so, subject to the following conditions:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg> The above copyright notice and this permission notice shall be included in
</div> all copies or substantial portions of the Software.
<div class="md-footer__title">
<span class="md-footer__direction"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
上一页 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
</span> FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
<div class="md-ellipsis"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
第 7 章 &nbsp; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
</div> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
</div> IN THE SOFTWARE.
</a> -->
<!-- Footer -->
<footer class="md-footer">
<a href="../binary_tree_traversal/" class="md-footer__link md-footer__link--next" aria-label="下一页: 7.2 &amp;nbsp; 二叉树遍历" rel="next"> <!-- Further information -->
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
7.2 &nbsp; 二叉树遍历
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset"> <div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid"> <div class="md-footer-meta__inner md-grid">
<div class="md-copyright"> <div class="md-copyright">
@@ -4217,6 +4294,8 @@
</div> </div>
<!-- Social links -->
<div class="md-social"> <div class="md-social">

Some files were not shown because too many files have changed in this diff Show More