Many bug fixes and improvements (#1270)

* Add Ruby and Kotlin icons
Add the avatar of @curtishd

* Update README

* Synchronize zh-hant and zh versions.

* Translate the pythontutor blocks to traditional Chinese

* Fix en/mkdocs.yml

* Update the landing page of the en version.

* Fix the Dockerfile

* Refine the en landingpage

* Fix en landing page

* Reset the README.md
This commit is contained in:
Yudong Jin
2024-04-11 20:18:19 +08:00
committed by GitHub
parent 07977184ad
commit b2f0d4603d
192 changed files with 2382 additions and 1196 deletions

View File

@@ -2,18 +2,23 @@ FROM python:3.10.0-alpine
ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install --upgrade pip RUN pip install --upgrade pip
RUN pip install mkdocs-material==9.5.2 mkdocs-glightbox RUN pip install mkdocs-material==9.5.5 mkdocs-glightbox
WORKDIR /hello-algo WORKDIR /hello-algo
COPY overrides ./build/overrides COPY overrides ./build/overrides
COPY docs ./build/docs
COPY docs-en ./build/docs-en
COPY mkdocs.yml ./mkdocs.yml
COPY mkdocs-en.yml ./mkdocs-en.yml
COPY docs ./build/docs
COPY mkdocs.yml mkdocs.yml
RUN mkdocs build -f mkdocs.yml RUN mkdocs build -f mkdocs.yml
RUN mkdocs build -f mkdocs-en.yml
COPY zh-hant/docs ./build/zh-hant/docs
COPY zh-hant/mkdocs.yml ./zh-hant/mkdocs.yml
RUN mkdocs build -f ./zh-hant/mkdocs.yml
COPY en/docs ./build/en/docs
COPY en/mkdocs.yml ./en/mkdocs.yml
RUN mkdocs build -f ./en/mkdocs.yml
WORKDIR /hello-algo/site WORKDIR /hello-algo/site
EXPOSE 8000 EXPOSE 8000

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -111,22 +111,24 @@
<!-- Section: brief introduction --> <!-- Section: brief introduction -->
<section data-md-color-scheme="slate" class="home-div"> <section data-md-color-scheme="slate" class="home-div">
<div class="section-content"> <div class="section-content">
<img src="index.assets/hello_algo_header.png" style="width: 100%; max-width: 39em;" alt="Preview"> <img src="index.assets/hello_algo_header.png" style="width: 100%; max-width: 41.5em;" alt="Preview">
<div class="code-badge"> <div class="code-badge">
<img src="https://img.shields.io/badge/Python-snow?logo=python&logoColor=3776AB" alt=""> <img src="https://img.shields.io/badge/Python-snow?logo=python&logoColor=3776AB" alt="" />
<img src="https://img.shields.io/badge/C%2B%2B-snow?logo=c%2B%2B&logoColor=00599C" alt=""> <img src="https://img.shields.io/badge/Java-snow?logo=coffeescript&logoColor=FC4C02" alt="" />
<img src="https://img.shields.io/badge/Java-snow?logo=coffeescript&logoColor=FC4C02" alt=""> <img src="https://img.shields.io/badge/C%2B%2B-snow?logo=c%2B%2B&logoColor=00599C" alt="" />
<img src="https://img.shields.io/badge/C%23-snow?logo=csharp&logoColor=512BD4" alt=""> <img src="https://img.shields.io/badge/C-snow?logo=c&logoColor=A8B9CC" alt="" />
<img src="https://img.shields.io/badge/Go-snow?logo=go&logoColor=00ADD8" alt=""> <img src="https://img.shields.io/badge/C%23-snow?logo=csharp&logoColor=512BD4" alt="" />
<img src="https://img.shields.io/badge/Swift-snow?logo=swift&logoColor=F05138" alt=""> <img src="https://img.shields.io/badge/JS-snow?logo=javascript&logoColor=E9CE30" alt="" />
<img src="https://img.shields.io/badge/JavaScript-snow?logo=javascript&logoColor=E9CE30" alt=""> <img src="https://img.shields.io/badge/Go-snow?logo=go&logoColor=00ADD8" alt="" />
<img src="https://img.shields.io/badge/TypeScript-snow?logo=typescript&logoColor=3178C6" alt=""> <img src="https://img.shields.io/badge/Swift-snow?logo=swift&logoColor=F05138" alt="" />
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2" alt=""> <img src="https://img.shields.io/badge/Rust-snow?logo=rust&logoColor=000000" alt="" />
<img src="https://img.shields.io/badge/Rust-snow?logo=rust&logoColor=000000" alt=""> <img src="https://img.shields.io/badge/Ruby-snow?logo=ruby&logoColor=CC342D" alt="" />
<img src="https://img.shields.io/badge/C-snow?logo=c&logoColor=A8B9CC" alt=""> <img src="https://img.shields.io/badge/Kotlin-snow?logo=kotlin&logoColor=7F52FF" alt="" />
<img src="https://img.shields.io/badge/Zig-snow?logo=zig&logoColor=F7A41D" alt=""> <img src="https://img.shields.io/badge/TS-snow?logo=typescript&logoColor=3178C6" alt="" />
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2" alt="" />
<img src="https://img.shields.io/badge/Zig-snow?logo=zig&logoColor=F7A41D" alt="" />
</div> </div>
<p style="margin-top: 2em;">500 幅动画图解、12 种编程语言代码、3000 条社区问答,助你快速入门数据结构与算法</p> <p style="margin-top: 2em;">500 幅动画图解、14 种编程语言代码、3000 条社区问答,助你快速入门数据结构与算法</p>
</div> </div>
</section> </section>
@@ -284,6 +286,14 @@
<br><sub>Zig, Rust</sub> <br><sub>Zig, Rust</sub>
</a> </a>
</div> </div>
<div class="profile-cell">
<a href="https://github.com/curtishd">
<img class="profile-img" src="assets/avatar/avatar_curtishd.jpg"
alt="Reviewer: curtishd" />
<br><b>curtishd</b>
<br><sub>Kotlin</sub>
</a>
</div>
<div class="profile-cell"> <div class="profile-cell">
<a href="https://github.com/Gonglja"> <a href="https://github.com/Gonglja">
<img class="profile-img" src="assets/avatar/avatar_Gonglja.jpg" alt="Reviewer: Gonglja" /> <img class="profile-img" src="assets/avatar/avatar_Gonglja.jpg" alt="Reviewer: Gonglja" />
@@ -347,7 +357,7 @@
<!-- contributors --> <!-- contributors -->
<div style="margin: 2em auto;"> <div style="margin: 2em auto;">
<h3>贡献者</h3> <h3>贡献者</h3>
<p>本书在开源社区 140 多位贡献者的共同努力下不断完善,感谢他们付出的时间与精力!</p> <p>本书在开源社区一百多位贡献者的共同努力下不断完善,感谢他们付出的时间与精力!</p>
<a href="https://github.com/krahets/hello-algo/graphs/contributors"> <a href="https://github.com/krahets/hello-algo/graphs/contributors">
<img src="https://contrib.rocks/image?repo=krahets/hello-algo&max=300&columns=16" alt="Contributors" <img src="https://contrib.rocks/image?repo=krahets/hello-algo&max=300&columns=16" alt="Contributors"
style="width: 100%; max-width: 38.5em;"> style="width: 100%; max-width: 38.5em;">

View File

@@ -1,9 +1,5 @@
--- # Hello 算法
comments: false
glightbox: false 动画图解、一键运行的数据结构与算法教程
hide:
- footer [开始阅读](chapter_hello_algo/)
- toc
- edit
- navigation
---

View File

@@ -6,7 +6,7 @@
<p align="center"> <p align="center">
<img style="height: 60px;" src="https://readme-typing-svg.demolab.com?font=Noto+Sans+SC&weight=500&duration=3500&pause=2000&color=21C8B8&center=true&vCenter=true&random=false&width=200&lines=Hello%2C+Algo+!" alt="hello-algo-typing-svg" /> <img style="height: 60px;" src="https://readme-typing-svg.demolab.com?font=Noto+Sans+SC&weight=500&duration=3500&pause=2000&color=21C8B8&center=true&vCenter=true&random=false&width=200&lines=Hello%2C+Algo+!" alt="hello-algo-typing-svg" />
</br> </br>
Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code Data structures and algorithms crash course with animated illustrations and off-the-shelf code
</p> </p>
<p align="center"> <p align="center">
@@ -57,13 +57,13 @@ If you find this book helpful, please give it a Star :star: to support us, thank
> "An easy-to-understand book on data structures and algorithms, which guides readers to learn by minds-on and hands-on. Strongly recommended for algorithm beginners!" > "An easy-to-understand book on data structures and algorithms, which guides readers to learn by minds-on and hands-on. Strongly recommended for algorithm beginners!"
> >
> **—— Junhui Deng, Professor of Computer Science, Tsinghua University** > **—— Junhui Deng, Professor, Department of computer science and technology, Tsinghua University**
> "If I had 'Hello Algo' when I was learning data structures and algorithms, it would have been 10 times easier!" > "If I had 'Hello Algo' when I was learning data structures and algorithms, it would have been 10 times easier!"
> >
> **—— Mu Li, Senior Principal Scientist, Amazon** > **—— Mu Li, Senior Principal Scientist, Amazon**
## Contribution ## Contributing
> [!Important] > [!Important]
> >

367
en/docs/index.html Normal file
View File

@@ -0,0 +1,367 @@
<!--Section: hero -->
<section data-md-color-scheme="slate" data-md-color-primary="grey" class="home-div"
style="height: min(100vh, 120vw); position: relative; margin-top:-2.4rem; padding: 0; overflow: hidden;">
<!-- hero image -->
<img src="../assets/hero/universe_bg.png" class="hero-bg" alt="">
<div class="hero-div">
<img src="../assets/hero/ground.png" alt=""
style="position: absolute; width: auto; height: 26.445%; left: 28.211%; top: 54.145%;">
<img src="../assets/hero/links.png" alt=""
style="position: absolute; width: auto; height: 78.751%; left: 10.545%; top: 7.326%;">
<a href="chapter_introduction/">
<img src="../assets/hero/astronaut.png" alt="" style="height: 46.673%; left: 35.413%; top: 24.343%;">
<span style="left: 52.744%; top: 22.319%;">Encounter with Algorithms</span>
</a>
<a href="chapter_computational_complexity/">
<img src="../assets/hero/chapter_computational_complexity.png" alt=""
style="height: 12.347%; left: 36.267%; top: 37.653%;">
<span style="left: 40.244%; top: 33.919%;">Complexity analysis</span>
</a>
<a href="chapter_array_and_linkedlist/">
<img src="../assets/hero/chapter_array_and_linkedlist.png" alt=""
style="height: 22.242%; left: 73.242%; top: 52.481%;">
<span style="left: 90.897%; top: 76.259%;">Array and linked list</span>
</a>
<a href="chapter_stack_and_queue/">
<img src="../assets/hero/chapter_stack_and_queue.png" alt=""
style="height: 14.302%; left: 62.646%; top: 77.875%;">
<span style="left: 80.071%; top: 88.25%;">Stack and queue</span>
</a>
<a href="chapter_hashing/">
<img src="../assets/hero/chapter_hashing.png" alt="" style="height: 15.266%; left: 63.281%; top: 27.933%;">
<span style="left: 68.862%; top: 46.292%;">Hash table</span>
</a>
<a href="chapter_tree/">
<img src="../assets/hero/chapter_tree.png" alt="" style="height: 19.615%; left: 80.137%; top: 26.678%;">
<span style="left: 96.159%; top: 44.8%;">Tree</span>
</a>
<a href="chapter_heap/">
<img src="../assets/hero/chapter_heap.png" alt="" style="height: 10.566%; left: 77.226%; top: 11.559%;">
<span style="left: 88.103%; top: 15.422%;">Heap</span>
</a>
<a href="chapter_graph/">
<img src="../assets/hero/chapter_graph.png" alt="" style="height: 16.112%; left: 51.854%; top: 5.575%;">
<span style="left: 71.195%; top: 6.503%;">Graph</span>
</a>
<a href="chapter_searching/">
<img src="../assets/hero/chapter_searching.png" alt="" style="height: 15.149%; left: 18.185%; top: 16.404%;">
<span style="left: 14.556%; top: 20.876%;">Searching</span>
</a>
<a href="chapter_sorting/">
<img src="../assets/hero/chapter_sorting.png" alt="" style="height: 9.574%; left: 25.409%; top: 40.747%;">
<span style="left: 28.805%; top: 53.808%;">Sorting</span>
</a>
<a href="chapter_divide_and_conquer/">
<img src="../assets/hero/chapter_divide_and_conquer.png" alt=""
style="height: 18.681%; left: 32.721%; top: 4.816%;">
<span style="left: 29.42%; top: 6.679%;">Divide and conquer</span>
</a>
<a href="chapter_backtracking/">
<img src="../assets/hero/chapter_backtracking.png" alt="" style="height: 17.338%; left: 4.875%; top: 32.925%;">
<span style="left: 4.742%; top: 50.113%;">Backtracking</span>
</a>
<a href="chapter_dynamic_programming/">
<img src="../assets/hero/chapter_dynamic_programming.png" alt=""
style="height: 15.47%; left: 9.406%; top: 57.472%;">
<span style="left: 8.561%; top: 75.351%;">Dynamic programming</span>
</a>
<a href="chapter_greedy/">
<img src="../assets/hero/chapter_greedy.png" alt="" style="height: 14.127%; left: 23.132%; top: 75.803%;">
<span style="left: 21.619%; top: 86.85%;">Greedy</span>
</a>
</div>
<!-- heading -->
<div class="heading-div">
<img style="height: min(9vh, 12vw);"
src="https://readme-typing-svg.demolab.com?font=Noto+Sans+SC&weight=400&duration=3500&pause=2000&color=21C8B8&center=true&vCenter=true&random=false&width=200&lines=Hello%2C+Algo+!"
alt="" />
<div style="pointer-events: auto;">
<p style="margin-top: max(-1vh, -2vw); margin-bottom: min(2vh, 3.5vw);">
Data structures and algorithms crash course with animated illustrations and off-the-shelf code
</p>
<a href="chapter_hello_algo/" class="rounded-button">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.-->
<path
d="M160 96a96 96 0 1 1 192 0A96 96 0 1 1 160 96zm80 152V512l-48.4-24.2c-20.9-10.4-43.5-17-66.8-19.3l-96-9.6C12.5 457.2 0 443.5 0 427V224c0-17.7 14.3-32 32-32H62.3c63.6 0 125.6 19.6 177.7 56zm32 264V248c52.1-36.4 114.1-56 177.7-56H480c17.7 0 32 14.3 32 32V427c0 16.4-12.5 30.2-28.8 31.8l-96 9.6c-23.2 2.3-45.9 8.9-66.8 19.3L272 512z" />
</svg>
<span>Dive in</span>
</a>
<a href="https://github.com/krahets/hello-algo" class="rounded-button">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512">
<path
d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3 .3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5 .3-6.2 2.3zm44.2-1.7c-2.9 .7-4.9 2.6-4.6 4.9 .3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3 .7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3 .3 2.9 2.3 3.9 1.6 1 3.6 .7 4.3-.7 .7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3 .7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3 .7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z">
</path>
</svg>
<span>GitHub</span>
</a>
</div>
<!-- arrow -->
<div style="text-align: center; margin-top: min(2.5vh, 3.5vw);">
<svg xmlns="http://www.w3.org/2000/svg" fill="var(--md-default-fg-color)" height="2vh"
viewBox="0 0 384 512">
<path
d="M169.4 470.6c12.5 12.5 32.8 12.5 45.3 0l160-160c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L224 370.8 224 64c0-17.7-14.3-32-32-32s-32 14.3-32 32l0 306.7L54.6 265.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l160 160z" />
</svg>
</div>
</div>
</section>
<!-- Section: brief introduction -->
<section data-md-color-scheme="slate" class="home-div">
<div class="section-content">
<img src="index.assets/hello_algo_header.png" style="width: 100%; max-width: 41.5em;" alt="Preview">
<div class="code-badge">
<img src="https://img.shields.io/badge/Python-snow?logo=python&logoColor=3776AB" alt="" />
<img src="https://img.shields.io/badge/Java-snow?logo=coffeescript&logoColor=FC4C02" alt="" />
<img src="https://img.shields.io/badge/C%2B%2B-snow?logo=c%2B%2B&logoColor=00599C" alt="" />
<img src="https://img.shields.io/badge/C-snow?logo=c&logoColor=A8B9CC" alt="" />
<img src="https://img.shields.io/badge/C%23-snow?logo=csharp&logoColor=512BD4" alt="" />
<img src="https://img.shields.io/badge/JS-snow?logo=javascript&logoColor=E9CE30" alt="" />
<img src="https://img.shields.io/badge/Go-snow?logo=go&logoColor=00ADD8" alt="" />
<img src="https://img.shields.io/badge/Swift-snow?logo=swift&logoColor=F05138" alt="" />
<img src="https://img.shields.io/badge/Rust-snow?logo=rust&logoColor=000000" alt="" />
<img src="https://img.shields.io/badge/Ruby-snow?logo=ruby&logoColor=CC342D" alt="" />
<img src="https://img.shields.io/badge/Kotlin-snow?logo=kotlin&logoColor=7F52FF" alt="" />
<img src="https://img.shields.io/badge/TS-snow?logo=typescript&logoColor=3178C6" alt="" />
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2" alt="" />
<img src="https://img.shields.io/badge/Zig-snow?logo=zig&logoColor=F7A41D" alt="" />
</div>
<p style="margin-top: 2em;">500 animated illustrations, 14 programming languages, and 3000 community Q&As to help you quickly get started with data structures and algorithms</p>
</div>
</section>
<!-- Section: reading -->
<section data-md-color-scheme="default" data-md-color-primary="white" class="home-div">
<div class="section-content">
<div class="reading-media">
<!-- devices -->
<!-- book height = 250 mm -->
<!-- mac height = 248.1 mm -->
<!-- ipad height = 280.6mm -->
<!-- iphone height = 160.7mm -->
<div class="media-block">
<a href="chapter_paperbook/">
<div style="height: 8.17%;"></div>
<img class="device-on-hover" style="height: 66.83%;" src="../assets/hero/cover_render.png" alt="Cover">
<div class="text-button">
<span>Paperbook</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
<path
d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" />
</svg>
</div>
</a>
</div>
<div class="media-block">
<a href="chapter_hello_algo/">
<div style="height: 4.34%;"></div>
<img class="device-on-hover" style="height: 66.31%;" src="../assets/hero/web_mac_iphone.png" alt="">
<div style="height: 4.34%;"></div>
<div class="text-button">
<span>Read online</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
<path
d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" />
</svg>
</div>
</a>
</div>
<div class="media-block">
<a href="https://github.com/krahets/hello-algo/releases">
<img class="device-on-hover" style="height: 75%;" src="../assets/hero/pdf_ipad.png" alt="">
<div class="text-button">
<span>Download PDF</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
<path
d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" />
</svg>
</div>
</a>
</div>
</div>
</div>
</section>
<!-- Section: endorsements -->
<section data-md-color-scheme="slate" data-md-color-primary="grey" class="home-div">
<div class="section-content">
<h3 style="text-align: center; margin: 1em auto;">Endorsements</h3>
<div class="intro-container" style="margin: 0 auto;">
<div class="intro-text endor-text">
<p style="margin-bottom: 0;">“An easy-to-understand book on data structures and algorithms, which guides readers to learn by minds-on and hands-on. Strongly recommended for algorithm beginners!”</p>
<p style="font-weight: bold;">—— Junhui Deng, Professor, Department of computer science and technology, Tsinghua University</p>
</div>
<div class="intro-text endor-text">
<p style="margin-bottom: 0;">“If I had 'Hello Algo' when I was learning data structures and algorithms, it would have been 10 times easier!”</p>
<p style="font-weight: bold;">—— Mu Li, Senior principal scientist, Amazon</p>
</div>
</div>
</div>
</section>
<!-- Section: features -->
<section data-md-color-scheme="default" data-md-color-primary="white" class="home-div">
<div class="section-content">
<div class="intro-container">
<div class="intro-text">
<div>
<div style="display: flex; align-items: center;">
<svg xmlns="http://www.w3.org/2000/svg" height="1.5em" width="1.5em" viewBox="0 0 640 512">
<path fill="var(--md-primary-bg-color)"
d="M256 0H576c35.3 0 64 28.7 64 64V288c0 35.3-28.7 64-64 64H256c-35.3 0-64-28.7-64-64V64c0-35.3 28.7-64 64-64zM476 106.7C471.5 100 464 96 456 96s-15.5 4-20 10.7l-56 84L362.7 169c-4.6-5.7-11.5-9-18.7-9s-14.2 3.3-18.7 9l-64 80c-5.8 7.2-6.9 17.1-2.9 25.4s12.4 13.6 21.6 13.6h80 48H552c8.9 0 17-4.9 21.2-12.7s3.7-17.3-1.2-24.6l-96-144zM336 96a32 32 0 1 0 -64 0 32 32 0 1 0 64 0zM64 128h96V384v32c0 17.7 14.3 32 32 32H320c17.7 0 32-14.3 32-32V384H512v64c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V192c0-35.3 28.7-64 64-64zm8 64c-8.8 0-16 7.2-16 16v16c0 8.8 7.2 16 16 16H88c8.8 0 16-7.2 16-16V208c0-8.8-7.2-16-16-16H72zm0 104c-8.8 0-16 7.2-16 16v16c0 8.8 7.2 16 16 16H88c8.8 0 16-7.2 16-16V312c0-8.8-7.2-16-16-16H72zm0 104c-8.8 0-16 7.2-16 16v16c0 8.8 7.2 16 16 16H88c8.8 0 16-7.2 16-16V416c0-8.8-7.2-16-16-16H72zm336 16v16c0 8.8 7.2 16 16 16h16c8.8 0 16-7.2 16-16V416c0-8.8-7.2-16-16-16H424c-8.8 0-16 7.2-16 16z" />
</svg>
<h3 style="margin-left: 0.5em;">Animated illustrations</h3>
</div>
<p style="margin: 0;">It's crafted for ease of understanding, ensuring a smooth learning journey.</p>
<p class="intro-quote">"A picture is worth a thousand words."</p>
</div>
</div>
<img class="intro-image" src="index.assets/animation.gif" alt="Animation example">
</div>
<div class="intro-container">
<img class="intro-image" src="index.assets/running_code.gif" alt="Running code example">
<div class="intro-text">
<div>
<div style="display: flex; align-items: center;">
<svg xmlns="http://www.w3.org/2000/svg" height="1.5em" width="1.5em" viewBox="0 0 640 512">
<path fill="var(--md-primary-bg-color)"
d="M392.8 1.2c-17-4.9-34.7 5-39.6 22l-128 448c-4.9 17 5 34.7 22 39.6s34.7-5 39.6-22l128-448c4.9-17-5-34.7-22-39.6zm80.6 120.1c-12.5 12.5-12.5 32.8 0 45.3L562.7 256l-89.4 89.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l112-112c12.5-12.5 12.5-32.8 0-45.3l-112-112c-12.5-12.5-32.8-12.5-45.3 0zm-306.7 0c-12.5-12.5-32.8-12.5-45.3 0l-112 112c-12.5 12.5-12.5 32.8 0 45.3l112 112c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L77.3 256l89.4-89.4c12.5-12.5 12.5-32.8 0-45.3z" />
</svg>
<h3 style="margin-left: 0.5em;">Off-the-shelf code</h3>
</div>
<p style="margin: 0;">Featuring multiple programming languages, all runnable with a single click.</p>
<p class="intro-quote">"Talk is cheap. Show me the code."</p>
</div>
</div>
</div>
<div class="intro-container">
<div class="intro-text">
<div>
<div style="display: flex; align-items: center;">
<svg xmlns="http://www.w3.org/2000/svg" height="1.5em" width="1.5em" viewBox="0 0 640 512">
<path fill="var(--md-primary-bg-color)"
d="M88.2 309.1c9.8-18.3 6.8-40.8-7.5-55.8C59.4 230.9 48 204 48 176c0-63.5 63.8-128 160-128s160 64.5 160 128s-63.8 128-160 128c-13.1 0-25.8-1.3-37.8-3.6c-10.4-2-21.2-.6-30.7 4.2c-4.1 2.1-8.3 4.1-12.6 6c-16 7.2-32.9 13.5-49.9 18c2.8-4.6 5.4-9.1 7.9-13.6c1.1-1.9 2.2-3.9 3.2-5.9zM0 176c0 41.8 17.2 80.1 45.9 110.3c-.9 1.7-1.9 3.5-2.8 5.1c-10.3 18.4-22.3 36.5-36.6 52.1c-6.6 7-8.3 17.2-4.6 25.9C5.8 378.3 14.4 384 24 384c43 0 86.5-13.3 122.7-29.7c4.8-2.2 9.6-4.5 14.2-6.8c15.1 3 30.9 4.5 47.1 4.5c114.9 0 208-78.8 208-176S322.9 0 208 0S0 78.8 0 176zM432 480c16.2 0 31.9-1.6 47.1-4.5c4.6 2.3 9.4 4.6 14.2 6.8C529.5 498.7 573 512 616 512c9.6 0 18.2-5.7 22-14.5c3.8-8.8 2-19-4.6-25.9c-14.2-15.6-26.2-33.7-36.6-52.1c-.9-1.7-1.9-3.4-2.8-5.1C622.8 384.1 640 345.8 640 304c0-94.4-87.9-171.5-198.2-175.8c4.1 15.2 6.2 31.2 6.2 47.8l0 .6c87.2 6.7 144 67.5 144 127.4c0 28-11.4 54.9-32.7 77.2c-14.3 15-17.3 37.6-7.5 55.8c1.1 2 2.2 4 3.2 5.9c2.5 4.5 5.2 9 7.9 13.6c-17-4.5-33.9-10.7-49.9-18c-4.3-1.9-8.5-3.9-12.6-6c-9.5-4.8-20.3-6.2-30.7-4.2c-12.1 2.4-24.7 3.6-37.8 3.6c-61.7 0-110-26.5-136.8-62.3c-16 5.4-32.8 9.4-50 11.8C279 439.8 350 480 432 480z" />
</svg>
<h3 style="margin-left: 0.5em;">Learning together</h3>
</div>
<p style="margin: 0;">Welcome discussions and questions with open arms.</p>
<p class="intro-quote">"Learning by teaching."</p>
</div>
</div>
<img class="intro-image" src="index.assets/comment.gif" alt="Comments example">
</div>
</div>
</section>
<!-- Section: contributors -->
<section data-md-color-scheme="slate" data-md-color-primary="grey" class="home-div">
<div class="section-content" style="max-width: 90vw;">
<!-- author -->
<div style="margin: 2em auto;">
<h3>Author</h3>
<div class="profile-div">
<div class="profile-cell">
<a href="https://github.com/krahets">
<img class="profile-img" src="../assets/avatar/avatar_yudongjin.jpg" alt="Author: yudongjin" />
<br><b>Yudong Jin@krahets</b>
</a>
</div>
</div>
</div>
<!-- reviewers -->
<div style="margin: 2em auto;">
<h3>Code reviewers</h3>
<div class="profile-div">
<div class="profile-cell">
<a href="https://github.com/codingonion">
<img class="profile-img" src="../assets/avatar/avatar_codingonion.jpg"
alt="Reviewer: codingonion" />
<br><b>codingonion</b>
<br><sub>Zig, Rust</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/curtishd">
<img class="profile-img" src="../assets/avatar/avatar_curtishd.jpg"
alt="Reviewer: curtishd" />
<br><b>curtishd</b>
<br><sub>Kotlin</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/Gonglja">
<img class="profile-img" src="../assets/avatar/avatar_Gonglja.jpg" alt="Reviewer: Gonglja" />
<br><b>Gonglja</b>
<br><sub>C, C++</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/gvenusleo">
<img class="profile-img" src="../assets/avatar/avatar_gvenusleo.jpg" alt="Reviewer: gvenusleo" />
<br><b>gvenusleo</b>
<br><sub>Dart</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/hpstory">
<img class="profile-img" src="../assets/avatar/avatar_hpstory.jpg" alt="Reviewer: hpstory" />
<br><b>hpstory</b>
<br><sub>C#</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/justin-tse">
<img class="profile-img" src="../assets/avatar/avatar_justin-tse.jpg" alt="Reviewer: justin-tse" />
<br><b>justin-tse</b>
<br><sub>JS, TS</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/krahets">
<img class="profile-img" src="../assets/avatar/avatar_krahets.jpg" alt="Reviewer: krahets" />
<br><b>krahets</b>
<br><sub>Python, Java</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/night-cruise">
<img class="profile-img" src="../assets/avatar/avatar_night-cruise.jpg"
alt="Reviewer: night-cruise" />
<br><b>night-cruise</b>
<br><sub>Rust</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/nuomi1">
<img class="profile-img" src="../assets/avatar/avatar_nuomi1.jpg" alt="Reviewer: nuomi1" />
<br><b>nuomi1</b>
<br><sub>Swift</sub>
</a>
</div>
<div class="profile-cell">
<a href="https://github.com/Reanon">
<img class="profile-img" src="../assets/avatar/avatar_Reanon.jpg" alt="Reviewer: Reanon" />
<br><b>Reanon</b>
<br><sub>Go, C</sub>
</a>
</div>
</div>
</div>
<!-- contributors -->
<div style="margin: 2em auto;">
<h3>Contributors</h3>
<p>This book has been enhanced through the collaborative efforts of more than 100 contributors. Thanks for their invaluable time and input!</p>
<a href="https://github.com/krahets/hello-algo/graphs/contributors">
<img src="https://contrib.rocks/image?repo=krahets/hello-algo&max=300&columns=16" alt="Contributors"
style="width: 100%; max-width: 38.5em;">
</a>
</div>
</div>
</section>

View File

@@ -1,156 +1,5 @@
--- # Hello Algo
comments: true
glightbox: false
hide:
- footer
- toc
- edit
---
<div class="header-img-div" style="max-width: 600px;"> Data structures and algorithms crash course with animated illustrations and off-the-shelf code
<img src="index.assets/hello_algo_header.png" style="width: 100%; height: auto; margin-left: 15px; margin-right: 15px;">
</div>
<h2 align="center" style="margin-top: 25px;">Hello Algo</h2> [Dive in](chapter_hello_algo/)
<p align="center">Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code</p>
<p align="center">
<a href="https://github.com/krahets/hello-algo">
<img alt="GitHub repo stars" src="https://img.shields.io/github/stars/krahets/hello-algo?style=social&link=https%3A%2F%2Fgithub.com%2Fkrahets%2Fhello-algo">
</a>
&nbsp;
<a href="https://github.com/krahets/hello-algo/releases">
<img alt="GitHub all releases" src="https://img.shields.io/github/downloads/krahets/hello-algo/total?style=social&logo=gitbook&logoColor=black&label=Downloads">
</a>
&nbsp;
<a href="https://github.com/krahets/hello-algo">
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors-anon/krahets/hello-algo?style=social&logo=git&logoColor=%23101010">
</a>
</p>
<p align="center">
<a href="https://www.hello-algo.com/en/chapter_preface/" class="rounded-button">
<svg xmlns="http://www.w3.org/2000/svg" height="16" width="18" viewBox="0 0 576 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path d="M249.6 471.5c10.8 3.8 22.4-4.1 22.4-15.5V78.6c0-4.2-1.6-8.4-5-11C247.4 52 202.4 32 144 32C93.5 32 46.3 45.3 18.1 56.1C6.8 60.5 0 71.7 0 83.8V454.1c0 11.9 12.8 20.2 24.1 16.5C55.6 460.1 105.5 448 144 448c33.9 0 79 14 105.6 23.5zm76.8 0C353 462 398.1 448 432 448c38.5 0 88.4 12.1 119.9 22.6c11.3 3.8 24.1-4.6 24.1-16.5V83.8c0-12.1-6.8-23.3-18.1-27.6C529.7 45.3 482.5 32 432 32c-58.4 0-103.4 20-123 35.6c-3.3 2.6-5 6.8-5 11V456c0 11.4 11.7 19.3 22.4 15.5z"/></svg>
Dive In
</a>
<a href="https://github.com/krahets/hello-algo" class="rounded-button">
<svg xmlns="http://www.w3.org/2000/svg" height="16px" width="15.5px" viewBox="0 0 496 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3 .3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5 .3-6.2 2.3zm44.2-1.7c-2.9 .7-4.9 2.6-4.6 4.9 .3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3 .7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3 .3 2.9 2.3 3.9 1.6 1 3.6 .7 4.3-.7 .7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3 .7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3 .7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
Clone Repo
</a>
<a href="https://github.com/krahets/hello-algo/releases" class="rounded-button">
<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path d="M0 64C0 28.7 28.7 0 64 0L224 0l0 128c0 17.7 14.3 32 32 32l128 0 0 144-208 0c-35.3 0-64 28.7-64 64l0 144-48 0c-35.3 0-64-28.7-64-64L0 64zm384 64l-128 0L256 0 384 128zM176 352l32 0c30.9 0 56 25.1 56 56s-25.1 56-56 56l-16 0 0 32c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-48 0-80c0-8.8 7.2-16 16-16zm32 80c13.3 0 24-10.7 24-24s-10.7-24-24-24l-16 0 0 48 16 0zm96-80l32 0c26.5 0 48 21.5 48 48l0 64c0 26.5-21.5 48-48 48l-32 0c-8.8 0-16-7.2-16-16l0-128c0-8.8 7.2-16 16-16zm32 128c8.8 0 16-7.2 16-16l0-64c0-8.8-7.2-16-16-16l-16 0 0 96 16 0zm80-112c0-8.8 7.2-16 16-16l48 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 32 32 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 48c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-64 0-64z"/></svg>
Get PDF
</a>
</p>
<div class="admonition example" style="max-width: 800px;margin: 0 auto;">
<p class="admonition-title">The English version is brewing...</p>
<p>Feel free to engage in Chinese-to-English translation and pull request review! For guidelines, please see <a href="https://github.com/krahets/hello-algo/issues/914">#914</a>.</p>
</div>
<div style="display: flex;">
<div class="admonition quote" style="flex: 1; margin-right: 0.4rem;">
<p class="admonition-title">Quote</p>
<p>"An easy-to-understand book on data structures and algorithms, which guides readers to learn by minds-on and hands-on. Strongly recommended for algorithm beginners!"</p>
<p><strong>—— Junhui Deng, Professor of Computer Science, Tsinghua University</strong></p>
</div>
<div class="admonition quote" style="flex: 1; margin-left: 0.4rem;">
<p class="admonition-title">Quote</p>
<p>"If I had 'Hello Algo' when I was learning data structures and algorithms, it would have been 10 times easier!"</p>
<p><strong>—— Mu Li, Senior Principal Scientist, Amazon</strong></p>
</div>
</div>
---
<div style="display: flex; align-items: center; margin: 2rem auto;">
<div style="flex: 1; margin: 0 2rem; display: flex; flex-direction: column; align-items: center;">
<div style="display: flex; flex-direction: column; align-items: center;">
<h3>Animated illustrations</h3>
<svg xmlns="http://www.w3.org/2000/svg" height="28" width="28" viewBox="0 0 640 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path fill="var(--md-primary-bg-color)" d="M256 0H576c35.3 0 64 28.7 64 64V288c0 35.3-28.7 64-64 64H256c-35.3 0-64-28.7-64-64V64c0-35.3 28.7-64 64-64zM476 106.7C471.5 100 464 96 456 96s-15.5 4-20 10.7l-56 84L362.7 169c-4.6-5.7-11.5-9-18.7-9s-14.2 3.3-18.7 9l-64 80c-5.8 7.2-6.9 17.1-2.9 25.4s12.4 13.6 21.6 13.6h80 48H552c8.9 0 17-4.9 21.2-12.7s3.7-17.3-1.2-24.6l-96-144zM336 96a32 32 0 1 0 -64 0 32 32 0 1 0 64 0zM64 128h96V384v32c0 17.7 14.3 32 32 32H320c17.7 0 32-14.3 32-32V384H512v64c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V192c0-35.3 28.7-64 64-64zm8 64c-8.8 0-16 7.2-16 16v16c0 8.8 7.2 16 16 16H88c8.8 0 16-7.2 16-16V208c0-8.8-7.2-16-16-16H72zm0 104c-8.8 0-16 7.2-16 16v16c0 8.8 7.2 16 16 16H88c8.8 0 16-7.2 16-16V312c0-8.8-7.2-16-16-16H72zm0 104c-8.8 0-16 7.2-16 16v16c0 8.8 7.2 16 16 16H88c8.8 0 16-7.2 16-16V416c0-8.8-7.2-16-16-16H72zm336 16v16c0 8.8 7.2 16 16 16h16c8.8 0 16-7.2 16-16V416c0-8.8-7.2-16-16-16H424c-8.8 0-16 7.2-16 16z"/></svg>
</div>
<p style="text-align: center;">Easy to understand</br>Smooth learning curve</p>
</div>
<img src="index.assets/animation.gif" class="cover-image" style="flex-shrink: 0; width: auto; max-width: 50%; border-radius: 0.5rem;">
</div>
<div class="admonition quote">
<p align="center">"A picture is worth a thousand words."</p>
</div>
<div style="display: flex; align-items: center; margin: 2rem auto;">
<img src="index.assets/running_code.gif" class="cover-image" style="flex-shrink: 0; width: auto; max-width: 50%; border-radius: 0.5rem;">
<div style="flex: 1; margin: 0 2rem; display: flex; flex-direction: column; align-items: center;">
<div style="display: flex; flex-direction: column; align-items: center;">
<h3>Off-the-Shelf Code</h3>
<svg xmlns="http://www.w3.org/2000/svg" height="28" width="28" viewBox="0 0 640 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path fill="var(--md-primary-bg-color)" d="M392.8 1.2c-17-4.9-34.7 5-39.6 22l-128 448c-4.9 17 5 34.7 22 39.6s34.7-5 39.6-22l128-448c4.9-17-5-34.7-22-39.6zm80.6 120.1c-12.5 12.5-12.5 32.8 0 45.3L562.7 256l-89.4 89.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l112-112c12.5-12.5 12.5-32.8 0-45.3l-112-112c-12.5-12.5-32.8-12.5-45.3 0zm-306.7 0c-12.5-12.5-32.8-12.5-45.3 0l-112 112c-12.5 12.5-12.5 32.8 0 45.3l112 112c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L77.3 256l89.4-89.4c12.5-12.5 12.5-32.8 0-45.3z"/></svg>
</div>
<p style="text-align: center;">Multi programming languages</br>Run with one click</p>
</div>
</div>
<div class="admonition quote">
<p align="center">"Talk is cheap. Show me the code."</p>
</div>
<div style="display: flex; align-items: center; margin: 2rem auto;">
<div style="flex: 1; margin: 0 2rem; display: flex; flex-direction: column; align-items: center;">
<div style="display: flex; flex-direction: column; align-items: center;">
<h3>Learning Together</h3>
<svg xmlns="http://www.w3.org/2000/svg" height="28" width="28" viewBox="0 0 640 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path fill="var(--md-primary-bg-color)" d="M88.2 309.1c9.8-18.3 6.8-40.8-7.5-55.8C59.4 230.9 48 204 48 176c0-63.5 63.8-128 160-128s160 64.5 160 128s-63.8 128-160 128c-13.1 0-25.8-1.3-37.8-3.6c-10.4-2-21.2-.6-30.7 4.2c-4.1 2.1-8.3 4.1-12.6 6c-16 7.2-32.9 13.5-49.9 18c2.8-4.6 5.4-9.1 7.9-13.6c1.1-1.9 2.2-3.9 3.2-5.9zM0 176c0 41.8 17.2 80.1 45.9 110.3c-.9 1.7-1.9 3.5-2.8 5.1c-10.3 18.4-22.3 36.5-36.6 52.1c-6.6 7-8.3 17.2-4.6 25.9C5.8 378.3 14.4 384 24 384c43 0 86.5-13.3 122.7-29.7c4.8-2.2 9.6-4.5 14.2-6.8c15.1 3 30.9 4.5 47.1 4.5c114.9 0 208-78.8 208-176S322.9 0 208 0S0 78.8 0 176zM432 480c16.2 0 31.9-1.6 47.1-4.5c4.6 2.3 9.4 4.6 14.2 6.8C529.5 498.7 573 512 616 512c9.6 0 18.2-5.7 22-14.5c3.8-8.8 2-19-4.6-25.9c-14.2-15.6-26.2-33.7-36.6-52.1c-.9-1.7-1.9-3.4-2.8-5.1C622.8 384.1 640 345.8 640 304c0-94.4-87.9-171.5-198.2-175.8c4.1 15.2 6.2 31.2 6.2 47.8l0 .6c87.2 6.7 144 67.5 144 127.4c0 28-11.4 54.9-32.7 77.2c-14.3 15-17.3 37.6-7.5 55.8c1.1 2 2.2 4 3.2 5.9c2.5 4.5 5.2 9 7.9 13.6c-17-4.5-33.9-10.7-49.9-18c-4.3-1.9-8.5-3.9-12.6-6c-9.5-4.8-20.3-6.2-30.7-4.2c-12.1 2.4-24.7 3.6-37.8 3.6c-61.7 0-110-26.5-136.8-62.3c-16 5.4-32.8 9.4-50 11.8C279 439.8 350 480 432 480z"/></svg>
</div>
<p style="text-align: center;">Discussion and questions welcome</br>Readers progress together</p>
</div>
<img src="index.assets/comment.gif" class="cover-image" style="flex-shrink: 0; width: auto; max-width: 50%; border-radius: 0.5rem;">
</div>
<div class="admonition quote">
<p align="center">"Learning by teaching."</p>
</div>
---
<h3 align="center"> Preface </h3>
Two years ago, I shared the "Sword Offer" series of problem solutions on LeetCode, which received much love and support from many students. During my interactions with readers, the most common question I encountered was "How to get started with algorithms." Gradually, I developed a deep interest in this question.
Blindly solving problems seems to be the most popular method, being simple, direct, and effective. However, problem-solving is like playing a "Minesweeper" game, where students with strong self-learning abilities can successfully clear the mines one by one, but those with insufficient foundations may end up bruised from explosions, retreating step by step in frustration. Thoroughly reading textbooks is also common, but for students aiming for job applications, the energy consumed by graduation, resume submissions, and preparing for written tests and interviews makes tackling thick books a daunting challenge.
If you are facing similar troubles, then you are lucky to have found this book. This book is my answer to this question, not necessarily the best solution, but at least an active attempt. Although this book won't directly land you an Offer, it will guide you through the "knowledge map" of data structures and algorithms, help you understand the shape, size, and distribution of different "mines," and equip you with various "demining methods." With these skills, I believe you can more comfortably solve problems and read literature, gradually building a complete knowledge system.
I deeply agree with Professor Feynman's saying: "Knowledge isn't free. You have to pay attention." In this sense, this book is not entirely "free." To not disappoint the precious "attention" you pay to this book, I will do my utmost, investing the greatest "attention" to complete the creation of this book.
<h3 align="left"> Author </h3>
Yudong Jin([krahets](https://leetcode.cn/u/jyd/)), Senior Algorithm Engineer in a top tech company, Master's degree from Shanghai Jiao Tong University. The highest-read blogger across the entire LeetCode, his published ["Illustration of Algorithm Data Structures"](https://leetcode.cn/leetbook/detail/illustration-of-algorithm/) has been subscribed to by over 300k.
---
<h3 align="center"> Contribution </h3>
This book is continuously improved with the joint efforts of many contributors from the open-source community. Thanks to each writer who invested their time and energy, listed in the order generated by GitHub:
<p align="center">
<a href="https://github.com/krahets/hello-algo/graphs/contributors">
<img width="550" src="https://contrib.rocks/image?repo=krahets/hello-algo">
</a>
</p>
The code review work for this book was completed by codingonion, Gonglja, gvenusleo, hpstory, justintse, krahets, night-cruise, nuomi1, and Reanon (listed in alphabetical order). Thanks to them for their time and effort, ensuring the standardization and uniformity of the code in various languages.
<div class="center-table">
<table style="border: none;">
<tbody>
<td align="center" style="border: none;"><a href="https://github.com/codingonion"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/99076655?v=4" width="50px;" alt="codingonion"/></br><sub><b>codingonion</b></sub></a></br><sub>Rust, Zig</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/Gonglja"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/39959756?v=4" width="50px;" alt="Gonglja"/></br><sub><b>Gonglja</b></sub></a></br><sub>C, C++</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/gvenusleo"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/79075347?v=4" width="50px;" alt="gvenusleo"/></br><sub><b>gvenusleo</b></sub></a></br><sub>Dart</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/hpstory"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/33348162?v=4" width="50px;" alt="hpstory"/></br><sub><b>hpstory</b></sub></a></br><sub>C#</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/justin-tse"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/24556310?v=4" width="50px;" alt="justin-tse"/></br><sub><b>justin-tse</b></sub></a></br><sub>JS, TS</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/krahets"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/26993056?v=4" width="50px;" alt="krahets"/></br><sub><b>krahets</b></sub></a></br><sub>Java, Python</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/night-cruise"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/77157236?v=4" width="50px;" alt="night-cruise"/></br><sub><b>night-cruise</b></sub></a></br><sub>Rust</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/nuomi1"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/3739017?v=4" width="50px;" alt="nuomi1"/></br><sub><b>nuomi1</b></sub></a></br><sub>Swift</sub></td>
<td align="center" style="border: none;"><a href="https://github.com/Reanon"><img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/22005836?v=4" width="50px;" alt="Reanon"/></br><sub><b>Reanon</b></sub></a></br><sub>Go, C</sub></td>
</tbody>
</table>
</div>

View File

@@ -64,7 +64,7 @@ nav:
# [icon: material/shape-outline] # [icon: material/shape-outline]
- chapter_data_structure/index.md - chapter_data_structure/index.md
- 3.1 Classification of data structures: chapter_data_structure/classification_of_data_structure.md - 3.1 Classification of data structures: chapter_data_structure/classification_of_data_structure.md
- 3.2 Fundamental data types: chapter_data_structure/basic_data_types.md - 3.2 Basic data types: chapter_data_structure/basic_data_types.md
- 3.3 Number encoding *: chapter_data_structure/number_encoding.md - 3.3 Number encoding *: chapter_data_structure/number_encoding.md
- 3.4 Character encoding *: chapter_data_structure/character_encoding.md - 3.4 Character encoding *: chapter_data_structure/character_encoding.md
- 3.5 Summary: chapter_data_structure/summary.md - 3.5 Summary: chapter_data_structure/summary.md
@@ -93,25 +93,25 @@ nav:
- Chapter 7. Tree: - Chapter 7. Tree:
# [icon: material/graph-outline] # [icon: material/graph-outline]
- chapter_tree/index.md - chapter_tree/index.md
- 7.1 Binary Tree: chapter_tree/binary_tree.md - 7.1 Binary tree: chapter_tree/binary_tree.md
- 7.2 Binary Tree Traversal: chapter_tree/binary_tree_traversal.md - 7.2 Binary tree Traversal: chapter_tree/binary_tree_traversal.md
- 7.3 Array Representation of Tree: chapter_tree/array_representation_of_tree.md - 7.3 Array Representation of tree: chapter_tree/array_representation_of_tree.md
- 7.4 Binary Search Tree: chapter_tree/binary_search_tree.md - 7.4 Binary Search tree: chapter_tree/binary_search_tree.md
- 7.5 AVL Tree *: chapter_tree/avl_tree.md - 7.5 AVL tree *: chapter_tree/avl_tree.md
- 7.6 Summary: chapter_tree/summary.md - 7.6 Summary: chapter_tree/summary.md
- Chapter 8. Heap: - Chapter 8. Heap:
# [icon: material/family-tree] # [icon: material/family-tree]
- chapter_heap/index.md - chapter_heap/index.md
- 8.1 Heap: chapter_heap/heap.md - 8.1 Heap: chapter_heap/heap.md
- 8.2 Building a Heap: chapter_heap/build_heap.md - 8.2 Building a heap: chapter_heap/build_heap.md
- 8.3 Top-k Problem: chapter_heap/top_k.md - 8.3 Top-k problem: chapter_heap/top_k.md
- 8.4 Summary: chapter_heap/summary.md - 8.4 Summary: chapter_heap/summary.md
- Chapter 9. Graph: - Chapter 9. Graph:
# [icon: material/graphql] # [icon: material/graphql]
- chapter_graph/index.md - chapter_graph/index.md
- 9.1 Graph: chapter_graph/graph.md - 9.1 Graph: chapter_graph/graph.md
- 9.2 Basic Graph Operations: chapter_graph/graph_operations.md - 9.2 Basic graph operations: chapter_graph/graph_operations.md
- 9.3 Graph Traversal: chapter_graph/graph_traversal.md - 9.3 Graph traversal: chapter_graph/graph_traversal.md
- 9.4 Summary: chapter_graph/summary.md - 9.4 Summary: chapter_graph/summary.md
# - Chapter 10. Searching: # - Chapter 10. Searching:
# # [icon: material/text-search] # # [icon: material/text-search]

View File

@@ -6,7 +6,7 @@
{% elif config.theme.language == 'zh-Hant' %} {% elif config.theme.language == 'zh-Hant' %}
{% set announcements = '紙質書(簡體中文版)已發行,詳情請見<a href="/chapter_paperbook/">這裡</a>' %} {% set announcements = '紙質書(簡體中文版)已發行,詳情請見<a href="/chapter_paperbook/">這裡</a>' %}
{% elif config.theme.language == 'en' %} {% elif config.theme.language == 'en' %}
{% set announcements = 'The paper book (Chinese version) published. Please visit <a href="/chapter_paperbook/">this link</a> for more details.' %} {% set announcements = 'Feel free to engage in Chinese-to-English translation and pull request review! Please visit <a href="https://github.com/krahets/hello-algo/issues/914">#914</a> for details.' %}
{% endif %} {% endif %}
<div class="banner-svg"> <div class="banner-svg">
<svg xmlns="http://www.w3.org/2000/svg" <svg xmlns="http://www.w3.org/2000/svg"

View File

@@ -519,13 +519,6 @@ a:hover .text-button span {
.text-button { .text-button {
margin: 0.7em auto; margin: 0.7em auto;
} }
.profile-div {
max-width: 30em;
}
.profile-cell {
flex-basis: 25%;
}
} }
.video-container { .video-container {

View File

@@ -49,6 +49,9 @@ void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {
free(pair); free(pair);
} }
} }
free(hashMap->buckets);
free(hashMap->TOMBSTONE);
free(hashMap);
} }
/* 雜湊函式 */ /* 雜湊函式 */

View File

@@ -122,8 +122,8 @@ int pop(LinkedListDeque *deque, bool isFront) {
if (fNext) { if (fNext) {
fNext->prev = NULL; fNext->prev = NULL;
deque->front->next = NULL; deque->front->next = NULL;
delDoublyListNode(deque->front);
} }
delDoublyListNode(deque->front);
deque->front = fNext; // 更新頭節點 deque->front = fNext; // 更新頭節點
} }
// 佇列尾出列操作 // 佇列尾出列操作
@@ -133,8 +133,8 @@ int pop(LinkedListDeque *deque, bool isFront) {
if (rPrev) { if (rPrev) {
rPrev->next = NULL; rPrev->next = NULL;
deque->rear->prev = NULL; deque->rear->prev = NULL;
delDoublyListNode(deque->rear);
} }
delDoublyListNode(deque->rear);
deque->rear = rPrev; // 更新尾節點 deque->rear = rPrev; // 更新尾節點
} }
deque->queSize--; // 更新佇列長度 deque->queSize--; // 更新佇列長度

View File

@@ -8,8 +8,6 @@
/* AVL 樹 */ /* AVL 樹 */
class AVLTree { class AVLTree {
public:
TreeNode *root; // 根節點
private: private:
/* 更新節點高度 */ /* 更新節點高度 */
void updateHeight(TreeNode *node) { void updateHeight(TreeNode *node) {
@@ -134,6 +132,8 @@ class AVLTree {
} }
public: public:
TreeNode *root; // 根節點
/* 獲取節點高度 */ /* 獲取節點高度 */
int height(TreeNode *node) { int height(TreeNode *node) {
// 空節點高度為 -1 ,葉節點高度為 0 // 空節點高度為 -1 ,葉節點高度為 0

View File

@@ -48,7 +48,7 @@ func coinChangeDPComp(coins []int, amt int) int {
} }
// 狀態轉移 // 狀態轉移
for i := 1; i <= n; i++ { for i := 1; i <= n; i++ {
// 序走訪 // 序走訪
for a := 1; a <= amt; a++ { for a := 1; a <= amt; a++ {
if coins[i-1] > a { if coins[i-1] > a {
// 若超過目標金額,則不選硬幣 i // 若超過目標金額,則不選硬幣 i

View File

@@ -39,7 +39,7 @@ func coinChangeIIDPComp(coins []int, amt int) int {
dp[0] = 1 dp[0] = 1
// 狀態轉移 // 狀態轉移
for i := 1; i <= n; i++ { for i := 1; i <= n; i++ {
// 序走訪 // 序走訪
for a := 1; a <= amt; a++ { for a := 1; a <= amt; a++ {
if coins[i-1] > a { if coins[i-1] > a {
// 若超過目標金額,則不選硬幣 i // 若超過目標金額,則不選硬幣 i

View File

@@ -50,12 +50,13 @@ public class radix_sort {
if (num > m) if (num > m)
m = num; m = num;
// 按照從低位到高位的順序走訪 // 按照從低位到高位的順序走訪
for (int exp = 1; exp <= m; exp *= 10) for (int exp = 1; exp <= m; exp *= 10) {
// 對陣列元素的第 k 位執行計數排序 // 對陣列元素的第 k 位執行計數排序
// k = 1 -> exp = 1 // k = 1 -> exp = 1
// k = 2 -> exp = 10 // k = 2 -> exp = 10
// 即 exp = 10^(k-1) // 即 exp = 10^(k-1)
countingSortDigit(nums, exp); countingSortDigit(nums, exp);
}
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -55,7 +55,7 @@ fun traverse(nums: IntArray) {
count += nums[i] count += nums[i]
} }
// 直接走訪陣列元素 // 直接走訪陣列元素
for (j: Int in nums) { for (j in nums) {
count += j count += j
} }
} }
@@ -63,7 +63,8 @@ fun traverse(nums: IntArray) {
/* 在陣列中查詢指定元素 */ /* 在陣列中查詢指定元素 */
fun find(nums: IntArray, target: Int): Int { fun find(nums: IntArray, target: Int): Int {
for (i in nums.indices) { for (i in nums.indices) {
if (nums[i] == target) return i if (nums[i] == target)
return i
} }
return -1 return -1
} }

View File

@@ -9,7 +9,7 @@ package chapter_array_and_linkedlist
import utils.ListNode import utils.ListNode
import utils.printLinkedList import utils.printLinkedList
/* 在鏈結串列的節點 n0 之後插入節點p */ /* 在鏈結串列的節點 n0 之後插入節點 P */
fun insert(n0: ListNode?, p: ListNode?) { fun insert(n0: ListNode?, p: ListNode?) {
val n1 = n0?.next val n1 = n0?.next
p?.next = n1 p?.next = n1
@@ -18,16 +18,20 @@ fun insert(n0: ListNode?, p: ListNode?) {
/* 刪除鏈結串列的節點 n0 之後的首個節點 */ /* 刪除鏈結串列的節點 n0 之後的首個節點 */
fun remove(n0: ListNode?) { fun remove(n0: ListNode?) {
val p = n0?.next if (n0?.next == null)
return
val p = n0.next
val n1 = p?.next val n1 = p?.next
n0?.next = n1 n0.next = n1
} }
/* 訪問鏈結串列中索引為 index 的節點 */ /* 訪問鏈結串列中索引為 index 的節點 */
fun access(head: ListNode?, index: Int): ListNode? { fun access(head: ListNode?, index: Int): ListNode? {
var h = head var h = head
for (i in 0..<index) { for (i in 0..<index) {
h = h?.next if (h == null)
return null
h = h.next
} }
return h return h
} }
@@ -37,7 +41,8 @@ fun find(head: ListNode?, target: Int): Int {
var index = 0 var index = 0
var h = head var h = head
while (h != null) { while (h != null) {
if (h.value == target) return index if (h._val == target)
return index
h = h.next h = h.next
index++ index++
} }
@@ -46,6 +51,7 @@ fun find(head: ListNode?, target: Int): Int {
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
/* 初始化鏈結串列 */
// 初始化各個節點 // 初始化各個節點
val n0 = ListNode(1) val n0 = ListNode(1)
val n1 = ListNode(3) val n1 = ListNode(3)
@@ -60,7 +66,8 @@ fun main() {
n3.next = n4 n3.next = n4
println("初始化的鏈結串列為") println("初始化的鏈結串列為")
printLinkedList(n0) printLinkedList(n0)
/* 插入節點 */
insert(n0, ListNode(0)) insert(n0, ListNode(0))
println("插入節點後的鏈結串列為") println("插入節點後的鏈結串列為")
printLinkedList(n0) printLinkedList(n0)
@@ -72,7 +79,7 @@ fun main() {
/* 訪問節點 */ /* 訪問節點 */
val node: ListNode = access(n0, 3)!! val node: ListNode = access(n0, 3)!!
println("鏈結串列中索引 3 處的節點的值 = ${node.value}") println("鏈結串列中索引 3 處的節點的值 = ${node._val}")
/* 查詢節點 */ /* 查詢節點 */
val index: Int = find(n0, 2) val index: Int = find(n0, 2)

View File

@@ -8,9 +8,9 @@ package chapter_array_and_linkedlist
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
/* 初始化串列 */
// 可變集合 // 可變集合
val numbers = mutableListOf(1, 3, 2, 5, 4) val nums = mutableListOf(1, 3, 2, 5, 4)
val nums = ArrayList<Int>(numbers)
println("串列 nums = $nums") println("串列 nums = $nums")
/* 訪問元素 */ /* 訪問元素 */
@@ -53,11 +53,11 @@ fun main() {
} }
/* 拼接兩個串列*/ /* 拼接兩個串列*/
val nums1 = ArrayList<Int>(listOf(6, 8, 7, 10, 9)) val nums1 = mutableListOf(6, 8, 7, 10, 9)
nums.addAll(nums1) nums.addAll(nums1)
println("將串列 nums1 拼接到 nums 之後,得到 nums = $nums") println("將串列 nums1 拼接到 nums 之後,得到 nums = $nums")
/* 排序串列 */ /* 排序串列 */
nums.sort() //排序後,串列元素從小到大排列 nums.sort()
println("排序串列後 nums = $nums") println("排序串列後 nums = $nums")
} }

View File

@@ -9,9 +9,9 @@ package chapter_array_and_linkedlist
/* 串列類別 */ /* 串列類別 */
class MyList { class MyList {
private var arr: IntArray = intArrayOf() // 陣列(儲存串列元素) private var arr: IntArray = intArrayOf() // 陣列(儲存串列元素)
private var capacity = 10 // 串列容量 private var capacity: Int = 10 // 串列容量
private var size = 0 // 串列長度(當前元素數量) private var size: Int = 0 // 串列長度(當前元素數量)
private var extendRatio = 2 // 每次串列擴容的倍數 private var extendRatio: Int = 2 // 每次串列擴容的倍數
/* 建構子 */ /* 建構子 */
init { init {
@@ -32,7 +32,7 @@ class MyList {
fun get(index: Int): Int { fun get(index: Int): Int {
// 索引如果越界,則丟擲異常,下同 // 索引如果越界,則丟擲異常,下同
if (index < 0 || index >= size) if (index < 0 || index >= size)
throw IndexOutOfBoundsException() throw IndexOutOfBoundsException("索引越界")
return arr[index] return arr[index]
} }
@@ -72,7 +72,7 @@ class MyList {
fun remove(index: Int): Int { fun remove(index: Int): Int {
if (index < 0 || index >= size) if (index < 0 || index >= size)
throw IndexOutOfBoundsException("索引越界") throw IndexOutOfBoundsException("索引越界")
val num: Int = arr[index] val num = arr[index]
// 將將索引 index 之後的元素都向前移動一位 // 將將索引 index 之後的元素都向前移動一位
for (j in index..<size - 1) for (j in index..<size - 1)
arr[j] = arr[j + 1] arr[j] = arr[j + 1]

View File

@@ -10,17 +10,17 @@ package chapter_backtracking.n_queens
fun backtrack( fun backtrack(
row: Int, row: Int,
n: Int, n: Int,
state: List<MutableList<String>>, state: MutableList<MutableList<String>>,
res: MutableList<List<List<String>>?>, res: MutableList<MutableList<MutableList<String>>?>,
cols: BooleanArray, cols: BooleanArray,
diags1: BooleanArray, diags1: BooleanArray,
diags2: BooleanArray diags2: BooleanArray
) { ) {
// 當放置完所有行時,記錄解 // 當放置完所有行時,記錄解
if (row == n) { if (row == n) {
val copyState: MutableList<List<String>> = ArrayList() val copyState = mutableListOf<MutableList<String>>()
for (sRow in state) { for (sRow in state) {
copyState.add(ArrayList(sRow)) copyState.add(sRow.toMutableList())
} }
res.add(copyState) res.add(copyState)
return return
@@ -49,11 +49,11 @@ fun backtrack(
} }
/* 求解 n 皇后 */ /* 求解 n 皇后 */
fun nQueens(n: Int): List<List<List<String>>?> { fun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {
// 初始化 n*n 大小的棋盤,其中 'Q' 代表皇后,'#' 代表空位 // 初始化 n*n 大小的棋盤,其中 'Q' 代表皇后,'#' 代表空位
val state: MutableList<MutableList<String>> = ArrayList() val state = mutableListOf<MutableList<String>>()
for (i in 0..<n) { for (i in 0..<n) {
val row: MutableList<String> = ArrayList() val row = mutableListOf<String>()
for (j in 0..<n) { for (j in 0..<n) {
row.add("#") row.add("#")
} }
@@ -62,7 +62,7 @@ fun nQueens(n: Int): List<List<List<String>>?> {
val cols = BooleanArray(n) // 記錄列是否有皇后 val cols = BooleanArray(n) // 記錄列是否有皇后
val diags1 = BooleanArray(2 * n - 1) // 記錄主對角線上是否有皇后 val diags1 = BooleanArray(2 * n - 1) // 記錄主對角線上是否有皇后
val diags2 = BooleanArray(2 * n - 1) // 記錄次對角線上是否有皇后 val diags2 = BooleanArray(2 * n - 1) // 記錄次對角線上是否有皇后
val res: MutableList<List<List<String>>?> = ArrayList() val res = mutableListOf<MutableList<MutableList<String>>?>()
backtrack(0, n, state, res, cols, diags1, diags2) backtrack(0, n, state, res, cols, diags1, diags2)
@@ -72,7 +72,7 @@ fun nQueens(n: Int): List<List<List<String>>?> {
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
val n = 4 val n = 4
val res: List<List<List<String?>?>?> = nQueens(n) val res = nQueens(n)
println("輸入棋盤長寬為 $n") println("輸入棋盤長寬為 $n")
println("皇后放置方案共有 ${res.size}") println("皇后放置方案共有 ${res.size}")

View File

@@ -11,11 +11,11 @@ fun backtrack(
state: MutableList<Int>, state: MutableList<Int>,
choices: IntArray, choices: IntArray,
selected: BooleanArray, selected: BooleanArray,
res: MutableList<List<Int>?> res: MutableList<MutableList<Int>?>
) { ) {
// 當狀態長度等於元素數量時,記錄解 // 當狀態長度等於元素數量時,記錄解
if (state.size == choices.size) { if (state.size == choices.size) {
res.add(ArrayList(state)) res.add(state.toMutableList())
return return
} }
// 走訪所有選擇 // 走訪所有選擇
@@ -36,9 +36,9 @@ fun backtrack(
} }
/* 全排列 I */ /* 全排列 I */
fun permutationsI(nums: IntArray): List<List<Int>?> { fun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {
val res: MutableList<List<Int>?> = ArrayList() val res = mutableListOf<MutableList<Int>?>()
backtrack(ArrayList(), nums, BooleanArray(nums.size), res) backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)
return res return res
} }

View File

@@ -15,11 +15,11 @@ fun backtrack(
) { ) {
// 當狀態長度等於元素數量時,記錄解 // 當狀態長度等於元素數量時,記錄解
if (state.size == choices.size) { if (state.size == choices.size) {
res.add(ArrayList(state)) res.add(state.toMutableList())
return return
} }
// 走訪所有選擇 // 走訪所有選擇
val duplicated: MutableSet<Int> = HashSet() val duplicated = HashSet<Int>()
for (i in choices.indices) { for (i in choices.indices) {
val choice = choices[i] val choice = choices[i]
// 剪枝:不允許重複選擇元素 且 不允許重複選擇相等元素 // 剪枝:不允許重複選擇元素 且 不允許重複選擇相等元素
@@ -39,15 +39,14 @@ fun backtrack(
/* 全排列 II */ /* 全排列 II */
fun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> { fun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {
val res: MutableList<MutableList<Int>?> = ArrayList() val res = mutableListOf<MutableList<Int>?>()
backtrack(ArrayList(), nums, BooleanArray(nums.size), res) backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)
return res return res
} }
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
val nums = intArrayOf(1, 2, 2) val nums = intArrayOf(1, 2, 2)
val res = permutationsII(nums) val res = permutationsII(nums)
println("輸入陣列 nums = ${nums.contentToString()}") println("輸入陣列 nums = ${nums.contentToString()}")

View File

@@ -16,7 +16,7 @@ fun preOrder(root: TreeNode?) {
if (root == null) { if (root == null) {
return return
} }
if (root.value == 7) { if (root._val == 7) {
// 記錄解 // 記錄解
res!!.add(root) res!!.add(root)
} }
@@ -31,13 +31,13 @@ fun main() {
printTree(root) printTree(root)
// 前序走訪 // 前序走訪
res = ArrayList() res = mutableListOf()
preOrder(root) preOrder(root)
println("\n輸出所有值為 7 的節點") println("\n輸出所有值為 7 的節點")
val vals: MutableList<Int> = ArrayList() val vals = mutableListOf<Int>()
for (node in res as ArrayList<TreeNode>) { for (node in res!!) {
vals.add(node.value) vals.add(node._val)
} }
println(vals) println(vals)
} }

View File

@@ -10,7 +10,7 @@ import utils.TreeNode
import utils.printTree import utils.printTree
var path: MutableList<TreeNode>? = null var path: MutableList<TreeNode>? = null
var res: MutableList<List<TreeNode>>? = null var res: MutableList<MutableList<TreeNode>>? = null
/* 前序走訪:例題二 */ /* 前序走訪:例題二 */
fun preOrder(root: TreeNode?) { fun preOrder(root: TreeNode?) {
@@ -19,9 +19,9 @@ fun preOrder(root: TreeNode?) {
} }
// 嘗試 // 嘗試
path!!.add(root) path!!.add(root)
if (root.value == 7) { if (root._val == 7) {
// 記錄解 // 記錄解
res!!.add(ArrayList(path!!)) res!!.add(path!!.toMutableList())
} }
preOrder(root.left) preOrder(root.left)
preOrder(root.right) preOrder(root.right)
@@ -36,16 +36,16 @@ fun main() {
printTree(root) printTree(root)
// 前序走訪 // 前序走訪
path = java.util.ArrayList<TreeNode>() path = mutableListOf()
res = java.util.ArrayList<List<TreeNode>>() res = mutableListOf()
preOrder(root) preOrder(root)
println("\n輸出所有根節點到節點 7 的路徑") println("\n輸出所有根節點到節點 7 的路徑")
for (path in res as ArrayList<List<TreeNode>>) { for (path in res!!) {
val values: MutableList<Int> = ArrayList() val _vals = mutableListOf<Int>()
for (node in path) { for (node in path) {
values.add(node.value) _vals.add(node._val)
} }
println(values) println(_vals)
} }
} }

View File

@@ -10,19 +10,19 @@ import utils.TreeNode
import utils.printTree import utils.printTree
var path: MutableList<TreeNode>? = null var path: MutableList<TreeNode>? = null
var res: MutableList<List<TreeNode>>? = null var res: MutableList<MutableList<TreeNode>>? = null
/* 前序走訪:例題三 */ /* 前序走訪:例題三 */
fun preOrder(root: TreeNode?) { fun preOrder(root: TreeNode?) {
// 剪枝 // 剪枝
if (root == null || root.value == 3) { if (root == null || root._val == 3) {
return return
} }
// 嘗試 // 嘗試
path!!.add(root) path!!.add(root)
if (root.value == 7) { if (root._val == 7) {
// 記錄解 // 記錄解
res!!.add(ArrayList(path!!)) res!!.add(path!!.toMutableList())
} }
preOrder(root.left) preOrder(root.left)
preOrder(root.right) preOrder(root.right)
@@ -37,16 +37,16 @@ fun main() {
printTree(root) printTree(root)
// 前序走訪 // 前序走訪
path = ArrayList() path = mutableListOf()
res = ArrayList() res = mutableListOf()
preOrder(root) preOrder(root)
println("\n輸出所有根節點到節點 7 的路徑,路徑中不包含值為 3 的節點") println("\n輸出所有根節點到節點 7 的路徑,路徑中不包含值為 3 的節點")
for (path in res as ArrayList<List<TreeNode>>) { for (path in res!!) {
val values: MutableList<Int> = ArrayList() val _vals = mutableListOf<Int>()
for (node in path) { for (node in path) {
values.add(node.value) _vals.add(node._val)
} }
println(values) println(_vals)
} }
} }

View File

@@ -8,21 +8,20 @@ package chapter_backtracking.preorder_traversal_iii_template
import utils.TreeNode import utils.TreeNode
import utils.printTree import utils.printTree
import java.util.*
/* 判斷當前狀態是否為解 */ /* 判斷當前狀態是否為解 */
fun isSolution(state: List<TreeNode?>): Boolean { fun isSolution(state: MutableList<TreeNode?>): Boolean {
return state.isNotEmpty() && state[state.size - 1]?.value == 7 return state.isNotEmpty() && state[state.size - 1]?._val == 7
} }
/* 記錄解 */ /* 記錄解 */
fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<List<TreeNode?>?>) { fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {
res.add(state?.let { ArrayList(it) }) res.add(state!!.toMutableList())
} }
/* 判斷在當前狀態下,該選擇是否合法 */ /* 判斷在當前狀態下,該選擇是否合法 */
fun isValid(state: List<TreeNode?>?, choice: TreeNode?): Boolean { fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
return choice != null && choice.value != 3 return choice != null && choice._val != 3
} }
/* 更新狀態 */ /* 更新狀態 */
@@ -38,8 +37,8 @@ fun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
/* 回溯演算法:例題三 */ /* 回溯演算法:例題三 */
fun backtrack( fun backtrack(
state: MutableList<TreeNode?>, state: MutableList<TreeNode?>,
choices: List<TreeNode?>, choices: MutableList<TreeNode?>,
res: MutableList<List<TreeNode?>?> res: MutableList<MutableList<TreeNode?>?>
) { ) {
// 檢查是否為解 // 檢查是否為解
if (isSolution(state)) { if (isSolution(state)) {
@@ -53,7 +52,7 @@ fun backtrack(
// 嘗試:做出選擇,更新狀態 // 嘗試:做出選擇,更新狀態
makeChoice(state, choice) makeChoice(state, choice)
// 進行下一輪選擇 // 進行下一輪選擇
backtrack(state, listOf(choice!!.left, choice.right), res) backtrack(state, mutableListOf(choice!!.left, choice.right), res)
// 回退:撤銷選擇,恢復到之前的狀態 // 回退:撤銷選擇,恢復到之前的狀態
undoChoice(state, choice) undoChoice(state, choice)
} }
@@ -67,15 +66,15 @@ fun main() {
printTree(root) printTree(root)
// 回溯演算法 // 回溯演算法
val res: MutableList<List<TreeNode?>?> = ArrayList() val res = mutableListOf<MutableList<TreeNode?>?>()
backtrack(ArrayList(), mutableListOf(root), res) backtrack(mutableListOf(), mutableListOf(root), res)
println("\n輸出所有根節點到節點 7 的路徑,要求路徑中不包含值為 3 的節點") println("\n輸出所有根節點到節點 7 的路徑,要求路徑中不包含值為 3 的節點")
for (path in res) { for (path in res) {
val vals = ArrayList<Int>() val vals = mutableListOf<Int>()
for (node in path!!) { for (node in path!!) {
if (node != null) { if (node != null) {
vals.add(node.value) vals.add(node._val)
} }
} }
println(vals) println(vals)

View File

@@ -6,19 +6,17 @@
package chapter_backtracking.subset_sum_i package chapter_backtracking.subset_sum_i
import java.util.*
/* 回溯演算法:子集和 I */ /* 回溯演算法:子集和 I */
fun backtrack( fun backtrack(
state: MutableList<Int>, state: MutableList<Int>,
target: Int, target: Int,
choices: IntArray, choices: IntArray,
start: Int, start: Int,
res: MutableList<List<Int>?> res: MutableList<MutableList<Int>?>
) { ) {
// 子集和等於 target 時,記錄解 // 子集和等於 target 時,記錄解
if (target == 0) { if (target == 0) {
res.add(ArrayList(state)) res.add(state.toMutableList())
return return
} }
// 走訪所有選擇 // 走訪所有選擇
@@ -39,11 +37,11 @@ fun backtrack(
} }
/* 求解子集和 I */ /* 求解子集和 I */
fun subsetSumI(nums: IntArray, target: Int): List<List<Int>?> { fun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
val state: MutableList<Int> = ArrayList() // 狀態(子集) val state = mutableListOf<Int>() // 狀態(子集)
Arrays.sort(nums) // 對 nums 進行排序 nums.sort() // 對 nums 進行排序
val start = 0 // 走訪起始點 val start = 0 // 走訪起始點
val res: MutableList<List<Int>?> = ArrayList() // 結果串列(子集串列) val res = mutableListOf<MutableList<Int>?>() // 結果串列(子集串列)
backtrack(state, target, nums, start, res) backtrack(state, target, nums, start, res)
return res return res
} }

View File

@@ -12,11 +12,11 @@ fun backtrack(
target: Int, target: Int,
total: Int, total: Int,
choices: IntArray, choices: IntArray,
res: MutableList<List<Int>?> res: MutableList<MutableList<Int>?>
) { ) {
// 子集和等於 target 時,記錄解 // 子集和等於 target 時,記錄解
if (total == target) { if (total == target) {
res.add(ArrayList(state)) res.add(state.toMutableList())
return return
} }
// 走訪所有選擇 // 走訪所有選擇
@@ -35,10 +35,10 @@ fun backtrack(
} }
/* 求解子集和 I包含重複子集 */ /* 求解子集和 I包含重複子集 */
fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> { fun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
val state: MutableList<Int> = ArrayList() // 狀態(子集) val state = mutableListOf<Int>() // 狀態(子集)
val total = 0 // 子集和 val total = 0 // 子集和
val res: MutableList<List<Int>?> = ArrayList() // 結果串列(子集串列) val res = mutableListOf<MutableList<Int>?>() // 結果串列(子集串列)
backtrack(state, target, total, nums, res) backtrack(state, target, total, nums, res)
return res return res
} }
@@ -47,8 +47,7 @@ fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> {
fun main() { fun main() {
val nums = intArrayOf(3, 4, 5) val nums = intArrayOf(3, 4, 5)
val target = 9 val target = 9
val res = subsetSumINaive(nums, target)
val res: List<List<Int>?> = subsetSumINaive(nums, target)
println("輸入陣列 nums = ${nums.contentToString()}, target = $target") println("輸入陣列 nums = ${nums.contentToString()}, target = $target")
println("所有和等於 $target 的子集 res = $res") println("所有和等於 $target 的子集 res = $res")

View File

@@ -6,19 +6,17 @@
package chapter_backtracking.subset_sum_ii package chapter_backtracking.subset_sum_ii
import java.util.*
/* 回溯演算法:子集和 II */ /* 回溯演算法:子集和 II */
fun backtrack( fun backtrack(
state: MutableList<Int>, state: MutableList<Int>,
target: Int, target: Int,
choices: IntArray, choices: IntArray,
start: Int, start: Int,
res: MutableList<List<Int>?> res: MutableList<MutableList<Int>?>
) { ) {
// 子集和等於 target 時,記錄解 // 子集和等於 target 時,記錄解
if (target == 0) { if (target == 0) {
res.add(ArrayList(state)) res.add(state.toMutableList())
return return
} }
// 走訪所有選擇 // 走訪所有選擇
@@ -44,11 +42,11 @@ fun backtrack(
} }
/* 求解子集和 II */ /* 求解子集和 II */
fun subsetSumII(nums: IntArray, target: Int): List<List<Int>?> { fun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
val state: MutableList<Int> = ArrayList() // 狀態(子集) val state = mutableListOf<Int>() // 狀態(子集)
Arrays.sort(nums) // 對 nums 進行排序 nums.sort() // 對 nums 進行排序
val start = 0 // 走訪起始點 val start = 0 // 走訪起始點
val res: MutableList<List<Int>?> = ArrayList() // 結果串列(子集串列) val res = mutableListOf<MutableList<Int>?>() // 結果串列(子集串列)
backtrack(state, target, nums, start, res) backtrack(state, target, nums, start, res)
return res return res
} }
@@ -57,7 +55,6 @@ fun subsetSumII(nums: IntArray, target: Int): List<List<Int>?> {
fun main() { fun main() {
val nums = intArrayOf(4, 4, 5) val nums = intArrayOf(4, 4, 5)
val target = 9 val target = 9
val res = subsetSumII(nums, target) val res = subsetSumII(nums, target)
println("輸入陣列 nums = ${nums.contentToString()}, target = $target") println("輸入陣列 nums = ${nums.contentToString()}, target = $target")

View File

@@ -74,4 +74,4 @@ fun main() {
res = fib(n) res = fib(n)
println("\n費波那契數列的第 $n 項為 $res") println("\n費波那契數列的第 $n 項為 $res")
} }

View File

@@ -60,9 +60,9 @@ fun linearRecur(n: Int) {
/* 平方階 */ /* 平方階 */
fun quadratic(n: Int) { fun quadratic(n: Int) {
// 矩陣佔用 O(n^2) 空間 // 矩陣佔用 O(n^2) 空間
val numMatrix: Array<Array<Int>?> = arrayOfNulls(n) val numMatrix = arrayOfNulls<Array<Int>?>(n)
// 二維串列佔用 O(n^2) 空間 // 二維串列佔用 O(n^2) 空間
val numList: MutableList<MutableList<Int>> = arrayListOf() val numList = mutableListOf<MutableList<Int>>()
for (i in 0..<n) { for (i in 0..<n) {
val tmp = mutableListOf<Int>() val tmp = mutableListOf<Int>()
for (j in 0..<n) { for (j in 0..<n) {
@@ -104,6 +104,6 @@ fun main() {
quadratic(n) quadratic(n)
quadraticRecur(n) quadraticRecur(n)
// 指數階 // 指數階
val root: TreeNode? = buildTree(n) val root = buildTree(n)
printTree(root) printTree(root)
} }

View File

@@ -9,7 +9,7 @@ package chapter_computational_complexity.time_complexity
/* 常數階 */ /* 常數階 */
fun constant(n: Int): Int { fun constant(n: Int): Int {
var count = 0 var count = 0
val size = 10_0000 val size = 100000
for (i in 0..<size) for (i in 0..<size)
count++ count++
return count return count
@@ -48,7 +48,7 @@ fun quadratic(n: Int): Int {
/* 平方階(泡沫排序) */ /* 平方階(泡沫排序) */
fun bubbleSort(nums: IntArray): Int { fun bubbleSort(nums: IntArray): Int {
var count = 0 var count = 0 // 計數器
// 外迴圈:未排序區間為 [0, i] // 外迴圈:未排序區間為 [0, i]
for (i in nums.size - 1 downTo 1) { for (i in nums.size - 1 downTo 1) {
// 內迴圈:將未排序區間 [0, i] 中的最大元素交換至該區間的最右端 // 內迴圈:將未排序區間 [0, i] 中的最大元素交換至該區間的最右端
@@ -109,7 +109,7 @@ fun linearLogRecur(n: Int): Int {
if (n <= 1) if (n <= 1)
return 1 return 1
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2) var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
for (i in 0..<n.toInt()) { for (i in 0..<n) {
count++ count++
} }
return count return count
@@ -133,7 +133,7 @@ fun main() {
val n = 8 val n = 8
println("輸入資料大小 n = $n") println("輸入資料大小 n = $n")
var count: Int = constant(n) var count = constant(n)
println("常數階的操作數量 = $count") println("常數階的操作數量 = $count")
count = linear(n) count = linear(n)
@@ -144,7 +144,8 @@ fun main() {
count = quadratic(n) count = quadratic(n)
println("平方階的操作數量 = $count") println("平方階的操作數量 = $count")
val nums = IntArray(n) val nums = IntArray(n)
for (i in 0..<n) nums[i] = n - i // [n,n-1,...,2,1] for (i in 0..<n)
nums[i] = n - i // [n,n-1,...,2,1]
count = bubbleSort(nums) count = bubbleSort(nums)
println("平方階(泡沫排序)的操作數量 = $count") println("平方階(泡沫排序)的操作數量 = $count")

View File

@@ -13,10 +13,9 @@ fun randomNumbers(n: Int): Array<Int?> {
for (i in 0..<n) { for (i in 0..<n) {
nums[i] = i + 1 nums[i] = i + 1
} }
// 隨機打亂陣列元素
val mutableList = nums.toMutableList() val mutableList = nums.toMutableList()
// 隨機打亂陣列元素
mutableList.shuffle() mutableList.shuffle()
// Integer[] -> int[]
val res = arrayOfNulls<Int>(n) val res = arrayOfNulls<Int>(n)
for (i in 0..<n) { for (i in 0..<n) {
res[i] = mutableList[i] res[i] = mutableList[i]
@@ -39,8 +38,8 @@ fun findOne(nums: Array<Int?>): Int {
fun main() { fun main() {
for (i in 0..9) { for (i in 0..9) {
val n = 100 val n = 100
val nums: Array<Int?> = randomNumbers(n) val nums = randomNumbers(n)
val index: Int = findOne(nums) val index = findOne(nums)
println("\n陣列 [ 1, 2, ..., n ] 被打亂後 = ${nums.contentToString()}") println("\n陣列 [ 1, 2, ..., n ] 被打亂後 = ${nums.contentToString()}")
println("數字 1 的索引為 $index") println("數字 1 的索引為 $index")
} }

View File

@@ -10,7 +10,13 @@ import utils.TreeNode
import utils.printTree import utils.printTree
/* 構建二元樹:分治 */ /* 構建二元樹:分治 */
fun dfs(preorder: IntArray, inorderMap: Map<Int?, Int?>, i: Int, l: Int, r: Int): TreeNode? { fun dfs(
preorder: IntArray,
inorderMap: Map<Int?, Int?>,
i: Int,
l: Int,
r: Int
): TreeNode? {
// 子樹區間為空時終止 // 子樹區間為空時終止
if (r - l < 0) return null if (r - l < 0) return null
// 初始化根節點 // 初始化根節點
@@ -28,7 +34,7 @@ fun dfs(preorder: IntArray, inorderMap: Map<Int?, Int?>, i: Int, l: Int, r: Int)
/* 構建二元樹 */ /* 構建二元樹 */
fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? { fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
// 初始化雜湊表,儲存 inorder 元素到索引的對映 // 初始化雜湊表,儲存 inorder 元素到索引的對映
val inorderMap: MutableMap<Int?, Int?> = HashMap() val inorderMap = HashMap<Int?, Int?>()
for (i in inorder.indices) { for (i in inorder.indices) {
inorderMap[inorder[i]] = i inorderMap[inorder[i]] = i
} }
@@ -40,8 +46,8 @@ fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
fun main() { fun main() {
val preorder = intArrayOf(3, 9, 2, 1, 7) val preorder = intArrayOf(3, 9, 2, 1, 7)
val inorder = intArrayOf(9, 3, 1, 2, 7) val inorder = intArrayOf(9, 3, 1, 2, 7)
println("前序走訪 = " + preorder.contentToString()) println("前序走訪 = ${preorder.contentToString()}")
println("中序走訪 = " + inorder.contentToString()) println("中序走訪 = ${inorder.contentToString()}")
val root = buildTree(preorder, inorder) val root = buildTree(preorder, inorder)
println("構建的二元樹為:") println("構建的二元樹為:")

View File

@@ -9,7 +9,7 @@ package chapter_divide_and_conquer.hanota
/* 移動一個圓盤 */ /* 移動一個圓盤 */
fun move(src: MutableList<Int>, tar: MutableList<Int>) { fun move(src: MutableList<Int>, tar: MutableList<Int>) {
// 從 src 頂部拿出一個圓盤 // 從 src 頂部拿出一個圓盤
val pan: Int = src.removeAt(src.size - 1) val pan = src.removeAt(src.size - 1)
// 將圓盤放入 tar 頂部 // 將圓盤放入 tar 頂部
tar.add(pan) tar.add(pan)
} }
@@ -39,9 +39,9 @@ fun solveHanota(A: MutableList<Int>, B: MutableList<Int>, C: MutableList<Int>) {
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
// 串列尾部是柱子頂部 // 串列尾部是柱子頂部
val A: MutableList<Int> = ArrayList(mutableListOf(5, 4, 3, 2, 1)) val A = mutableListOf(5, 4, 3, 2, 1)
val B: MutableList<Int> = ArrayList() val B = mutableListOf<Int>()
val C: MutableList<Int> = ArrayList() val C = mutableListOf<Int>()
println("初始狀態下:") println("初始狀態下:")
println("A = $A") println("A = $A")
println("B = $B") println("B = $B")
@@ -53,4 +53,4 @@ fun main() {
println("A = $A") println("A = $A")
println("B = $B") println("B = $B")
println("C = $C") println("C = $C")
} }

View File

@@ -8,13 +8,14 @@ package chapter_dynamic_programming
/* 回溯 */ /* 回溯 */
fun backtrack( fun backtrack(
choices: List<Int>, choices: MutableList<Int>,
state: Int, state: Int,
n: Int, n: Int,
res: MutableList<Int> res: MutableList<Int>
) { ) {
// 當爬到第 n 階時,方案數量加 1 // 當爬到第 n 階時,方案數量加 1
if (state == n) res[0] = res[0] + 1 if (state == n)
res[0] = res[0] + 1
// 走訪所有選擇 // 走訪所有選擇
for (choice in choices) { for (choice in choices) {
// 剪枝:不允許越過第 n 階 // 剪枝:不允許越過第 n 階
@@ -29,7 +30,7 @@ fun backtrack(
fun climbingStairsBacktrack(n: Int): Int { fun climbingStairsBacktrack(n: Int): Int {
val choices = mutableListOf(1, 2) // 可選擇向上爬 1 階或 2 階 val choices = mutableListOf(1, 2) // 可選擇向上爬 1 階或 2 階
val state = 0 // 從第 0 階開始爬 val state = 0 // 從第 0 階開始爬
val res = ArrayList<Int>() val res = mutableListOf<Int>()
res.add(0) // 使用 res[0] 記錄方案數量 res.add(0) // 使用 res[0] 記錄方案數量
backtrack(choices, state, n, res) backtrack(choices, state, n, res)
return res[0] return res[0]

View File

@@ -6,8 +6,6 @@
package chapter_dynamic_programming package chapter_dynamic_programming
import java.util.*
/* 記憶化搜尋 */ /* 記憶化搜尋 */
fun dfs(i: Int, mem: IntArray): Int { fun dfs(i: Int, mem: IntArray): Int {
// 已知 dp[1] 和 dp[2] ,返回之 // 已知 dp[1] 和 dp[2] ,返回之
@@ -25,7 +23,7 @@ fun dfs(i: Int, mem: IntArray): Int {
fun climbingStairsDFSMem(n: Int): Int { fun climbingStairsDFSMem(n: Int): Int {
// mem[i] 記錄爬到第 i 階的方案總數,-1 代表無記錄 // mem[i] 記錄爬到第 i 階的方案總數,-1 代表無記錄
val mem = IntArray(n + 1) val mem = IntArray(n + 1)
Arrays.fill(mem, -1) mem.fill(-1)
return dfs(n, mem) return dfs(n, mem)
} }
@@ -33,6 +31,6 @@ fun climbingStairsDFSMem(n: Int): Int {
fun main() { fun main() {
val n = 9 val n = 9
val res: Int = climbingStairsDFSMem(n) val res = climbingStairsDFSMem(n)
println("$n 階樓梯共有 $res 種方案") println("$n 階樓梯共有 $res 種方案")
} }

View File

@@ -27,9 +27,7 @@ fun climbingStairsDPComp(n: Int): Int {
var a = 1 var a = 1
var b = 2 var b = 2
for (i in 3..n) { for (i in 3..n) {
val tmp = b b += a.also { a = b }
b += a
a = tmp
} }
return b return b
} }

View File

@@ -6,7 +6,6 @@
package chapter_dynamic_programming package chapter_dynamic_programming
import java.util.*
import kotlin.math.min import kotlin.math.min
/* 零錢兌換:動態規劃 */ /* 零錢兌換:動態規劃 */
@@ -27,8 +26,7 @@ fun coinChangeDP(coins: IntArray, amt: Int): Int {
dp[i][a] = dp[i - 1][a] dp[i][a] = dp[i - 1][a]
} else { } else {
// 不選和選硬幣 i 這兩種方案的較小值 // 不選和選硬幣 i 這兩種方案的較小值
dp[i][a] = min(dp[i - 1][a].toDouble(), (dp[i][a - coins[i - 1]] + 1).toDouble()) dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)
.toInt()
} }
} }
} }
@@ -41,7 +39,7 @@ fun coinChangeDPComp(coins: IntArray, amt: Int): Int {
val MAX = amt + 1 val MAX = amt + 1
// 初始化 dp 表 // 初始化 dp 表
val dp = IntArray(amt + 1) val dp = IntArray(amt + 1)
Arrays.fill(dp, MAX) dp.fill(MAX)
dp[0] = 0 dp[0] = 0
// 狀態轉移 // 狀態轉移
for (i in 1..n) { for (i in 1..n) {
@@ -51,7 +49,7 @@ fun coinChangeDPComp(coins: IntArray, amt: Int): Int {
dp[a] = dp[a] dp[a] = dp[a]
} else { } else {
// 不選和選硬幣 i 這兩種方案的較小值 // 不選和選硬幣 i 這兩種方案的較小值
dp[a] = min(dp[a].toDouble(), (dp[a - coins[i - 1]] + 1).toDouble()).toInt() dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)
} }
} }
} }

View File

@@ -6,7 +6,6 @@
package chapter_dynamic_programming package chapter_dynamic_programming
import java.util.*
import kotlin.math.min import kotlin.math.min
/* 編輯距離:暴力搜尋 */ /* 編輯距離:暴力搜尋 */
@@ -29,7 +28,7 @@ fun editDistanceDFS(
val delete = editDistanceDFS(s, t, i - 1, j) val delete = editDistanceDFS(s, t, i - 1, j)
val replace = editDistanceDFS(s, t, i - 1, j - 1) val replace = editDistanceDFS(s, t, i - 1, j - 1)
// 返回最少編輯步數 // 返回最少編輯步數
return (min(min(insert.toDouble(), delete.toDouble()), replace.toDouble()) + 1).toInt() return min(min(insert, delete), replace) + 1
} }
/* 編輯距離:記憶化搜尋 */ /* 編輯距離:記憶化搜尋 */
@@ -55,7 +54,7 @@ fun editDistanceDFSMem(
val delete = editDistanceDFSMem(s, t, mem, i - 1, j) val delete = editDistanceDFSMem(s, t, mem, i - 1, j)
val replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1) val replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1)
// 記錄並返回最少編輯步數 // 記錄並返回最少編輯步數
mem[i][j] = (min(min(insert.toDouble(), delete.toDouble()), replace.toDouble()) + 1).toInt() mem[i][j] = min(min(insert, delete), replace) + 1
return mem[i][j] return mem[i][j]
} }
@@ -79,11 +78,7 @@ fun editDistanceDP(s: String, t: String): Int {
dp[i][j] = dp[i - 1][j - 1] dp[i][j] = dp[i - 1][j - 1]
} else { } else {
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1 // 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
dp[i][j] = dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1
(min(
min(dp[i][j - 1].toDouble(), dp[i - 1][j].toDouble()),
dp[i - 1][j - 1].toDouble()
) + 1).toInt()
} }
} }
} }
@@ -112,7 +107,7 @@ fun editDistanceDPComp(s: String, t: String): Int {
dp[j] = leftup dp[j] = leftup
} else { } else {
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1 // 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
dp[j] = (min(min(dp[j - 1].toDouble(), dp[j].toDouble()), leftup.toDouble()) + 1).toInt() dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1
} }
leftup = temp // 更新為下一輪的 dp[i-1, j-1] leftup = temp // 更新為下一輪的 dp[i-1, j-1]
} }
@@ -133,7 +128,8 @@ fun main() {
// 記憶化搜尋 // 記憶化搜尋
val mem = Array(n + 1) { IntArray(m + 1) } val mem = Array(n + 1) { IntArray(m + 1) }
for (row in mem) Arrays.fill(row, -1) for (row in mem)
row.fill(-1)
res = editDistanceDFSMem(s, t, mem, n, m) res = editDistanceDFSMem(s, t, mem, n, m)
println("$s 更改為 $t 最少需要編輯 $res") println("$s 更改為 $t 最少需要編輯 $res")

View File

@@ -6,13 +6,12 @@
package chapter_dynamic_programming package chapter_dynamic_programming
import java.util.*
import kotlin.math.max import kotlin.math.max
/* 0-1 背包:暴力搜尋 */ /* 0-1 背包:暴力搜尋 */
fun knapsackDFS( fun knapsackDFS(
wgt: IntArray, wgt: IntArray,
value: IntArray, _val: IntArray,
i: Int, i: Int,
c: Int c: Int
): Int { ): Int {
@@ -22,19 +21,19 @@ fun knapsackDFS(
} }
// 若超過背包容量,則只能選擇不放入背包 // 若超過背包容量,則只能選擇不放入背包
if (wgt[i - 1] > c) { if (wgt[i - 1] > c) {
return knapsackDFS(wgt, value, i - 1, c) return knapsackDFS(wgt, _val, i - 1, c)
} }
// 計算不放入和放入物品 i 的最大價值 // 計算不放入和放入物品 i 的最大價值
val no = knapsackDFS(wgt, value, i - 1, c) val no = knapsackDFS(wgt, _val, i - 1, c)
val yes = knapsackDFS(wgt, value, i - 1, c - wgt[i - 1]) + value[i - 1] val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 返回兩種方案中價值更大的那一個 // 返回兩種方案中價值更大的那一個
return max(no.toDouble(), yes.toDouble()).toInt() return max(no, yes)
} }
/* 0-1 背包:記憶化搜尋 */ /* 0-1 背包:記憶化搜尋 */
fun knapsackDFSMem( fun knapsackDFSMem(
wgt: IntArray, wgt: IntArray,
value: IntArray, _val: IntArray,
mem: Array<IntArray>, mem: Array<IntArray>,
i: Int, i: Int,
c: Int c: Int
@@ -49,20 +48,20 @@ fun knapsackDFSMem(
} }
// 若超過背包容量,則只能選擇不放入背包 // 若超過背包容量,則只能選擇不放入背包
if (wgt[i - 1] > c) { if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, value, mem, i - 1, c) return knapsackDFSMem(wgt, _val, mem, i - 1, c)
} }
// 計算不放入和放入物品 i 的最大價值 // 計算不放入和放入物品 i 的最大價值
val no = knapsackDFSMem(wgt, value, mem, i - 1, c) val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)
val yes = knapsackDFSMem(wgt, value, mem, i - 1, c - wgt[i - 1]) + value[i - 1] val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 記錄並返回兩種方案中價值更大的那一個 // 記錄並返回兩種方案中價值更大的那一個
mem[i][c] = max(no.toDouble(), yes.toDouble()).toInt() mem[i][c] = max(no, yes)
return mem[i][c] return mem[i][c]
} }
/* 0-1 背包:動態規劃 */ /* 0-1 背包:動態規劃 */
fun knapsackDP( fun knapsackDP(
wgt: IntArray, wgt: IntArray,
value: IntArray, _val: IntArray,
cap: Int cap: Int
): Int { ): Int {
val n = wgt.size val n = wgt.size
@@ -76,8 +75,7 @@ fun knapsackDP(
dp[i][c] = dp[i - 1][c] dp[i][c] = dp[i - 1][c]
} else { } else {
// 不選和選物品 i 這兩種方案的較大值 // 不選和選物品 i 這兩種方案的較大值
dp[i][c] = max(dp[i - 1][c].toDouble(), (dp[i - 1][c - wgt[i - 1]] + value[i - 1]).toDouble()) dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])
.toInt()
} }
} }
} }
@@ -87,7 +85,7 @@ fun knapsackDP(
/* 0-1 背包:空間最佳化後的動態規劃 */ /* 0-1 背包:空間最佳化後的動態規劃 */
fun knapsackDPComp( fun knapsackDPComp(
wgt: IntArray, wgt: IntArray,
value: IntArray, _val: IntArray,
cap: Int cap: Int
): Int { ): Int {
val n = wgt.size val n = wgt.size
@@ -100,7 +98,7 @@ fun knapsackDPComp(
if (wgt[i - 1] <= c) { if (wgt[i - 1] <= c) {
// 不選和選物品 i 這兩種方案的較大值 // 不選和選物品 i 這兩種方案的較大值
dp[c] = dp[c] =
max(dp[c].toDouble(), (dp[c - wgt[i - 1]] + value[i - 1]).toDouble()).toInt() max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
} }
} }
} }
@@ -110,27 +108,27 @@ fun knapsackDPComp(
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50) val wgt = intArrayOf(10, 20, 30, 40, 50)
val value = intArrayOf(50, 120, 150, 210, 240) val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50 val cap = 50
val n = wgt.size val n = wgt.size
// 暴力搜尋 // 暴力搜尋
var res = knapsackDFS(wgt, value, n, cap) var res = knapsackDFS(wgt, _val, n, cap)
println("不超過背包容量的最大物品價值為 $res") println("不超過背包容量的最大物品價值為 $res")
// 記憶化搜尋 // 記憶化搜尋
val mem = Array(n + 1) { IntArray(cap + 1) } val mem = Array(n + 1) { IntArray(cap + 1) }
for (row in mem) { for (row in mem) {
Arrays.fill(row, -1) row.fill(-1)
} }
res = knapsackDFSMem(wgt, value, mem, n, cap) res = knapsackDFSMem(wgt, _val, mem, n, cap)
println("不超過背包容量的最大物品價值為 $res") println("不超過背包容量的最大物品價值為 $res")
// 動態規劃 // 動態規劃
res = knapsackDP(wgt, value, cap) res = knapsackDP(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res") println("不超過背包容量的最大物品價值為 $res")
// 空間最佳化後的動態規劃 // 空間最佳化後的動態規劃
res = knapsackDPComp(wgt, value, cap) res = knapsackDPComp(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res") println("不超過背包容量的最大物品價值為 $res")
} }

View File

@@ -19,7 +19,7 @@ fun minCostClimbingStairsDP(cost: IntArray): Int {
dp[2] = cost[2] dp[2] = cost[2]
// 狀態轉移:從較小子問題逐步求解較大子問題 // 狀態轉移:從較小子問題逐步求解較大子問題
for (i in 3..n) { for (i in 3..n) {
dp[i] = (min(dp[i - 1].toDouble(), dp[i - 2].toDouble()) + cost[i]).toInt() dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
} }
return dp[n] return dp[n]
} }
@@ -32,7 +32,7 @@ fun minCostClimbingStairsDPComp(cost: IntArray): Int {
var b = cost[2] var b = cost[2]
for (i in 3..n) { for (i in 3..n) {
val tmp = b val tmp = b
b = (min(a.toDouble(), tmp.toDouble()) + cost[i]).toInt() b = min(a, tmp) + cost[i]
a = tmp a = tmp
} }
return b return b

View File

@@ -6,15 +6,10 @@
package chapter_dynamic_programming package chapter_dynamic_programming
import java.util.*
import kotlin.math.min import kotlin.math.min
/* 最小路徑和:暴力搜尋 */ /* 最小路徑和:暴力搜尋 */
fun minPathSumDFS( fun minPathSumDFS(grid: Array<IntArray>, i: Int, j: Int): Int {
grid: Array<Array<Int>>,
i: Int,
j: Int
): Int {
// 若為左上角單元格,則終止搜尋 // 若為左上角單元格,則終止搜尋
if (i == 0 && j == 0) { if (i == 0 && j == 0) {
return grid[0][0] return grid[0][0]
@@ -27,13 +22,13 @@ fun minPathSumDFS(
val up = minPathSumDFS(grid, i - 1, j) val up = minPathSumDFS(grid, i - 1, j)
val left = minPathSumDFS(grid, i, j - 1) val left = minPathSumDFS(grid, i, j - 1)
// 返回從左上角到 (i, j) 的最小路徑代價 // 返回從左上角到 (i, j) 的最小路徑代價
return (min(left.toDouble(), up.toDouble()) + grid[i][j]).toInt() return min(left, up) + grid[i][j]
} }
/* 最小路徑和:記憶化搜尋 */ /* 最小路徑和:記憶化搜尋 */
fun minPathSumDFSMem( fun minPathSumDFSMem(
grid: Array<Array<Int>>, grid: Array<IntArray>,
mem: Array<Array<Int>>, mem: Array<IntArray>,
i: Int, i: Int,
j: Int j: Int
): Int { ): Int {
@@ -53,12 +48,12 @@ fun minPathSumDFSMem(
val up = minPathSumDFSMem(grid, mem, i - 1, j) val up = minPathSumDFSMem(grid, mem, i - 1, j)
val left = minPathSumDFSMem(grid, mem, i, j - 1) val left = minPathSumDFSMem(grid, mem, i, j - 1)
// 記錄並返回左上角到 (i, j) 的最小路徑代價 // 記錄並返回左上角到 (i, j) 的最小路徑代價
mem[i][j] = (min(left.toDouble(), up.toDouble()) + grid[i][j]).toInt() mem[i][j] = min(left, up) + grid[i][j]
return mem[i][j] return mem[i][j]
} }
/* 最小路徑和:動態規劃 */ /* 最小路徑和:動態規劃 */
fun minPathSumDP(grid: Array<Array<Int>>): Int { fun minPathSumDP(grid: Array<IntArray>): Int {
val n = grid.size val n = grid.size
val m = grid[0].size val m = grid[0].size
// 初始化 dp 表 // 初始化 dp 表
@@ -75,15 +70,14 @@ fun minPathSumDP(grid: Array<Array<Int>>): Int {
// 狀態轉移:其餘行和列 // 狀態轉移:其餘行和列
for (i in 1..<n) { for (i in 1..<n) {
for (j in 1..<m) { for (j in 1..<m) {
dp[i][j] = dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]
(min(dp[i][j - 1].toDouble(), dp[i - 1][j].toDouble()) + grid[i][j]).toInt()
} }
} }
return dp[n - 1][m - 1] return dp[n - 1][m - 1]
} }
/* 最小路徑和:空間最佳化後的動態規劃 */ /* 最小路徑和:空間最佳化後的動態規劃 */
fun minPathSumDPComp(grid: Array<Array<Int>>): Int { fun minPathSumDPComp(grid: Array<IntArray>): Int {
val n = grid.size val n = grid.size
val m = grid[0].size val m = grid[0].size
// 初始化 dp 表 // 初始化 dp 表
@@ -99,7 +93,7 @@ fun minPathSumDPComp(grid: Array<Array<Int>>): Int {
dp[0] = dp[0] + grid[i][0] dp[0] = dp[0] + grid[i][0]
// 狀態轉移:其餘列 // 狀態轉移:其餘列
for (j in 1..<m) { for (j in 1..<m) {
dp[j] = (min(dp[j - 1].toDouble(), dp[j].toDouble()) + grid[i][j]).toInt() dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]
} }
} }
return dp[m - 1] return dp[m - 1]
@@ -108,10 +102,10 @@ fun minPathSumDPComp(grid: Array<Array<Int>>): Int {
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
val grid = arrayOf( val grid = arrayOf(
arrayOf(1, 3, 1, 5), intArrayOf(1, 3, 1, 5),
arrayOf(2, 2, 4, 2), intArrayOf(2, 2, 4, 2),
arrayOf(5, 3, 2, 1), intArrayOf(5, 3, 2, 1),
arrayOf(4, 3, 5, 2) intArrayOf(4, 3, 5, 2)
) )
val n = grid.size val n = grid.size
val m = grid[0].size val m = grid[0].size
@@ -121,9 +115,9 @@ fun main() {
println("從左上角到右下角的最小路徑和為 $res") println("從左上角到右下角的最小路徑和為 $res")
// 記憶化搜尋 // 記憶化搜尋
val mem = Array(n) { Array(m) { 0 } } val mem = Array(n) { IntArray(m) }
for (row in mem) { for (row in mem) {
Arrays.fill(row, -1) row.fill(-1)
} }
res = minPathSumDFSMem(grid, mem, n - 1, m - 1) res = minPathSumDFSMem(grid, mem, n - 1, m - 1)
println("從左上角到右下角的最小路徑和為 $res") println("從左上角到右下角的最小路徑和為 $res")

View File

@@ -9,11 +9,7 @@ package chapter_dynamic_programming
import kotlin.math.max import kotlin.math.max
/* 完全背包:動態規劃 */ /* 完全背包:動態規劃 */
fun unboundedKnapsackDP( fun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
wgt: IntArray,
value: IntArray,
cap: Int
): Int {
val n = wgt.size val n = wgt.size
// 初始化 dp 表 // 初始化 dp 表
val dp = Array(n + 1) { IntArray(cap + 1) } val dp = Array(n + 1) { IntArray(cap + 1) }
@@ -25,8 +21,7 @@ fun unboundedKnapsackDP(
dp[i][c] = dp[i - 1][c] dp[i][c] = dp[i - 1][c]
} else { } else {
// 不選和選物品 i 這兩種方案的較大值 // 不選和選物品 i 這兩種方案的較大值
dp[i][c] = max(dp[i - 1][c].toDouble(), (dp[i][c - wgt[i - 1]] + value[i - 1]).toDouble()) dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])
.toInt()
} }
} }
} }
@@ -36,7 +31,7 @@ fun unboundedKnapsackDP(
/* 完全背包:空間最佳化後的動態規劃 */ /* 完全背包:空間最佳化後的動態規劃 */
fun unboundedKnapsackDPComp( fun unboundedKnapsackDPComp(
wgt: IntArray, wgt: IntArray,
value: IntArray, _val: IntArray,
cap: Int cap: Int
): Int { ): Int {
val n = wgt.size val n = wgt.size
@@ -50,8 +45,7 @@ fun unboundedKnapsackDPComp(
dp[c] = dp[c] dp[c] = dp[c]
} else { } else {
// 不選和選物品 i 這兩種方案的較大值 // 不選和選物品 i 這兩種方案的較大值
dp[c] = dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
max(dp[c].toDouble(), (dp[c - wgt[i - 1]] + value[i - 1]).toDouble()).toInt()
} }
} }
} }
@@ -61,14 +55,14 @@ fun unboundedKnapsackDPComp(
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
val wgt = intArrayOf(1, 2, 3) val wgt = intArrayOf(1, 2, 3)
val value = intArrayOf(5, 11, 15) val _val = intArrayOf(5, 11, 15)
val cap = 4 val cap = 4
// 動態規劃 // 動態規劃
var res = unboundedKnapsackDP(wgt, value, cap) var res = unboundedKnapsackDP(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res") println("不超過背包容量的最大物品價值為 $res")
// 空間最佳化後的動態規劃 // 空間最佳化後的動態規劃
res = unboundedKnapsackDPComp(wgt, value, cap) res = unboundedKnapsackDPComp(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res") println("不超過背包容量的最大物品價值為 $res")
} }

View File

@@ -11,7 +11,7 @@ import utils.Vertex
/* 基於鄰接表實現的無向圖類別 */ /* 基於鄰接表實現的無向圖類別 */
class GraphAdjList(edges: Array<Array<Vertex?>>) { class GraphAdjList(edges: Array<Array<Vertex?>>) {
// 鄰接表key頂點value該頂點的所有鄰接頂點 // 鄰接表key頂點value該頂點的所有鄰接頂點
val adjList: MutableMap<Vertex, MutableList<Vertex>> = HashMap() val adjList = HashMap<Vertex, MutableList<Vertex>>()
/* 建構子 */ /* 建構子 */
init { init {
@@ -70,11 +70,11 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
fun print() { fun print() {
println("鄰接表 =") println("鄰接表 =")
for (pair in adjList.entries) { for (pair in adjList.entries) {
val tmp = ArrayList<Int>() val tmp = mutableListOf<Int>()
for (vertex in pair.value) { for (vertex in pair.value) {
tmp.add(vertex.value) tmp.add(vertex._val)
} }
println("${pair.key.value}: $tmp,") println("${pair.key._val}: $tmp,")
} }
} }
} }
@@ -82,7 +82,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
/* 初始化無向圖 */ /* 初始化無向圖 */
val v: Array<Vertex?> = Vertex.valsToVets(intArrayOf(1, 3, 2, 5, 4)) val v = Vertex.valsToVets(intArrayOf(1, 3, 2, 5, 4))
val edges = arrayOf( val edges = arrayOf(
arrayOf(v[0], v[1]), arrayOf(v[0], v[1]),
arrayOf(v[0], v[3]), arrayOf(v[0], v[3]),

View File

@@ -10,8 +10,8 @@ import utils.printMatrix
/* 基於鄰接矩陣實現的無向圖類別 */ /* 基於鄰接矩陣實現的無向圖類別 */
class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) { class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
val vertices: MutableList<Int> = ArrayList() // 頂點串列,元素代表“頂點值”,索引代表“頂點索引” val vertices = mutableListOf<Int>() // 頂點串列,元素代表“頂點值”,索引代表“頂點索引”
val adjMat: MutableList<MutableList<Int>> = ArrayList() // 鄰接矩陣,行列索引對應“頂點索引” val adjMat = mutableListOf<MutableList<Int>>() // 鄰接矩陣,行列索引對應“頂點索引”
/* 建構子 */ /* 建構子 */
init { init {
@@ -32,12 +32,12 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
} }
/* 新增頂點 */ /* 新增頂點 */
fun addVertex(value: Int) { fun addVertex(_val: Int) {
val n = size() val n = size()
// 向頂點串列中新增新頂點的值 // 向頂點串列中新增新頂點的值
vertices.add(value) vertices.add(_val)
// 在鄰接矩陣中新增一行 // 在鄰接矩陣中新增一行
val newRow: MutableList<Int> = mutableListOf() val newRow = mutableListOf<Int>()
for (j in 0..<n) { for (j in 0..<n) {
newRow.add(0) newRow.add(0)
} }
@@ -50,7 +50,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
/* 刪除頂點 */ /* 刪除頂點 */
fun removeVertex(index: Int) { fun removeVertex(index: Int) {
if (index >= size()) throw IndexOutOfBoundsException() if (index >= size())
throw IndexOutOfBoundsException()
// 在頂點串列中移除索引 index 的頂點 // 在頂點串列中移除索引 index 的頂點
vertices.removeAt(index) vertices.removeAt(index)
// 在鄰接矩陣中刪除索引 index 的行 // 在鄰接矩陣中刪除索引 index 的行
@@ -65,7 +66,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
// 參數 i, j 對應 vertices 元素索引 // 參數 i, j 對應 vertices 元素索引
fun addEdge(i: Int, j: Int) { fun addEdge(i: Int, j: Int) {
// 索引越界與相等處理 // 索引越界與相等處理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException() if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw IndexOutOfBoundsException()
// 在無向圖中,鄰接矩陣關於主對角線對稱,即滿足 (i, j) == (j, i) // 在無向圖中,鄰接矩陣關於主對角線對稱,即滿足 (i, j) == (j, i)
adjMat[i][j] = 1; adjMat[i][j] = 1;
adjMat[j][i] = 1; adjMat[j][i] = 1;
@@ -75,7 +77,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
// 參數 i, j 對應 vertices 元素索引 // 參數 i, j 對應 vertices 元素索引
fun removeEdge(i: Int, j: Int) { fun removeEdge(i: Int, j: Int) {
// 索引越界與相等處理 // 索引越界與相等處理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException() if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw IndexOutOfBoundsException()
adjMat[i][j] = 0; adjMat[i][j] = 0;
adjMat[j][i] = 0; adjMat[j][i] = 0;
} }

View File

@@ -11,24 +11,24 @@ import java.util.*
/* 廣度優先走訪 */ /* 廣度優先走訪 */
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點 // 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
fun graphBFS(graph: GraphAdjList, startVet: Vertex): List<Vertex> { fun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {
// 頂點走訪序列 // 頂點走訪序列
val res: MutableList<Vertex> = ArrayList() val res = mutableListOf<Vertex?>()
// 雜湊表,用於記錄已被訪問過的頂點 // 雜湊表,用於記錄已被訪問過的頂點
val visited: MutableSet<Vertex> = HashSet() val visited = HashSet<Vertex>()
visited.add(startVet) visited.add(startVet)
// 佇列用於實現 BFS // 佇列用於實現 BFS
val que: Queue<Vertex> = LinkedList() val que = LinkedList<Vertex>()
que.offer(startVet) que.offer(startVet)
// 以頂點 vet 為起點,迴圈直至訪問完所有頂點 // 以頂點 vet 為起點,迴圈直至訪問完所有頂點
while (!que.isEmpty()) { while (!que.isEmpty()) {
val vet = que.poll() // 佇列首頂點出隊 val vet = que.poll() // 佇列首頂點出隊
res.add(vet) // 記錄訪問頂點 res.add(vet) // 記錄訪問頂點
// 走訪該頂點的所有鄰接頂點 // 走訪該頂點的所有鄰接頂點
for (adjVet in graph.adjList[vet]!!) { for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet)) continue // 跳過已被訪問的頂點 if (visited.contains(adjVet))
continue // 跳過已被訪問的頂點
que.offer(adjVet) // 只入列未訪問的頂點 que.offer(adjVet) // 只入列未訪問的頂點
visited.add(adjVet) // 標記該頂點已被訪問 visited.add(adjVet) // 標記該頂點已被訪問
} }
} }

View File

@@ -15,11 +15,12 @@ fun dfs(
res: MutableList<Vertex?>, res: MutableList<Vertex?>,
vet: Vertex? vet: Vertex?
) { ) {
res.add(vet) // 記錄訪問頂點 res.add(vet) // 記錄訪問頂點
visited.add(vet) // 標記該頂點已被訪問 visited.add(vet) // 標記該頂點已被訪問
// 走訪該頂點的所有鄰接頂點 // 走訪該頂點的所有鄰接頂點
for (adjVet in graph.adjList[vet]!!) { for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet)) continue // 跳過已被訪問的頂點 if (visited.contains(adjVet))
continue // 跳過已被訪問的頂點
// 遞迴訪問鄰接頂點 // 遞迴訪問鄰接頂點
dfs(graph, visited, res, adjVet) dfs(graph, visited, res, adjVet)
} }
@@ -27,14 +28,11 @@ fun dfs(
/* 深度優先走訪 */ /* 深度優先走訪 */
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點 // 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
fun graphDFS( fun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {
graph: GraphAdjList,
startVet: Vertex?
): List<Vertex?> {
// 頂點走訪序列 // 頂點走訪序列
val res: MutableList<Vertex?> = ArrayList() val res = mutableListOf<Vertex?>()
// 雜湊表,用於記錄已被訪問過的頂點 // 雜湊表,用於記錄已被訪問過的頂點
val visited: MutableSet<Vertex?> = HashSet() val visited = HashSet<Vertex?>()
dfs(graph, visited, res, startVet) dfs(graph, visited, res, startVet)
return res return res
} }

View File

@@ -6,34 +6,28 @@
package chapter_greedy package chapter_greedy
import java.util.*
/* 物品 */ /* 物品 */
class Item( class Item(
val w: Int, // 物品 val w: Int, // 物品
val v: Int // 物品價值 val v: Int // 物品價值
) )
/* 分數背包:貪婪 */ /* 分數背包:貪婪 */
fun fractionalKnapsack( fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {
wgt: IntArray,
value: IntArray,
c: Int
): Double {
// 建立物品串列,包含兩個屬性:重量、價值 // 建立物品串列,包含兩個屬性:重量、價值
var cap = c var cap = c
val items = arrayOfNulls<Item>(wgt.size) val items = arrayOfNulls<Item>(wgt.size)
for (i in wgt.indices) { for (i in wgt.indices) {
items[i] = Item(wgt[i], value[i]) items[i] = Item(wgt[i], _val[i])
} }
// 按照單位價值 item.v / item.w 從高到低進行排序 // 按照單位價值 item.v / item.w 從高到低進行排序
Arrays.sort(items, Comparator.comparingDouble { item: Item -> -(item.v.toDouble() / item.w) }) items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) }
// 迴圈貪婪選擇 // 迴圈貪婪選擇
var res = 0.0 var res = 0.0
for (item in items) { for (item in items) {
if (item!!.w <= cap) { if (item!!.w <= cap) {
// 若剩餘容量充足,則將當前物品整個裝進背包 // 若剩餘容量充足,則將當前物品整個裝進背包
res += item.v.toDouble() res += item.v
cap -= item.w cap -= item.w
} else { } else {
// 若剩餘容量不足,則將當前物品的一部分裝進背包 // 若剩餘容量不足,則將當前物品的一部分裝進背包
@@ -48,10 +42,10 @@ fun fractionalKnapsack(
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50) val wgt = intArrayOf(10, 20, 30, 40, 50)
val values = intArrayOf(50, 120, 150, 210, 240) val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50 val cap = 50
// 貪婪演算法 // 貪婪演算法
val res = fractionalKnapsack(wgt, values, cap) val res = fractionalKnapsack(wgt, _val, cap)
println("不超過背包容量的最大物品價值為 $res") println("不超過背包容量的最大物品價值為 $res")
} }

View File

@@ -19,8 +19,8 @@ fun maxCapacity(ht: IntArray): Int {
// 迴圈貪婪選擇,直至兩板相遇 // 迴圈貪婪選擇,直至兩板相遇
while (i < j) { while (i < j) {
// 更新最大容量 // 更新最大容量
val cap = (min(ht[i].toDouble(), ht[j].toDouble()) * (j - i)).toInt() val cap = min(ht[i], ht[j]) * (j - i)
res = max(res.toDouble(), cap.toDouble()).toInt() res = max(res, cap)
// 向內移動短板 // 向內移動短板
if (ht[i] < ht[j]) { if (ht[i] < ht[j]) {
i++ i++

View File

@@ -19,14 +19,14 @@ fun maxProductCutting(n: Int): Int {
val b = n % 3 val b = n % 3
if (b == 1) { if (b == 1) {
// 當餘數為 1 時,將一對 1 * 3 轉化為 2 * 2 // 當餘數為 1 時,將一對 1 * 3 轉化為 2 * 2
return 3.0.pow((a - 1).toDouble()).toInt() * 2 * 2 return 3.0.pow((a - 1)).toInt() * 2 * 2
} }
if (b == 2) { if (b == 2) {
// 當餘數為 2 時,不做處理 // 當餘數為 2 時,不做處理
return 3.0.pow(a.toDouble()).toInt() * 2 * 2 return 3.0.pow(a).toInt() * 2 * 2
} }
// 當餘數為 0 時,不做處理 // 當餘數為 0 時,不做處理
return 3.0.pow(a.toDouble()).toInt() return 3.0.pow(a).toInt()
} }
/* Driver Code */ /* Driver Code */
@@ -36,4 +36,4 @@ fun main() {
// 貪婪演算法 // 貪婪演算法
val res = maxProductCutting(n) val res = maxProductCutting(n)
println("最大切分乘積為 $res") println("最大切分乘積為 $res")
} }

View File

@@ -9,20 +9,14 @@ package chapter_hashing
/* 鍵值對 */ /* 鍵值對 */
class Pair( class Pair(
var key: Int, var key: Int,
var value: String var _val: String
) )
/* 基於陣列實現的雜湊表 */ /* 基於陣列實現的雜湊表 */
class ArrayHashMap { class ArrayHashMap {
// 初始化陣列,包含 100 個桶
private val buckets = arrayOfNulls<Pair>(100) private val buckets = arrayOfNulls<Pair>(100)
init {
// 初始化陣列,包含 100 個桶
for (i in 0..<100) {
buckets[i] = null
}
}
/* 雜湊函式 */ /* 雜湊函式 */
fun hashFunc(key: Int): Int { fun hashFunc(key: Int): Int {
val index = key % 100 val index = key % 100
@@ -33,12 +27,12 @@ class ArrayHashMap {
fun get(key: Int): String? { fun get(key: Int): String? {
val index = hashFunc(key) val index = hashFunc(key)
val pair = buckets[index] ?: return null val pair = buckets[index] ?: return null
return pair.value return pair._val
} }
/* 新增操作 */ /* 新增操作 */
fun put(key: Int, value: String) { fun put(key: Int, _val: String) {
val pair = Pair(key, value) val pair = Pair(key, _val)
val index = hashFunc(key) val index = hashFunc(key)
buckets[index] = pair buckets[index] = pair
} }
@@ -52,27 +46,29 @@ class ArrayHashMap {
/* 獲取所有鍵值對 */ /* 獲取所有鍵值對 */
fun pairSet(): MutableList<Pair> { fun pairSet(): MutableList<Pair> {
val pairSet = ArrayList<Pair>() val pairSet = mutableListOf<Pair>()
for (pair in buckets) { for (pair in buckets) {
if (pair != null) pairSet.add(pair) if (pair != null)
pairSet.add(pair)
} }
return pairSet return pairSet
} }
/* 獲取所有鍵 */ /* 獲取所有鍵 */
fun keySet(): MutableList<Int> { fun keySet(): MutableList<Int> {
val keySet = ArrayList<Int>() val keySet = mutableListOf<Int>()
for (pair in buckets) { for (pair in buckets) {
if (pair != null) keySet.add(pair.key) if (pair != null)
keySet.add(pair.key)
} }
return keySet return keySet
} }
/* 獲取所有值 */ /* 獲取所有值 */
fun valueSet(): MutableList<String> { fun valueSet(): MutableList<String> {
val valueSet = ArrayList<String>() val valueSet = mutableListOf<String>()
for (pair in buckets) { for (pair in buckets) {
pair?.let { valueSet.add(it.value) } pair?.let { valueSet.add(it._val) }
} }
return valueSet return valueSet
} }
@@ -81,8 +77,8 @@ class ArrayHashMap {
fun print() { fun print() {
for (kv in pairSet()) { for (kv in pairSet()) {
val key = kv.key val key = kv.key
val value = kv.value val _val = kv._val
println("${key}->${value}") println("${key} -> ${_val}")
} }
} }
} }
@@ -104,7 +100,7 @@ fun main() {
/* 查詢操作 */ /* 查詢操作 */
// 向雜湊表中輸入鍵 key ,得到值 value // 向雜湊表中輸入鍵 key ,得到值 value
val name: String? = map.get(15937) val name = map.get(15937)
println("\n輸入學號 15937 ,查詢到姓名 $name") println("\n輸入學號 15937 ,查詢到姓名 $name")
/* 刪除操作 */ /* 刪除操作 */
@@ -114,16 +110,16 @@ fun main() {
map.print() map.print()
/* 走訪雜湊表 */ /* 走訪雜湊表 */
println("\n走訪鍵值對 Key->Value") println("\n走訪鍵值對 Key -> Value")
for (kv in map.pairSet()) { for (kv in map.pairSet()) {
println("${kv.key} -> ${kv.value}") println("${kv.key} -> ${kv._val}")
} }
println("\n單獨走訪鍵 Key") println("\n單獨走訪鍵 Key")
for (key in map.keySet()) { for (key in map.keySet()) {
println(key) println(key)
} }
println("\n單獨走訪值 Value") println("\n單獨走訪值 Value")
for (value in map.valueSet()) { for (_val in map.valueSet()) {
println(value) println(_val)
} }
} }

View File

@@ -11,15 +11,15 @@ import utils.ListNode
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
val num = 3 val num = 3
val hashNum = Integer.hashCode(num) val hashNum = num.hashCode()
println("整數 $num 的雜湊值為 $hashNum") println("整數 $num 的雜湊值為 $hashNum")
val bol = true val bol = true
val hashBol = Boolean.hashCode() val hashBol = bol.hashCode()
println("布林量 $bol 的雜湊值為 $hashBol") println("布林量 $bol 的雜湊值為 $hashBol")
val dec = 3.14159 val dec = 3.14159
val hashDec = java.lang.Double.hashCode(dec) val hashDec = dec.hashCode()
println("小數 $dec 的雜湊值為 $hashDec") println("小數 $dec 的雜湊值為 $hashDec")
val str = "Hello 演算法" val str = "Hello 演算法"

View File

@@ -11,7 +11,7 @@ import utils.printHashMap
/* Driver Code */ /* Driver Code */
fun main() { fun main() {
/* 初始化雜湊表 */ /* 初始化雜湊表 */
val map: MutableMap<Int, String> = HashMap() val map = HashMap<Int, String>()
/* 新增操作 */ /* 新增操作 */
// 在雜湊表中新增鍵值對 (key, value) // 在雜湊表中新增鍵值對 (key, value)
@@ -44,7 +44,7 @@ fun main() {
println(key) println(key)
} }
println("\n單獨走訪值 Value") println("\n單獨走訪值 Value")
for (value in map.values) { for (_val in map.values) {
println(value) println(_val)
} }
} }

View File

@@ -20,7 +20,7 @@ class HashMapChaining() {
capacity = 4 capacity = 4
loadThres = 2.0 / 3.0 loadThres = 2.0 / 3.0
extendRatio = 2 extendRatio = 2
buckets = ArrayList(capacity) buckets = mutableListOf()
for (i in 0..<capacity) { for (i in 0..<capacity) {
buckets.add(mutableListOf()) buckets.add(mutableListOf())
} }
@@ -42,14 +42,14 @@ class HashMapChaining() {
val bucket = buckets[index] val bucket = buckets[index]
// 走訪桶,若找到 key ,則返回對應 val // 走訪桶,若找到 key ,則返回對應 val
for (pair in bucket) { for (pair in bucket) {
if (pair.key == key) return pair.value if (pair.key == key) return pair._val
} }
// 若未找到 key ,則返回 null // 若未找到 key ,則返回 null
return null return null
} }
/* 新增操作 */ /* 新增操作 */
fun put(key: Int, value: String) { fun put(key: Int, _val: String) {
// 當負載因子超過閾值時,執行擴容 // 當負載因子超過閾值時,執行擴容
if (loadFactor() > loadThres) { if (loadFactor() > loadThres) {
extend() extend()
@@ -59,12 +59,12 @@ class HashMapChaining() {
// 走訪桶,若遇到指定 key ,則更新對應 val 並返回 // 走訪桶,若遇到指定 key ,則更新對應 val 並返回
for (pair in bucket) { for (pair in bucket) {
if (pair.key == key) { if (pair.key == key) {
pair.value = value pair._val = _val
return return
} }
} }
// 若無該 key ,則將鍵值對新增至尾部 // 若無該 key ,則將鍵值對新增至尾部
val pair = Pair(key, value) val pair = Pair(key, _val)
bucket.add(pair) bucket.add(pair)
size++ size++
} }
@@ -98,7 +98,7 @@ class HashMapChaining() {
// 將鍵值對從原雜湊表搬運至新雜湊表 // 將鍵值對從原雜湊表搬運至新雜湊表
for (bucket in bucketsTmp) { for (bucket in bucketsTmp) {
for (pair in bucket) { for (pair in bucket) {
put(pair.key, pair.value) put(pair.key, pair._val)
} }
} }
} }
@@ -109,7 +109,7 @@ class HashMapChaining() {
val res = mutableListOf<String>() val res = mutableListOf<String>()
for (pair in bucket) { for (pair in bucket) {
val k = pair.key val k = pair.key
val v = pair.value val v = pair._val
res.add("$k -> $v") res.add("$k -> $v")
} }
println(res) println(res)
@@ -142,4 +142,4 @@ fun main() {
map.remove(12836) map.remove(12836)
println("\n刪除 12836 後,雜湊表為\nKey -> Value") println("\n刪除 12836 後,雜湊表為\nKey -> Value")
map.print() map.print()
} }

View File

@@ -8,16 +8,21 @@ package chapter_hashing
/* 開放定址雜湊表 */ /* 開放定址雜湊表 */
class HashMapOpenAddressing { class HashMapOpenAddressing {
private var size: Int = 0 // 鍵值對數量 private var size: Int // 鍵值對數量
private var capacity = 4 // 雜湊表容量 private var capacity: Int // 雜湊表容量
private val loadThres: Double = 2.0 / 3.0 // 觸發擴容的負載因子閾值 private val loadThres: Double // 觸發擴容的負載因子閾值
private val extendRatio = 2 // 擴容倍數 private val extendRatio: Int // 擴容倍數
private var buckets: Array<Pair?> // 桶陣列 private var buckets: Array<Pair?> // 桶陣列
private val TOMBSTONE = Pair(-1, "-1") // 刪除標記 private val TOMBSTONE: Pair // 刪除標記
/* 建構子 */ /* 建構子 */
init { init {
size = 0
capacity = 4
loadThres = 2.0 / 3.0
extendRatio = 2
buckets = arrayOfNulls(capacity) buckets = arrayOfNulls(capacity)
TOMBSTONE = Pair(-1, "-1")
} }
/* 雜湊函式 */ /* 雜湊函式 */
@@ -63,14 +68,14 @@ class HashMapOpenAddressing {
val index = findBucket(key) val index = findBucket(key)
// 若找到鍵值對,則返回對應 val // 若找到鍵值對,則返回對應 val
if (buckets[index] != null && buckets[index] != TOMBSTONE) { if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index]?.value return buckets[index]?._val
} }
// 若鍵值對不存在,則返回 null // 若鍵值對不存在,則返回 null
return null return null
} }
/* 新增操作 */ /* 新增操作 */
fun put(key: Int, value: String) { fun put(key: Int, _val: String) {
// 當負載因子超過閾值時,執行擴容 // 當負載因子超過閾值時,執行擴容
if (loadFactor() > loadThres) { if (loadFactor() > loadThres) {
extend() extend()
@@ -79,11 +84,11 @@ class HashMapOpenAddressing {
val index = findBucket(key) val index = findBucket(key)
// 若找到鍵值對,則覆蓋 val 並返回 // 若找到鍵值對,則覆蓋 val 並返回
if (buckets[index] != null && buckets[index] != TOMBSTONE) { if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index]!!.value = value buckets[index]!!._val = _val
return return
} }
// 若鍵值對不存在,則新增該鍵值對 // 若鍵值對不存在,則新增該鍵值對
buckets[index] = Pair(key, value) buckets[index] = Pair(key, _val)
size++ size++
} }
@@ -109,7 +114,7 @@ class HashMapOpenAddressing {
// 將鍵值對從原雜湊表搬運至新雜湊表 // 將鍵值對從原雜湊表搬運至新雜湊表
for (pair in bucketsTmp) { for (pair in bucketsTmp) {
if (pair != null && pair != TOMBSTONE) { if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.value) put(pair.key, pair._val)
} }
} }
} }
@@ -122,7 +127,7 @@ class HashMapOpenAddressing {
} else if (pair == TOMBSTONE) { } else if (pair == TOMBSTONE) {
println("TOMESTOME") println("TOMESTOME")
} else { } else {
println("${pair.key} -> ${pair.value}") println("${pair.key} -> ${pair._val}")
} }
} }
} }

View File

@@ -6,7 +6,7 @@
package chapter_hashing package chapter_hashing
const val MODULUS = 10_0000_0007 const val MODULUS = 1000000007
/* 加法雜湊 */ /* 加法雜湊 */
fun addHash(key: String): Int { fun addHash(key: String): Int {
@@ -48,7 +48,7 @@ fun rotHash(key: String): Int {
fun main() { fun main() {
val key = "Hello 演算法" val key = "Hello 演算法"
var hash: Int = addHash(key) var hash = addHash(key)
println("加法雜湊值為 $hash") println("加法雜湊值為 $hash")
hash = mulHash(key) hash = mulHash(key)

View File

@@ -9,15 +9,15 @@ package chapter_heap
import utils.printHeap import utils.printHeap
import java.util.* import java.util.*
fun testPush(heap: Queue<Int>, value: Int) { fun testPush(heap: Queue<Int>, _val: Int) {
heap.offer(value) // 元素入堆積 heap.offer(_val) // 元素入堆積
print("\n元素 $value 入堆積後\n") print("\n元素 $_val 入堆積後\n")
printHeap(heap) printHeap(heap)
} }
fun testPop(heap: Queue<Int>) { fun testPop(heap: Queue<Int>) {
val value = heap.poll() // 堆積頂元素出堆積 val _val = heap.poll() // 堆積頂元素出堆積
print("\n堆積頂元素 $value 出堆積後\n") print("\n堆積頂元素 $_val 出堆積後\n")
printHeap(heap) printHeap(heap)
} }
@@ -25,7 +25,7 @@ fun testPop(heap: Queue<Int>) {
fun main() { fun main() {
/* 初始化堆積 */ /* 初始化堆積 */
// 初始化小頂堆積 // 初始化小頂堆積
val minHeap: PriorityQueue<Int> var minHeap = PriorityQueue<Int>()
// 初始化大頂堆積(使用 lambda 表示式修改 Comparator 即可) // 初始化大頂堆積(使用 lambda 表示式修改 Comparator 即可)
val maxHeap = PriorityQueue { a: Int, b: Int -> b - a } val maxHeap = PriorityQueue { a: Int, b: Int -> b - a }

View File

@@ -10,13 +10,14 @@ import utils.printHeap
import java.util.* import java.util.*
/* 大頂堆積 */ /* 大頂堆積 */
class MaxHeap(nums: List<Int>?) { class MaxHeap(nums: MutableList<Int>?) {
// 使用串列而非陣列,這樣無須考慮擴容問題 // 使用串列而非陣列,這樣無須考慮擴容問題
// 將串列元素原封不動新增進堆積 private val maxHeap = mutableListOf<Int>()
private val maxHeap = ArrayList(nums!!)
/* 建構子,根據輸入串列建堆積 */ /* 建構子,根據輸入串列建堆積 */
init { init {
// 將串列元素原封不動新增進堆積
maxHeap.addAll(nums!!)
// 堆積化除葉節點以外的其他所有節點 // 堆積化除葉節點以外的其他所有節點
for (i in parent(size() - 1) downTo 0) { for (i in parent(size() - 1) downTo 0) {
siftDown(i) siftDown(i)
@@ -60,9 +61,9 @@ class MaxHeap(nums: List<Int>?) {
} }
/* 元素入堆積 */ /* 元素入堆積 */
fun push(value: Int) { fun push(_val: Int) {
// 新增節點 // 新增節點
maxHeap.add(value) maxHeap.add(_val)
// 從底至頂堆積化 // 從底至頂堆積化
siftUp(size() - 1) siftUp(size() - 1)
} }
@@ -90,11 +91,11 @@ class MaxHeap(nums: List<Int>?) {
// 交換根節點與最右葉節點(交換首元素與尾元素) // 交換根節點與最右葉節點(交換首元素與尾元素)
swap(0, size() - 1) swap(0, size() - 1)
// 刪除節點 // 刪除節點
val value = maxHeap.removeAt(size() - 1) val _val = maxHeap.removeAt(size() - 1)
// 從頂至底堆積化 // 從頂至底堆積化
siftDown(0) siftDown(0)
// 返回堆積頂元素 // 返回堆積頂元素
return value return _val
} }
/* 從節點 i 開始,從頂至底堆積化 */ /* 從節點 i 開始,從頂至底堆積化 */
@@ -137,9 +138,9 @@ fun main() {
print("\n堆積頂元素為 $peek\n") print("\n堆積頂元素為 $peek\n")
/* 元素入堆積 */ /* 元素入堆積 */
val value = 7 val _val = 7
maxHeap.push(value) maxHeap.push(_val)
print("\n元素 $value 入堆積後\n") print("\n元素 $_val 入堆積後\n")
maxHeap.print() maxHeap.print()
/* 堆積頂元素出堆積 */ /* 堆積頂元素出堆積 */
@@ -154,4 +155,4 @@ fun main() {
/* 判斷堆積是否為空 */ /* 判斷堆積是否為空 */
val isEmpty = maxHeap.isEmpty() val isEmpty = maxHeap.isEmpty()
print("\n堆積是否為空 $isEmpty\n") print("\n堆積是否為空 $isEmpty\n")
} }

View File

@@ -7,18 +7,17 @@
package chapter_searching package chapter_searching
import utils.ListNode import utils.ListNode
import java.util.HashMap
/* 雜湊查詢(陣列) */ /* 雜湊查詢(陣列) */
fun hashingSearchArray(map: Map<Int?, Int>, target: Int): Int { fun hashingSearchArray(map: Map<Int?, Int>, target: Int): Int {
// 雜湊表的 key: 目標元素value: 索引 // 雜湊表的 key: 目標元素,_val: 索引
// 若雜湊表中無此 key ,返回 -1 // 若雜湊表中無此 key ,返回 -1
return map.getOrDefault(target, -1) return map.getOrDefault(target, -1)
} }
/* 雜湊查詢(鏈結串列) */ /* 雜湊查詢(鏈結串列) */
fun hashingSearchLinkedList(map: Map<Int?, ListNode?>, target: Int): ListNode? { fun hashingSearchLinkedList(map: Map<Int?, ListNode?>, target: Int): ListNode? {
// 雜湊表的 key: 目標節點值value: 節點物件 // 雜湊表的 key: 目標節點值,_val: 節點物件
// 若雜湊表中無此 key ,返回 null // 若雜湊表中無此 key ,返回 null
return map.getOrDefault(target, null) return map.getOrDefault(target, null)
} }
@@ -32,7 +31,7 @@ fun main() {
// 初始化雜湊表 // 初始化雜湊表
val map = HashMap<Int?, Int>() val map = HashMap<Int?, Int>()
for (i in nums.indices) { for (i in nums.indices) {
map[nums[i]] = i // key: 元素value: 索引 map[nums[i]] = i // key: 元素,_val: 索引
} }
val index = hashingSearchArray(map, target) val index = hashingSearchArray(map, target)
println("目標元素 3 的索引 = $index") println("目標元素 3 的索引 = $index")
@@ -42,7 +41,7 @@ fun main() {
// 初始化雜湊表 // 初始化雜湊表
val map1 = HashMap<Int?, ListNode?>() val map1 = HashMap<Int?, ListNode?>()
while (head != null) { while (head != null) {
map1[head.value] = head // key: 節點值value: 節點 map1[head._val] = head // key: 節點值,_val: 節點
head = head.next head = head.next
} }
val node = hashingSearchLinkedList(map1, target) val node = hashingSearchLinkedList(map1, target)

View File

@@ -26,7 +26,7 @@ fun linearSearchLinkedList(h: ListNode?, target: Int): ListNode? {
var head = h var head = h
while (head != null) { while (head != null) {
// 找到目標節點,返回之 // 找到目標節點,返回之
if (head.value == target) if (head._val == target)
return head return head
head = head.next head = head.next
} }

View File

@@ -14,7 +14,7 @@ fun bubbleSort(nums: IntArray) {
for (j in 0..<i) { for (j in 0..<i) {
if (nums[j] > nums[j + 1]) { if (nums[j] > nums[j + 1]) {
// 交換 nums[j] 與 nums[j + 1] // 交換 nums[j] 與 nums[j + 1]
nums[j] = nums[j+1].also { nums[j+1] = nums[j] } nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }
} }
} }
} }

View File

@@ -6,15 +6,13 @@
package chapter_sorting package chapter_sorting
import kotlin.collections.ArrayList
/* 桶排序 */ /* 桶排序 */
fun bucketSort(nums: FloatArray) { fun bucketSort(nums: FloatArray) {
// 初始化 k = n/2 個桶,預期向每個桶分配 2 個元素 // 初始化 k = n/2 個桶,預期向每個桶分配 2 個元素
val k = nums.size / 2 val k = nums.size / 2
val buckets = ArrayList<ArrayList<Float>>() val buckets = mutableListOf<MutableList<Float>>()
for (i in 0..<k) { for (i in 0..<k) {
buckets.add(ArrayList()) buckets.add(mutableListOf())
} }
// 1. 將陣列元素分配到各個桶中 // 1. 將陣列元素分配到各個桶中
for (num in nums) { for (num in nums) {

View File

@@ -14,7 +14,7 @@ fun countingSortNaive(nums: IntArray) {
// 1. 統計陣列最大元素 m // 1. 統計陣列最大元素 m
var m = 0 var m = 0
for (num in nums) { for (num in nums) {
m = max(m.toDouble(), num.toDouble()).toInt() m = max(m, num)
} }
// 2. 統計各數字的出現次數 // 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數 // counter[num] 代表 num 的出現次數
@@ -40,7 +40,7 @@ fun countingSort(nums: IntArray) {
// 1. 統計陣列最大元素 m // 1. 統計陣列最大元素 m
var m = 0 var m = 0
for (num in nums) { for (num in nums) {
m = max(m.toDouble(), num.toDouble()).toInt() m = max(m, num)
} }
// 2. 統計各數字的出現次數 // 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數 // counter[num] 代表 num 的出現次數

View File

@@ -14,10 +14,13 @@ fun siftDown(nums: IntArray, n: Int, li: Int) {
val l = 2 * i + 1 val l = 2 * i + 1
val r = 2 * i + 2 val r = 2 * i + 2
var ma = i var ma = i
if (l < n && nums[l] > nums[ma]) ma = l if (l < n && nums[l] > nums[ma])
if (r < n && nums[r] > nums[ma]) ma = r ma = l
if (r < n && nums[r] > nums[ma])
ma = r
// 若節點 i 最大或索引 l, r 越界,則無須繼續堆積化,跳出 // 若節點 i 最大或索引 l, r 越界,則無須繼續堆積化,跳出
if (ma == i) break if (ma == i)
break
// 交換兩節點 // 交換兩節點
nums[i] = nums[ma].also { nums[ma] = nums[i] } nums[i] = nums[ma].also { nums[ma] = nums[i] }
// 迴圈向下堆積化 // 迴圈向下堆積化

View File

@@ -12,7 +12,7 @@ fun insertionSort(nums: IntArray) {
for (i in nums.indices) { for (i in nums.indices) {
val base = nums[i] val base = nums[i]
var j = i - 1 var j = i - 1
// 內迴圈: 將 base 插入到已排序部分的正確位置 // 內迴圈將 base 插入到已排序區間 [0, i-1] 中的正確位置
while (j >= 0 && nums[j] > base) { while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j] // 將 nums[j] 向右移動一位 nums[j + 1] = nums[j] // 將 nums[j] 向右移動一位
j-- j--

View File

@@ -17,8 +17,10 @@ fun merge(nums: IntArray, left: Int, mid: Int, right: Int) {
var k = 0 var k = 0
// 當左右子陣列都還有元素時,進行比較並將較小的元素複製到臨時陣列中 // 當左右子陣列都還有元素時,進行比較並將較小的元素複製到臨時陣列中
while (i <= mid && j <= right) { while (i <= mid && j <= right) {
if (nums[i] <= nums[j]) tmp[k++] = nums[i++] if (nums[i] <= nums[j])
else tmp[k++] = nums[j++] tmp[k++] = nums[i++]
else
tmp[k++] = nums[j++]
} }
// 將左子陣列和右子陣列的剩餘元素複製到臨時陣列中 // 將左子陣列和右子陣列的剩餘元素複製到臨時陣列中
while (i <= mid) { while (i <= mid) {

View File

@@ -20,7 +20,7 @@ fun countingSortDigit(nums: IntArray, exp: Int) {
// 統計 0~9 各數字的出現次數 // 統計 0~9 各數字的出現次數
for (i in 0..<n) { for (i in 0..<n) {
val d = digit(nums[i], exp) // 獲取 nums[i] 第 k 位,記為 d val d = digit(nums[i], exp) // 獲取 nums[i] 第 k 位,記為 d
counter[d]++ // 統計數字 d 的出現次數 counter[d]++ // 統計數字 d 的出現次數
} }
// 求前綴和,將“出現個數”轉換為“陣列索引” // 求前綴和,將“出現個數”轉換為“陣列索引”
for (i in 1..9) { for (i in 1..9) {
@@ -31,11 +31,12 @@ fun countingSortDigit(nums: IntArray, exp: Int) {
for (i in n - 1 downTo 0) { for (i in n - 1 downTo 0) {
val d = digit(nums[i], exp) val d = digit(nums[i], exp)
val j = counter[d] - 1 // 獲取 d 在陣列中的索引 j val j = counter[d] - 1 // 獲取 d 在陣列中的索引 j
res[j] = nums[i] // 將當前元素填入索引 j res[j] = nums[i] // 將當前元素填入索引 j
counter[d]-- // 將 d 的數量減 1 counter[d]-- // 將 d 的數量減 1
} }
// 使用結果覆蓋原陣列 nums // 使用結果覆蓋原陣列 nums
for (i in 0..<n) nums[i] = res[i] for (i in 0..<n)
nums[i] = res[i]
} }
/* 基數排序 */ /* 基數排序 */

View File

@@ -14,7 +14,8 @@ fun selectionSort(nums: IntArray) {
var k = i var k = i
// 內迴圈:找到未排序區間內的最小元素 // 內迴圈:找到未排序區間內的最小元素
for (j in i + 1..<n) { for (j in i + 1..<n) {
if (nums[j] < nums[k]) k = j // 記錄最小元素的索引 if (nums[j] < nums[k])
k = j // 記錄最小元素的索引
} }
// 將該最小元素與未排序區間的首個元素交換 // 將該最小元素與未排序區間的首個元素交換
nums[i] = nums[k].also { nums[k] = nums[i] } nums[i] = nums[k].also { nums[k] = nums[i] }

View File

@@ -7,10 +7,11 @@
package chapter_stack_and_queue package chapter_stack_and_queue
/* 基於環形陣列實現的雙向佇列 */ /* 基於環形陣列實現的雙向佇列 */
/* 建構子 */
class ArrayDeque(capacity: Int) { class ArrayDeque(capacity: Int) {
private var nums = IntArray(capacity) // 用於儲存雙向佇列元素的陣列 private var nums: IntArray = IntArray(capacity) // 用於儲存雙向佇列元素的陣列
private var front = 0 // 佇列首指標,指向佇列首元素 private var front: Int = 0 // 佇列首指標,指向佇列首元素
private var queSize = 0 // 雙向佇列長度 private var queSize: Int = 0 // 雙向佇列長度
/* 獲取雙向佇列的容量 */ /* 獲取雙向佇列的容量 */
fun capacity(): Int { fun capacity(): Int {
@@ -71,7 +72,7 @@ class ArrayDeque(capacity: Int) {
return num return num
} }
/* 訪問佇列尾元素 */ /* 佇列尾出列 */
fun popLast(): Int { fun popLast(): Int {
val num = peekLast() val num = peekLast()
queSize-- queSize--

View File

@@ -8,9 +8,9 @@ package chapter_stack_and_queue
/* 基於環形陣列實現的佇列 */ /* 基於環形陣列實現的佇列 */
class ArrayQueue(capacity: Int) { class ArrayQueue(capacity: Int) {
private val nums = IntArray(capacity) // 用於儲存佇列元素的陣列 private val nums: IntArray = IntArray(capacity) // 用於儲存佇列元素的陣列
private var front = 0 // 佇列首指標,指向佇列首元素 private var front: Int = 0 // 佇列首指標,指向佇列首元素
private var queSize = 0 // 佇列長度 private var queSize: Int = 0 // 佇列長度
/* 獲取佇列的容量 */ /* 獲取佇列的容量 */
fun capacity(): Int { fun capacity(): Int {

View File

@@ -9,7 +9,7 @@ package chapter_stack_and_queue
/* 基於陣列實現的堆疊 */ /* 基於陣列實現的堆疊 */
class ArrayStack { class ArrayStack {
// 初始化串列(動態陣列) // 初始化串列(動態陣列)
private val stack = ArrayList<Int>() private val stack = mutableListOf<Int>()
/* 獲取堆疊的長度 */ /* 獲取堆疊的長度 */
fun size(): Int { fun size(): Int {
@@ -40,7 +40,7 @@ class ArrayStack {
/* 將 List 轉化為 Array 並返回 */ /* 將 List 轉化為 Array 並返回 */
fun toArray(): Array<Any> { fun toArray(): Array<Any> {
return stack.toArray() return stack.toTypedArray()
} }
} }
@@ -63,7 +63,7 @@ fun main() {
/* 元素出堆疊 */ /* 元素出堆疊 */
val pop = stack.pop() val pop = stack.pop()
println("出堆疊元素 pop = ${pop},出堆疊後 stack = ${stack.toArray().contentToString()}") println("出堆疊元素 pop = $pop,出堆疊後 stack = ${stack.toArray().contentToString()}")
/* 獲取堆疊的長度 */ /* 獲取堆疊的長度 */
val size = stack.size() val size = stack.size()

View File

@@ -7,7 +7,7 @@
package chapter_stack_and_queue package chapter_stack_and_queue
/* 雙向鏈結串列節點 */ /* 雙向鏈結串列節點 */
class ListNode(var value: Int) { class ListNode(var _val: Int) {
// 節點值 // 節點值
var next: ListNode? = null // 後繼節點引用 var next: ListNode? = null // 後繼節點引用
var prev: ListNode? = null // 前驅節點引用 var prev: ListNode? = null // 前驅節點引用
@@ -15,9 +15,9 @@ class ListNode(var value: Int) {
/* 基於雙向鏈結串列實現的雙向佇列 */ /* 基於雙向鏈結串列實現的雙向佇列 */
class LinkedListDeque { class LinkedListDeque {
private var front: ListNode? = null // 頭節點 front ,尾節點 rear private var front: ListNode? = null // 頭節點 front
private var rear: ListNode? = null private var rear: ListNode? = null // 尾節點 rear
private var queSize = 0 // 雙向佇列的長度 private var queSize: Int = 0 // 雙向佇列的長度
/* 獲取雙向佇列的長度 */ /* 獲取雙向佇列的長度 */
fun size(): Int { fun size(): Int {
@@ -64,12 +64,12 @@ class LinkedListDeque {
/* 出列操作 */ /* 出列操作 */
fun pop(isFront: Boolean): Int { fun pop(isFront: Boolean): Int {
if (isEmpty()) throw IndexOutOfBoundsException() if (isEmpty())
throw IndexOutOfBoundsException()
val value: Int val _val: Int
// 佇列首出列操作 // 佇列首出列操作
if (isFront) { if (isFront) {
value = front!!.value // 暫存頭節點值 _val = front!!._val // 暫存頭節點值
// 刪除頭節點 // 刪除頭節點
val fNext = front!!.next val fNext = front!!.next
if (fNext != null) { if (fNext != null) {
@@ -79,7 +79,7 @@ class LinkedListDeque {
front = fNext // 更新頭節點 front = fNext // 更新頭節點
// 佇列尾出列操作 // 佇列尾出列操作
} else { } else {
value = rear!!.value // 暫存尾節點值 _val = rear!!._val // 暫存尾節點值
// 刪除尾節點 // 刪除尾節點
val rPrev = rear!!.prev val rPrev = rear!!.prev
if (rPrev != null) { if (rPrev != null) {
@@ -89,7 +89,7 @@ class LinkedListDeque {
rear = rPrev // 更新尾節點 rear = rPrev // 更新尾節點
} }
queSize-- // 更新佇列長度 queSize-- // 更新佇列長度
return value return _val
} }
/* 佇列首出列 */ /* 佇列首出列 */
@@ -104,17 +104,14 @@ class LinkedListDeque {
/* 訪問佇列首元素 */ /* 訪問佇列首元素 */
fun peekFirst(): Int { fun peekFirst(): Int {
if (isEmpty()) { if (isEmpty()) throw IndexOutOfBoundsException()
throw IndexOutOfBoundsException() return front!!._val
}
return front!!.value
} }
/* 訪問佇列尾元素 */ /* 訪問佇列尾元素 */
fun peekLast(): Int { fun peekLast(): Int {
if (isEmpty()) throw IndexOutOfBoundsException() if (isEmpty()) throw IndexOutOfBoundsException()
return rear!!.value return rear!!._val
} }
/* 返回陣列用於列印 */ /* 返回陣列用於列印 */
@@ -122,7 +119,7 @@ class LinkedListDeque {
var node = front var node = front
val res = IntArray(size()) val res = IntArray(size())
for (i in res.indices) { for (i in res.indices) {
res[i] = node!!.value res[i] = node!!._val
node = node.next node = node.next
} }
return res return res

View File

@@ -52,7 +52,7 @@ class LinkedListQueue(
/* 訪問佇列首元素 */ /* 訪問佇列首元素 */
fun peek(): Int { fun peek(): Int {
if (isEmpty()) throw IndexOutOfBoundsException() if (isEmpty()) throw IndexOutOfBoundsException()
return front!!.value return front!!._val
} }
/* 將鏈結串列轉化為 Array 並返回 */ /* 將鏈結串列轉化為 Array 並返回 */
@@ -60,7 +60,7 @@ class LinkedListQueue(
var node = front var node = front
val res = IntArray(size()) val res = IntArray(size())
for (i in res.indices) { for (i in res.indices) {
res[i] = node!!.value res[i] = node!!._val
node = node.next node = node.next
} }
return res return res
@@ -95,4 +95,4 @@ fun main() {
/* 判斷佇列是否為空 */ /* 判斷佇列是否為空 */
val isEmpty = queue.isEmpty() val isEmpty = queue.isEmpty()
println("佇列是否為空 = $isEmpty") println("佇列是否為空 = $isEmpty")
} }

View File

@@ -41,7 +41,7 @@ class LinkedListStack(
/* 訪問堆疊頂元素 */ /* 訪問堆疊頂元素 */
fun peek(): Int? { fun peek(): Int? {
if (isEmpty()) throw IndexOutOfBoundsException() if (isEmpty()) throw IndexOutOfBoundsException()
return stackPeek?.value return stackPeek?._val
} }
/* 將 List 轉化為 Array 並返回 */ /* 將 List 轉化為 Array 並返回 */
@@ -49,7 +49,7 @@ class LinkedListStack(
var node = stackPeek var node = stackPeek
val res = IntArray(size()) val res = IntArray(size())
for (i in res.size - 1 downTo 0) { for (i in res.size - 1 downTo 0) {
res[i] = node?.value!! res[i] = node?._val!!
node = node.next node = node.next
} }
return res return res

View File

@@ -10,14 +10,15 @@ import utils.TreeNode
import utils.printTree import utils.printTree
/* 陣列表示下的二元樹類別 */ /* 陣列表示下的二元樹類別 */
class ArrayBinaryTree(private val tree: List<Int?>) { /* 建構子 */
class ArrayBinaryTree(private val tree: MutableList<Int?>) {
/* 串列容量 */ /* 串列容量 */
fun size(): Int { fun size(): Int {
return tree.size return tree.size
} }
/* 獲取索引為 i 節點的值 */ /* 獲取索引為 i 節點的值 */
fun value(i: Int): Int? { fun _val(i: Int): Int? {
// 若索引越界,則返回 null ,代表空位 // 若索引越界,則返回 null ,代表空位
if (i < 0 || i >= size()) return null if (i < 0 || i >= size()) return null
return tree[i] return tree[i]
@@ -39,11 +40,12 @@ class ArrayBinaryTree(private val tree: List<Int?>) {
} }
/* 層序走訪 */ /* 層序走訪 */
fun levelOrder(): List<Int?> { fun levelOrder(): MutableList<Int?> {
val res = ArrayList<Int?>() val res = mutableListOf<Int?>()
// 直接走訪陣列 // 直接走訪陣列
for (i in 0..<size()) { for (i in 0..<size()) {
if (value(i) != null) res.add(value(i)) if (_val(i) != null)
res.add(_val(i))
} }
return res return res
} }
@@ -51,34 +53,38 @@ class ArrayBinaryTree(private val tree: List<Int?>) {
/* 深度優先走訪 */ /* 深度優先走訪 */
fun dfs(i: Int, order: String, res: MutableList<Int?>) { fun dfs(i: Int, order: String, res: MutableList<Int?>) {
// 若為空位,則返回 // 若為空位,則返回
if (value(i) == null) return if (_val(i) == null)
return
// 前序走訪 // 前序走訪
if ("pre" == order) res.add(value(i)) if ("pre" == order)
res.add(_val(i))
dfs(left(i), order, res) dfs(left(i), order, res)
// 中序走訪 // 中序走訪
if ("in" == order) res.add(value(i)) if ("in" == order)
res.add(_val(i))
dfs(right(i), order, res) dfs(right(i), order, res)
// 後序走訪 // 後序走訪
if ("post" == order) res.add(value(i)) if ("post" == order)
res.add(_val(i))
} }
/* 前序走訪 */ /* 前序走訪 */
fun preOrder(): List<Int?> { fun preOrder(): MutableList<Int?> {
val res = ArrayList<Int?>() val res = mutableListOf<Int?>()
dfs(0, "pre", res) dfs(0, "pre", res)
return res return res
} }
/* 中序走訪 */ /* 中序走訪 */
fun inOrder(): List<Int?> { fun inOrder(): MutableList<Int?> {
val res = ArrayList<Int?>() val res = mutableListOf<Int?>()
dfs(0, "in", res) dfs(0, "in", res)
return res return res
} }
/* 後序走訪 */ /* 後序走訪 */
fun postOrder(): List<Int?> { fun postOrder(): MutableList<Int?> {
val res = ArrayList<Int?>() val res = mutableListOf<Int?>()
dfs(0, "post", res) dfs(0, "post", res)
return res return res
} }
@@ -105,10 +111,10 @@ fun main() {
val l = abt.left(i) val l = abt.left(i)
val r = abt.right(i) val r = abt.right(i)
val p = abt.parent(i) val p = abt.parent(i)
println("當前節點的索引為 $i ,值為 ${abt.value(i)}") println("當前節點的索引為 $i ,值為 ${abt._val(i)}")
println("其左子節點的索引為 $l ,值為 ${abt.value(l)}") println("其左子節點的索引為 $l ,值為 ${abt._val(l)}")
println("其右子節點的索引為 $r ,值為 ${abt.value(r)}") println("其右子節點的索引為 $r ,值為 ${abt._val(r)}")
println("其父節點的索引為 $p ,值為 ${abt.value(p)}") println("其父節點的索引為 $p ,值為 ${abt._val(p)}")
// 走訪樹 // 走訪樹
var res = abt.levelOrder() var res = abt.levelOrder()

View File

@@ -23,7 +23,7 @@ class AVLTree {
/* 更新節點高度 */ /* 更新節點高度 */
private fun updateHeight(node: TreeNode?) { private fun updateHeight(node: TreeNode?) {
// 節點高度等於最高子樹高度 + 1 // 節點高度等於最高子樹高度 + 1
node?.height = (max(height(node?.left).toDouble(), height(node?.right).toDouble()) + 1).toInt() node?.height = max(height(node?.left), height(node?.right)) + 1
} }
/* 獲取平衡因子 */ /* 獲取平衡因子 */
@@ -93,20 +93,22 @@ class AVLTree {
} }
/* 插入節點 */ /* 插入節點 */
fun insert(value: Int) { fun insert(_val: Int) {
root = insertHelper(root, value) root = insertHelper(root, _val)
} }
/* 遞迴插入節點(輔助方法) */ /* 遞迴插入節點(輔助方法) */
private fun insertHelper(n: TreeNode?, value: Int): TreeNode { private fun insertHelper(n: TreeNode?, _val: Int): TreeNode {
if (n == null) if (n == null)
return TreeNode(value) return TreeNode(_val)
var node = n var node = n
/* 1. 查詢插入位置並插入節點 */ /* 1. 查詢插入位置並插入節點 */
if (value < node.value) node.left = insertHelper(node.left, value) if (_val < node._val)
else if (value > node.value) node.right = insertHelper(node.right, value) node.left = insertHelper(node.left, _val)
else return node // 重複節點不插入,直接返回 else if (_val > node._val)
node.right = insertHelper(node.right, _val)
else
return node // 重複節點不插入,直接返回
updateHeight(node) // 更新節點高度 updateHeight(node) // 更新節點高度
/* 2. 執行旋轉操作,使該子樹重新恢復平衡 */ /* 2. 執行旋轉操作,使該子樹重新恢復平衡 */
node = rotate(node) node = rotate(node)
@@ -115,30 +117,38 @@ class AVLTree {
} }
/* 刪除節點 */ /* 刪除節點 */
fun remove(value: Int) { fun remove(_val: Int) {
root = removeHelper(root, value) root = removeHelper(root, _val)
} }
/* 遞迴刪除節點(輔助方法) */ /* 遞迴刪除節點(輔助方法) */
private fun removeHelper(n: TreeNode?, value: Int): TreeNode? { private fun removeHelper(n: TreeNode?, _val: Int): TreeNode? {
var node = n ?: return null var node = n ?: return null
/* 1. 查詢節點並刪除 */ /* 1. 查詢節點並刪除 */
if (value < node.value) node.left = removeHelper(node.left, value) if (_val < node._val)
else if (value > node.value) node.right = removeHelper(node.right, value) node.left = removeHelper(node.left, _val)
else if (_val > node._val)
node.right = removeHelper(node.right, _val)
else { else {
if (node.left == null || node.right == null) { if (node.left == null || node.right == null) {
val child = if (node.left != null) node.left else node.right val child = if (node.left != null)
node.left
else
node.right
// 子節點數量 = 0 ,直接刪除 node 並返回 // 子節點數量 = 0 ,直接刪除 node 並返回
if (child == null) return null if (child == null)
else node = child return null
// 子節點數量 = 1 ,直接刪除 node
else
node = child
} else { } else {
// 子節點數量 = 2 ,則將中序走訪的下個節點刪除,並用該節點替換當前節點 // 子節點數量 = 2 ,則將中序走訪的下個節點刪除,並用該節點替換當前節點
var temp = node.right var temp = node.right
while (temp!!.left != null) { while (temp!!.left != null) {
temp = temp.left temp = temp.left
} }
node.right = removeHelper(node.right, temp.value) node.right = removeHelper(node.right, temp._val)
node.value = temp.value node._val = temp._val
} }
} }
updateHeight(node) // 更新節點高度 updateHeight(node) // 更新節點高度
@@ -149,29 +159,34 @@ class AVLTree {
} }
/* 查詢節點 */ /* 查詢節點 */
fun search(value: Int): TreeNode? { fun search(_val: Int): TreeNode? {
var cur = root var cur = root
// 迴圈查詢,越過葉節點後跳出 // 迴圈查詢,越過葉節點後跳出
while (cur != null) { while (cur != null) {
// 目標節點在 cur 的右子樹中 // 目標節點在 cur 的右子樹中
cur = if (cur.value < value) cur.right!! cur = if (cur._val < _val)
else (if (cur.value > value) cur.left cur.right!!
else break)!! // 目標節點在 cur 的左子樹中
else if (cur._val > _val)
cur.left
// 找到目標節點,跳出迴圈
else
break
} }
// 返回目標節點 // 返回目標節點
return cur return cur
} }
} }
fun testInsert(tree: AVLTree, value: Int) { fun testInsert(tree: AVLTree, _val: Int) {
tree.insert(value) tree.insert(_val)
println("\n插入節點 $valueAVL 樹為") println("\n插入節點 $_valAVL 樹為")
printTree(tree.root) printTree(tree.root)
} }
fun testRemove(tree: AVLTree, value: Int) { fun testRemove(tree: AVLTree, _val: Int) {
tree.remove(value) tree.remove(_val)
println("\n刪除節點 $valueAVL 樹為") println("\n刪除節點 $_valAVL 樹為")
printTree(tree.root) printTree(tree.root)
} }
@@ -204,5 +219,5 @@ fun main() {
/* 查詢節點 */ /* 查詢節點 */
val node = avlTree.search(7) val node = avlTree.search(7)
println("\n 查詢到的節點物件為 $node,節點值 = ${node?.value}") println("\n 查詢到的節點物件為 $node,節點值 = ${node?._val}")
} }

View File

@@ -11,6 +11,7 @@ import utils.printTree
/* 二元搜尋樹 */ /* 二元搜尋樹 */
class BinarySearchTree { class BinarySearchTree {
// 初始化空樹
private var root: TreeNode? = null private var root: TreeNode? = null
/* 獲取二元樹根節點 */ /* 獲取二元樹根節點 */
@@ -24,11 +25,14 @@ class BinarySearchTree {
// 迴圈查詢,越過葉節點後跳出 // 迴圈查詢,越過葉節點後跳出
while (cur != null) { while (cur != null) {
// 目標節點在 cur 的右子樹中 // 目標節點在 cur 的右子樹中
cur = if (cur.value < num) cur.right cur = if (cur._val < num)
cur.right
// 目標節點在 cur 的左子樹中 // 目標節點在 cur 的左子樹中
else if (cur.value > num) cur.left else if (cur._val > num)
cur.left
// 找到目標節點,跳出迴圈 // 找到目標節點,跳出迴圈
else break else
break
} }
// 返回目標節點 // 返回目標節點
return cur return cur
@@ -46,45 +50,60 @@ class BinarySearchTree {
// 迴圈查詢,越過葉節點後跳出 // 迴圈查詢,越過葉節點後跳出
while (cur != null) { while (cur != null) {
// 找到重複節點,直接返回 // 找到重複節點,直接返回
if (cur.value == num) return if (cur._val == num)
return
pre = cur pre = cur
// 插入位置在 cur 的右子樹中 // 插入位置在 cur 的右子樹中
cur = if (cur.value < num) cur.right cur = if (cur._val < num)
cur.right
// 插入位置在 cur 的左子樹中 // 插入位置在 cur 的左子樹中
else cur.left else
cur.left
} }
// 插入節點 // 插入節點
val node = TreeNode(num) val node = TreeNode(num)
if (pre?.value!! < num) pre.right = node if (pre?._val!! < num)
else pre.left = node pre.right = node
else
pre.left = node
} }
/* 刪除節點 */ /* 刪除節點 */
fun remove(num: Int) { fun remove(num: Int) {
// 若樹為空,直接提前返回 // 若樹為空,直接提前返回
if (root == null) return if (root == null)
return
var cur = root var cur = root
var pre: TreeNode? = null var pre: TreeNode? = null
// 迴圈查詢,越過葉節點後跳出 // 迴圈查詢,越過葉節點後跳出
while (cur != null) { while (cur != null) {
// 找到待刪除節點,跳出迴圈 // 找到待刪除節點,跳出迴圈
if (cur.value == num) break if (cur._val == num)
break
pre = cur pre = cur
// 待刪除節點在 cur 的右子樹中 // 待刪除節點在 cur 的右子樹中
cur = if (cur.value < num) cur.right cur = if (cur._val < num)
cur.right
// 待刪除節點在 cur 的左子樹中 // 待刪除節點在 cur 的左子樹中
else cur.left else
cur.left
} }
// 若無待刪除節點,則直接返回 // 若無待刪除節點,則直接返回
if (cur == null) return if (cur == null)
return
// 子節點數量 = 0 or 1 // 子節點數量 = 0 or 1
if (cur.left == null || cur.right == null) { if (cur.left == null || cur.right == null) {
// 當子節點數量 = 0 / 1 時, child = null / 該子節點 // 當子節點數量 = 0 / 1 時, child = null / 該子節點
val child = if (cur.left != null) cur.left else cur.right val child = if (cur.left != null)
cur.left
else
cur.right
// 刪除節點 cur // 刪除節點 cur
if (cur != root) { if (cur != root) {
if (pre!!.left == cur) pre.left = child if (pre!!.left == cur)
else pre.right = child pre.left = child
else
pre.right = child
} else { } else {
// 若刪除節點為根節點,則重新指定根節點 // 若刪除節點為根節點,則重新指定根節點
root = child root = child
@@ -97,9 +116,9 @@ class BinarySearchTree {
tmp = tmp.left tmp = tmp.left
} }
// 遞迴刪除節點 tmp // 遞迴刪除節點 tmp
remove(tmp.value) remove(tmp._val)
// 用 tmp 覆蓋 cur // 用 tmp 覆蓋 cur
cur.value = tmp.value cur._val = tmp._val
} }
} }
} }
@@ -118,7 +137,7 @@ fun main() {
/* 查詢節點 */ /* 查詢節點 */
val node = bst.search(7) val node = bst.search(7)
println("查詢到的節點物件為 $node,節點值 = ${node?.value}") println("查詢到的節點物件為 $node,節點值 = ${node?._val}")
/* 插入節點 */ /* 插入節點 */
bst.insert(16) bst.insert(16)

View File

@@ -16,13 +16,14 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
val queue = LinkedList<TreeNode?>() val queue = LinkedList<TreeNode?>()
queue.add(root) queue.add(root)
// 初始化一個串列,用於儲存走訪序列 // 初始化一個串列,用於儲存走訪序列
val list = ArrayList<Int>() val list = mutableListOf<Int>()
while (!queue.isEmpty()) { while (queue.isNotEmpty()) {
val node = queue.poll() // 隊列出隊 val node = queue.poll() // 隊列出隊
list.add(node?.value!!) // 儲存節點值 list.add(node?._val!!) // 儲存節點值
if (node.left != null) queue.offer(node.left) // 左子節點入列 if (node.left != null)
queue.offer(node.left) // 左子節點入列
if (node.right != null) queue.offer(node.right) // 右子節點入列 if (node.right != null)
queue.offer(node.right) // 右子節點入列
} }
return list return list
} }

View File

@@ -10,13 +10,13 @@ import utils.TreeNode
import utils.printTree import utils.printTree
// 初始化串列,用於儲存走訪序列 // 初始化串列,用於儲存走訪序列
var list = ArrayList<Int>() var list = mutableListOf<Int>()
/* 前序走訪 */ /* 前序走訪 */
fun preOrder(root: TreeNode?) { fun preOrder(root: TreeNode?) {
if (root == null) return if (root == null) return
// 訪問優先順序:根節點 -> 左子樹 -> 右子樹 // 訪問優先順序:根節點 -> 左子樹 -> 右子樹
list.add(root.value) list.add(root._val)
preOrder(root.left) preOrder(root.left)
preOrder(root.right) preOrder(root.right)
} }
@@ -26,7 +26,7 @@ fun inOrder(root: TreeNode?) {
if (root == null) return if (root == null) return
// 訪問優先順序:左子樹 -> 根節點 -> 右子樹 // 訪問優先順序:左子樹 -> 根節點 -> 右子樹
inOrder(root.left) inOrder(root.left)
list.add(root.value) list.add(root._val)
inOrder(root.right) inOrder(root.right)
} }
@@ -36,7 +36,7 @@ fun postOrder(root: TreeNode?) {
// 訪問優先順序:左子樹 -> 右子樹 -> 根節點 // 訪問優先順序:左子樹 -> 右子樹 -> 根節點
postOrder(root.left) postOrder(root.left)
postOrder(root.right) postOrder(root.right)
list.add(root.value) list.add(root._val)
} }
/* Driver Code */ /* Driver Code */

View File

@@ -7,7 +7,7 @@
package utils package utils
/* 鏈結串列節點 */ /* 鏈結串列節點 */
class ListNode(var value: Int) { class ListNode(var _val: Int) {
var next: ListNode? = null var next: ListNode? = null
companion object { companion object {
@@ -15,11 +15,11 @@ class ListNode(var value: Int) {
fun arrToLinkedList(arr: IntArray): ListNode? { fun arrToLinkedList(arr: IntArray): ListNode? {
val dum = ListNode(0) val dum = ListNode(0)
var head = dum var head = dum
for (value in arr) { for (_val in arr) {
head.next = ListNode(value) head.next = ListNode(_val)
head = head.next!! head = head.next!!
} }
return dum.next return dum.next
} }
} }
} }

View File

@@ -20,7 +20,7 @@ fun <T> printMatrix(matrix: Array<Array<T>>) {
} }
/* 列印矩陣List */ /* 列印矩陣List */
fun <T> printMatrix(matrix: List<List<T>>) { fun <T> printMatrix(matrix: MutableList<MutableList<T>>) {
println("[") println("[")
for (row in matrix) { for (row in matrix) {
println(" $row,") println(" $row,")
@@ -31,9 +31,9 @@ fun <T> printMatrix(matrix: List<List<T>>) {
/* 列印鏈結串列 */ /* 列印鏈結串列 */
fun printLinkedList(h: ListNode?) { fun printLinkedList(h: ListNode?) {
var head = h var head = h
val list = ArrayList<String>() val list = mutableListOf<String>()
while (head != null) { while (head != null) {
list.add(head.value.toString()) list.add(head._val.toString())
head = head.next head = head.next
} }
println(list.joinToString(separator = " -> ")) println(list.joinToString(separator = " -> "))
@@ -70,7 +70,7 @@ fun printTree(root: TreeNode?, prev: Trunk?, isRight: Boolean) {
} }
showTrunks(trunk) showTrunks(trunk)
println(" ${root.value}") println(" ${root._val}")
if (prev != null) { if (prev != null) {
prev.str = prevStr prev.str = prevStr
@@ -91,16 +91,17 @@ fun showTrunks(p: Trunk?) {
/* 列印雜湊表 */ /* 列印雜湊表 */
fun <K, V> printHashMap(map: Map<K, V>) { fun <K, V> printHashMap(map: Map<K, V>) {
for ((key, value) in map) { for ((key, value) in map) {
println(key.toString() + " -> " + value) println("${key.toString()} -> $value")
} }
} }
/* 列印堆積 */ /* 列印堆積 */
fun printHeap(queue: Queue<Int>?) { fun printHeap(queue: Queue<Int>?) {
val list = queue?.let { ArrayList(it) } val list = mutableListOf<Int?>()
queue?.let { list.addAll(it) }
print("堆積的陣列表示:") print("堆積的陣列表示:")
println(list) println(list)
println("堆積的樹狀表示:") println("堆積的樹狀表示:")
val root = list?.let { TreeNode.listToTree(it) } val root = TreeNode.listToTree(list)
printTree(root) printTree(root)
} }

View File

@@ -7,8 +7,9 @@
package utils package utils
/* 二元樹節點類別 */ /* 二元樹節點類別 */
/* 建構子 */
class TreeNode( class TreeNode(
var value: Int // 節點值 var _val: Int // 節點值
) { ) {
var height: Int = 0 // 節點高度 var height: Int = 0 // 節點高度
var left: TreeNode? = null // 左子節點引用 var left: TreeNode? = null // 左子節點引用
@@ -53,14 +54,14 @@ class TreeNode(
while (i >= res.size) { while (i >= res.size) {
res.add(null) res.add(null)
} }
res[i] = root.value res[i] = root._val
treeToListDFS(root.left, 2 * i + 1, res) treeToListDFS(root.left, 2 * i + 1, res)
treeToListDFS(root.right, 2 * i + 2, res) treeToListDFS(root.right, 2 * i + 2, res)
} }
/* 將二元樹序列化為串列 */ /* 將二元樹序列化為串列 */
fun treeToList(root: TreeNode?): List<Int?> { fun treeToList(root: TreeNode?): MutableList<Int?> {
val res = ArrayList<Int?>() val res = mutableListOf<Int?>()
treeToListDFS(root, 0, res) treeToListDFS(root, 0, res)
return res return res
} }

View File

@@ -7,7 +7,7 @@
package utils package utils
/* 頂點類別 */ /* 頂點類別 */
class Vertex(val value: Int) { class Vertex(val _val: Int) {
companion object { companion object {
/* 輸入值串列 vals ,返回頂點串列 vets */ /* 輸入值串列 vals ,返回頂點串列 vets */
fun valsToVets(vals: IntArray): Array<Vertex?> { fun valsToVets(vals: IntArray): Array<Vertex?> {
@@ -19,10 +19,10 @@ class Vertex(val value: Int) {
} }
/* 輸入頂點串列 vets ,返回值串列 vals */ /* 輸入頂點串列 vets ,返回值串列 vals */
fun vetsToVals(vets: List<Vertex?>): List<Int> { fun vetsToVals(vets: MutableList<Vertex?>): MutableList<Int> {
val vals = ArrayList<Int>() val vals = mutableListOf<Int>()
for (vet in vets) { for (vet in vets) {
vals.add(vet!!.value) vals.add(vet!!._val)
} }
return vals return vals
} }

View File

@@ -5,19 +5,19 @@
---> --->
<!-- [file]{array}-[class]{}-[func]{random_access} --> <!-- [file]{array}-[class]{}-[func]{random_access} -->
https://pythontutor.com/render.html#code=import%20random%0A%0Adef%20random_access%28nums%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%9A%8F%E6%9C%BA%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%23%20%E5%9C%A8%E5%8C%BA%E9%97%B4%20%5B0,%20len%28nums%29-1%5D%20%E4%B8%AD%E9%9A%8F%E6%9C%BA%E6%8A%BD%E5%8F%96%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97%0A%20%20%20%20random_index%20%3D%20random.randint%280,%20len%28nums%29%20-%201%29%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%B9%B6%E8%BF%94%E5%9B%9E%E9%9A%8F%E6%9C%BA%E5%85%83%E7%B4%A0%0A%20%20%20%20random_num%20%3D%20nums%5Brandom_index%5D%0A%20%20%20%20return%20random_num%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20print%28%22%E6%95%B0%E7%BB%84%20nums%20%3D%22,%20nums%29%0A%0A%20%20%20%20%23%20%E9%9A%8F%E6%9C%BA%E8%AE%BF%E9%97%AE%0A%20%20%20%20random_num%3A%20int%20%3D%20random_access%28nums%29%0A%20%20%20%20print%28%22%E5%9C%A8%20nums%20%E4%B8%AD%E8%8E%B7%E5%8F%96%E9%9A%8F%E6%9C%BA%E5%85%83%E7%B4%A0%22,%20random_num%29%0A&cumulative=false&curInstr=7&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=import%20random%0A%0Adef%20random_access%28nums%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%9A%A8%E6%A9%9F%E8%A8%AA%E5%95%8F%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%23%20%E5%9C%A8%E5%8D%80%E9%96%93%20%5B0%2C%20len%28nums%29-1%5D%20%E4%B8%AD%E9%9A%A8%E6%A9%9F%E6%8A%BD%E5%8F%96%E4%B8%80%E5%80%8B%E6%95%B8%E5%AD%97%0A%20%20%20%20random_index%20%3D%20random.randint%280%2C%20len%28nums%29%20-%201%29%0A%20%20%20%20%23%20%E7%8D%B2%E5%8F%96%E4%B8%A6%E8%BF%94%E5%9B%9E%E9%9A%A8%E6%A9%9F%E5%85%83%E7%B4%A0%0A%20%20%20%20random_num%20%3D%20nums%5Brandom_index%5D%0A%20%20%20%20return%20random_num%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%99%A3%E5%88%97%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20print%28%22%E9%99%A3%E5%88%97%20nums%20%3D%22%2C%20nums%29%0A%0A%20%20%20%20%23%20%E9%9A%A8%E6%A9%9F%E8%A8%AA%E5%95%8F%0A%20%20%20%20random_num%3A%20int%20%3D%20random_access%28nums%29%0A%20%20%20%20print%28%22%E5%9C%A8%20nums%20%E4%B8%AD%E7%8D%B2%E5%8F%96%E9%9A%A8%E6%A9%9F%E5%85%83%E7%B4%A0%22%2C%20random_num%29%0A&cumulative=false&curInstr=7&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{array}-[class]{}-[func]{insert} --> <!-- [file]{array}-[class]{}-[func]{insert} -->
https://pythontutor.com/render.html#code=def%20insert%28nums%3A%20list%5Bint%5D,%20num%3A%20int,%20index%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E6%95%B0%E7%BB%84%E7%9A%84%E7%B4%A2%E5%BC%95%20index%20%E5%A4%84%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%20num%22%22%22%0A%20%20%20%20%23%20%E6%8A%8A%E7%B4%A2%E5%BC%95%20index%20%E4%BB%A5%E5%8F%8A%E4%B9%8B%E5%90%8E%E7%9A%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E5%90%91%E5%90%8E%E7%A7%BB%E5%8A%A8%E4%B8%80%E4%BD%8D%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%20-%201,%20index,%20-1%29%3A%0A%20%20%20%20%20%20%20%20nums%5Bi%5D%20%3D%20nums%5Bi%20-%201%5D%0A%20%20%20%20%23%20%E5%B0%86%20num%20%E8%B5%8B%E7%BB%99%20index%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5Bindex%5D%20%3D%20num%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20print%28%22%E6%95%B0%E7%BB%84%20nums%20%3D%22,%20nums%29%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20insert%28nums,%206,%203%29%0A%20%20%20%20print%28%22%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E6%8F%92%E5%85%A5%E6%95%B0%E5%AD%97%206%20%EF%BC%8C%E5%BE%97%E5%88%B0%20nums%20%3D%22,%20nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20insert%28nums%3A%20list%5Bint%5D%2C%20num%3A%20int%2C%20index%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%99%A3%E5%88%97%E7%9A%84%E7%B4%A2%E5%BC%95%20index%20%E8%99%95%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%20num%22%22%22%0A%20%20%20%20%23%20%E6%8A%8A%E7%B4%A2%E5%BC%95%20index%20%E4%BB%A5%E5%8F%8A%E4%B9%8B%E5%BE%8C%E7%9A%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E5%90%91%E5%BE%8C%E7%A7%BB%E5%8B%95%E4%B8%80%E4%BD%8D%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%20-%201%2C%20index%2C%20-1%29%3A%0A%20%20%20%20%20%20%20%20nums%5Bi%5D%20%3D%20nums%5Bi%20-%201%5D%0A%20%20%20%20%23%20%E5%B0%87%20num%20%E8%B3%A6%E7%B5%A6%20index%20%E8%99%95%E7%9A%84%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5Bindex%5D%20%3D%20num%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%99%A3%E5%88%97%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20print%28%22%E9%99%A3%E5%88%97%20nums%20%3D%22%2C%20nums%29%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20insert%28nums%2C%206%2C%203%29%0A%20%20%20%20print%28%22%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E8%99%95%E6%8F%92%E5%85%A5%E6%95%B8%E5%AD%97%206%20%EF%BC%8C%E5%BE%97%E5%88%B0%20nums%20%3D%22%2C%20nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{array}-[class]{}-[func]{remove} --> <!-- [file]{array}-[class]{}-[func]{remove} -->
https://pythontutor.com/render.html#code=def%20remove%28nums%3A%20list%5Bint%5D,%20index%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%20index%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%23%20%E6%8A%8A%E7%B4%A2%E5%BC%95%20index%20%E4%B9%8B%E5%90%8E%E7%9A%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E5%90%91%E5%89%8D%E7%A7%BB%E5%8A%A8%E4%B8%80%E4%BD%8D%0A%20%20%20%20for%20i%20in%20range%28index,%20len%28nums%29%20-%201%29%3A%0A%20%20%20%20%20%20%20%20nums%5Bi%5D%20%3D%20nums%5Bi%20%2B%201%5D%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20print%28%22%E6%95%B0%E7%BB%84%20nums%20%3D%22,%20nums%29%0A%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20remove%28nums,%202%29%0A%20%20%20%20print%28%22%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%202%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%EF%BC%8C%E5%BE%97%E5%88%B0%20nums%20%3D%22,%20nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20remove%28nums%3A%20list%5Bint%5D%2C%20index%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%88%AA%E9%99%A4%E7%B4%A2%E5%BC%95%20index%20%E8%99%95%E7%9A%84%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%23%20%E6%8A%8A%E7%B4%A2%E5%BC%95%20index%20%E4%B9%8B%E5%BE%8C%E7%9A%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E5%90%91%E5%89%8D%E7%A7%BB%E5%8B%95%E4%B8%80%E4%BD%8D%0A%20%20%20%20for%20i%20in%20range%28index%2C%20len%28nums%29%20-%201%29%3A%0A%20%20%20%20%20%20%20%20nums%5Bi%5D%20%3D%20nums%5Bi%20%2B%201%5D%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%99%A3%E5%88%97%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20print%28%22%E9%99%A3%E5%88%97%20nums%20%3D%22%2C%20nums%29%0A%0A%20%20%20%20%23%20%E5%88%AA%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20remove%28nums%2C%202%29%0A%20%20%20%20print%28%22%E5%88%AA%E9%99%A4%E7%B4%A2%E5%BC%95%202%20%E8%99%95%E7%9A%84%E5%85%83%E7%B4%A0%EF%BC%8C%E5%BE%97%E5%88%B0%20nums%20%3D%22%2C%20nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{array}-[class]{}-[func]{traverse} --> <!-- [file]{array}-[class]{}-[func]{traverse} -->
https://pythontutor.com/render.html#code=def%20traverse%28nums%3A%20list%5Bint%5D%29%3A%0A%20%20%20%20%22%22%22%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num%0A%20%20%20%20%23%20%E5%90%8C%E6%97%B6%E9%81%8D%E5%8E%86%E6%95%B0%E6%8D%AE%E7%B4%A2%E5%BC%95%E5%92%8C%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20i,%20num%20in%20enumerate%28nums%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20print%28%22%E6%95%B0%E7%BB%84%20nums%20%3D%22,%20nums%29%0A%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84%0A%20%20%20%20traverse%28nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20traverse%28nums%3A%20list%5Bint%5D%29%3A%0A%20%20%20%20%22%22%22%E8%B5%B0%E8%A8%AA%E9%99%A3%E5%88%97%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E9%80%8F%E9%81%8E%E7%B4%A2%E5%BC%95%E8%B5%B0%E8%A8%AA%E9%99%A3%E5%88%97%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E8%B5%B0%E8%A8%AA%E9%99%A3%E5%88%97%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num%0A%20%20%20%20%23%20%E5%90%8C%E6%99%82%E8%B5%B0%E8%A8%AA%E8%B3%87%E6%96%99%E7%B4%A2%E5%BC%95%E5%92%8C%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20i%2C%20num%20in%20enumerate%28nums%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%99%A3%E5%88%97%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20print%28%22%E9%99%A3%E5%88%97%20nums%20%3D%22%2C%20nums%29%0A%0A%20%20%20%20%23%20%E8%B5%B0%E8%A8%AA%E9%99%A3%E5%88%97%0A%20%20%20%20traverse%28nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{array}-[class]{}-[func]{find} --> <!-- [file]{array}-[class]{}-[func]{find} -->
https://pythontutor.com/render.html#code=def%20find%28nums%3A%20list%5Bint%5D,%20target%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E6%8C%87%E5%AE%9A%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20if%20nums%5Bi%5D%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20return%20-1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20print%28%22%E6%95%B0%E7%BB%84%20nums%20%3D%22,%20nums%29%0A%0A%20%20%20%20%23%20%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%0A%20%20%20%20index%3A%20int%20%3D%20find%28nums,%203%29%0A%20%20%20%20print%28%22%E5%9C%A8%20nums%20%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%203%20%EF%BC%8C%E5%BE%97%E5%88%B0%E7%B4%A2%E5%BC%95%20%3D%22,%20index%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20find%28nums%3A%20list%5Bint%5D%2C%20target%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%99%A3%E5%88%97%E4%B8%AD%E6%9F%A5%E8%A9%A2%E6%8C%87%E5%AE%9A%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20if%20nums%5Bi%5D%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20return%20-1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%99%A3%E5%88%97%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20print%28%22%E9%99%A3%E5%88%97%20nums%20%3D%22%2C%20nums%29%0A%0A%20%20%20%20%23%20%E6%9F%A5%E8%A9%A2%E5%85%83%E7%B4%A0%0A%20%20%20%20index%3A%20int%20%3D%20find%28nums%2C%203%29%0A%20%20%20%20print%28%22%E5%9C%A8%20nums%20%E4%B8%AD%E6%9F%A5%E8%A9%A2%E5%85%83%E7%B4%A0%203%20%EF%BC%8C%E5%BE%97%E5%88%B0%E7%B4%A2%E5%BC%95%20%3D%22%2C%20index%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{array}-[class]{}-[func]{extend} --> <!-- [file]{array}-[class]{}-[func]{extend} -->
https://pythontutor.com/render.html#code=%23%20%E8%AF%B7%E6%B3%A8%E6%84%8F%EF%BC%8CPython%20%E7%9A%84%20list%20%E6%98%AF%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84%EF%BC%8C%E5%8F%AF%E4%BB%A5%E7%9B%B4%E6%8E%A5%E6%89%A9%E5%B1%95%0A%23%20%E4%B8%BA%E4%BA%86%E6%96%B9%E4%BE%BF%E5%AD%A6%E4%B9%A0%EF%BC%8C%E6%9C%AC%E5%87%BD%E6%95%B0%E5%B0%86%20list%20%E7%9C%8B%E4%BD%9C%E9%95%BF%E5%BA%A6%E4%B8%8D%E5%8F%AF%E5%8F%98%E7%9A%84%E6%95%B0%E7%BB%84%0Adef%20extend%28nums%3A%20list%5Bint%5D,%20enlarge%3A%20int%29%20-%3E%20list%5Bint%5D%3A%0A%20%20%20%20%22%22%22%E6%89%A9%E5%B1%95%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%B8%80%E4%B8%AA%E6%89%A9%E5%B1%95%E9%95%BF%E5%BA%A6%E5%90%8E%E7%9A%84%E6%95%B0%E7%BB%84%0A%20%20%20%20res%20%3D%20%5B0%5D%20*%20%28len%28nums%29%20%2B%20enlarge%29%0A%20%20%20%20%23%20%E5%B0%86%E5%8E%9F%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E5%A4%8D%E5%88%B6%E5%88%B0%E6%96%B0%E6%95%B0%E7%BB%84%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20res%5Bi%5D%20%3D%20nums%5Bi%5D%0A%20%20%20%20%23%20%E8%BF%94%E5%9B%9E%E6%89%A9%E5%B1%95%E5%90%8E%E7%9A%84%E6%96%B0%E6%95%B0%E7%BB%84%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20print%28%22%E6%95%B0%E7%BB%84%20nums%20%3D%22,%20nums%29%0A%0A%20%20%20%20%23%20%E9%95%BF%E5%BA%A6%E6%89%A9%E5%B1%95%0A%20%20%20%20nums%20%3D%20extend%28nums,%203%29%0A%20%20%20%20print%28%22%E5%B0%86%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E6%89%A9%E5%B1%95%E8%87%B3%208%20%EF%BC%8C%E5%BE%97%E5%88%B0%20nums%20%3D%22,%20nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=%23%20%E8%AB%8B%E6%B3%A8%E6%84%8F%EF%BC%8CPython%20%E7%9A%84%20list%20%E6%98%AF%E5%8B%95%E6%85%8B%E9%99%A3%E5%88%97%EF%BC%8C%E5%8F%AF%E4%BB%A5%E7%9B%B4%E6%8E%A5%E6%93%B4%E5%B1%95%0A%23%20%E7%82%BA%E4%BA%86%E6%96%B9%E4%BE%BF%E5%AD%B8%E7%BF%92%EF%BC%8C%E6%9C%AC%E5%87%BD%E5%BC%8F%E5%B0%87%20list%20%E7%9C%8B%E4%BD%9C%E9%95%B7%E5%BA%A6%E4%B8%8D%E5%8F%AF%E8%AE%8A%E7%9A%84%E9%99%A3%E5%88%97%0Adef%20extend%28nums%3A%20list%5Bint%5D%2C%20enlarge%3A%20int%29%20-%3E%20list%5Bint%5D%3A%0A%20%20%20%20%22%22%22%E6%93%B4%E5%B1%95%E9%99%A3%E5%88%97%E9%95%B7%E5%BA%A6%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%B8%80%E5%80%8B%E6%93%B4%E5%B1%95%E9%95%B7%E5%BA%A6%E5%BE%8C%E7%9A%84%E9%99%A3%E5%88%97%0A%20%20%20%20res%20%3D%20%5B0%5D%20%2A%20%28len%28nums%29%20%2B%20enlarge%29%0A%20%20%20%20%23%20%E5%B0%87%E5%8E%9F%E9%99%A3%E5%88%97%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E8%A4%87%E8%A3%BD%E5%88%B0%E6%96%B0%E9%99%A3%E5%88%97%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20res%5Bi%5D%20%3D%20nums%5Bi%5D%0A%20%20%20%20%23%20%E8%BF%94%E5%9B%9E%E6%93%B4%E5%B1%95%E5%BE%8C%E7%9A%84%E6%96%B0%E9%99%A3%E5%88%97%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%99%A3%E5%88%97%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20print%28%22%E9%99%A3%E5%88%97%20nums%20%3D%22%2C%20nums%29%0A%0A%20%20%20%20%23%20%E9%95%B7%E5%BA%A6%E6%93%B4%E5%B1%95%0A%20%20%20%20nums%20%3D%20extend%28nums%2C%203%29%0A%20%20%20%20print%28%22%E5%B0%87%E9%99%A3%E5%88%97%E9%95%B7%E5%BA%A6%E6%93%B4%E5%B1%95%E8%87%B3%208%20%EF%BC%8C%E5%BE%97%E5%88%B0%20nums%20%3D%22%2C%20nums%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,13 +5,13 @@
---> --->
<!-- [file]{linked_list}-[class]{}-[func]{insert} --> <!-- [file]{linked_list}-[class]{}-[func]{insert} -->
https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20insert%28n0%3A%20ListNode,%20P%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%93%BE%E8%A1%A8%E7%9A%84%E8%8A%82%E7%82%B9%20n0%20%E4%B9%8B%E5%90%8E%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%22%22%22%0A%20%20%20%20n1%20%3D%20n0.next%0A%20%20%20%20P.next%20%3D%20n1%0A%20%20%20%20n0.next%20%3D%20P%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20ListNode%280%29%0A%20%20%20%20insert%28n0,%20p%29&cumulative=false&curInstr=39&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%BE%8C%E7%B9%BC%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0Adef%20insert%28n0%3A%20ListNode%2C%20P%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E7%9A%84%E7%AF%80%E9%BB%9E%20n0%20%E4%B9%8B%E5%BE%8C%E6%8F%92%E5%85%A5%E7%AF%80%E9%BB%9E%20P%22%22%22%0A%20%20%20%20n1%20%3D%20n0.next%0A%20%20%20%20P.next%20%3D%20n1%0A%20%20%20%20n0.next%20%3D%20P%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E5%80%8B%E7%AF%80%E9%BB%9E%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%AF%80%E9%BB%9E%E4%B9%8B%E9%96%93%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E7%AF%80%E9%BB%9E%0A%20%20%20%20p%20%3D%20ListNode%280%29%0A%20%20%20%20insert%28n0%2C%20p%29&cumulative=false&curInstr=39&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{linked_list}-[class]{}-[func]{remove} --> <!-- [file]{linked_list}-[class]{}-[func]{remove} -->
https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20remove%28n0%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E8%8A%82%E7%82%B9%20n0%20%E4%B9%8B%E5%90%8E%E7%9A%84%E9%A6%96%E4%B8%AA%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20if%20not%20n0.next%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20n0%20-%3E%20P%20-%3E%20n1%0A%20%20%20%20P%20%3D%20n0.next%0A%20%20%20%20n1%20%3D%20P.next%0A%20%20%20%20n0.next%20%3D%20n1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20remove%28n0%29&cumulative=false&curInstr=34&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%BE%8C%E7%B9%BC%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0Adef%20remove%28n0%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%88%AA%E9%99%A4%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E7%9A%84%E7%AF%80%E9%BB%9E%20n0%20%E4%B9%8B%E5%BE%8C%E7%9A%84%E9%A6%96%E5%80%8B%E7%AF%80%E9%BB%9E%22%22%22%0A%20%20%20%20if%20not%20n0.next%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20n0%20-%3E%20P%20-%3E%20n1%0A%20%20%20%20P%20%3D%20n0.next%0A%20%20%20%20n1%20%3D%20P.next%0A%20%20%20%20n0.next%20%3D%20n1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E5%80%8B%E7%AF%80%E9%BB%9E%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%AF%80%E9%BB%9E%E4%B9%8B%E9%96%93%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E5%88%AA%E9%99%A4%E7%AF%80%E9%BB%9E%0A%20%20%20%20remove%28n0%29&cumulative=false&curInstr=34&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{linked_list}-[class]{}-[func]{access} --> <!-- [file]{linked_list}-[class]{}-[func]{access} -->
https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20access%28head%3A%20ListNode,%20index%3A%20int%29%20-%3E%20ListNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E8%AE%BF%E9%97%AE%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%B4%A2%E5%BC%95%E4%B8%BA%20index%20%E7%9A%84%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20for%20_%20in%20range%28index%29%3A%0A%20%20%20%20%20%20%20%20if%20not%20head%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20return%20head%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E8%8A%82%E7%82%B9%0A%20%20%20%20node%20%3D%20access%28n0,%203%29%0A%20%20%20%20print%28%22%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E8%8A%82%E7%82%B9%E7%9A%84%E5%80%BC%20%3D%20%7B%7D%22.format%28node.val%29%29&cumulative=false&curInstr=34&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%BE%8C%E7%B9%BC%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0Adef%20access%28head%3A%20ListNode%2C%20index%3A%20int%29%20-%3E%20ListNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E8%A8%AA%E5%95%8F%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E4%B8%AD%E7%B4%A2%E5%BC%95%E7%82%BA%20index%20%E7%9A%84%E7%AF%80%E9%BB%9E%22%22%22%0A%20%20%20%20for%20_%20in%20range%28index%29%3A%0A%20%20%20%20%20%20%20%20if%20not%20head%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20return%20head%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E5%80%8B%E7%AF%80%E9%BB%9E%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%AF%80%E9%BB%9E%E4%B9%8B%E9%96%93%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E8%A8%AA%E5%95%8F%E7%AF%80%E9%BB%9E%0A%20%20%20%20node%20%3D%20access%28n0%2C%203%29%0A%20%20%20%20print%28%22%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E4%B8%AD%E7%B4%A2%E5%BC%95%203%20%E8%99%95%E7%9A%84%E7%AF%80%E9%BB%9E%E7%9A%84%E5%80%BC%20%3D%20%7B%7D%22.format%28node.val%29%29&cumulative=false&curInstr=34&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{linked_list}-[class]{}-[func]{find} --> <!-- [file]{linked_list}-[class]{}-[func]{find} -->
https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20find%28head%3A%20ListNode,%20target%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%80%BC%E4%B8%BA%20target%20%E7%9A%84%E9%A6%96%E4%B8%AA%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20index%20%3D%200%0A%20%20%20%20while%20head%3A%0A%20%20%20%20%20%20%20%20if%20head.val%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20index%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20%20%20%20%20index%20%2B%3D%201%0A%20%20%20%20return%20-1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%9F%A5%E6%89%BE%E8%8A%82%E7%82%B9%0A%20%20%20%20index%20%3D%20find%28n0,%202%29%0A%20%20%20%20print%28%22%E9%93%BE%E8%A1%A8%E4%B8%AD%E5%80%BC%E4%B8%BA%202%20%E7%9A%84%E8%8A%82%E7%82%B9%E7%9A%84%E7%B4%A2%E5%BC%95%20%3D%20%7B%7D%22.format%28index%29%29&cumulative=false&curInstr=34&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%BE%8C%E7%B9%BC%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0Adef%20find%28head%3A%20ListNode%2C%20target%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E4%B8%AD%E6%9F%A5%E8%A9%A2%E5%80%BC%E7%82%BA%20target%20%E7%9A%84%E9%A6%96%E5%80%8B%E7%AF%80%E9%BB%9E%22%22%22%0A%20%20%20%20index%20%3D%200%0A%20%20%20%20while%20head%3A%0A%20%20%20%20%20%20%20%20if%20head.val%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20index%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20%20%20%20%20index%20%2B%3D%201%0A%20%20%20%20return%20-1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E5%80%8B%E7%AF%80%E9%BB%9E%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%AF%80%E9%BB%9E%E4%B9%8B%E9%96%93%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%9F%A5%E8%A9%A2%E7%AF%80%E9%BB%9E%0A%20%20%20%20index%20%3D%20find%28n0%2C%202%29%0A%20%20%20%20print%28%22%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E4%B8%AD%E5%80%BC%E7%82%BA%202%20%E7%9A%84%E7%AF%80%E9%BB%9E%E7%9A%84%E7%B4%A2%E5%BC%95%20%3D%20%7B%7D%22.format%28index%29%29&cumulative=false&curInstr=34&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

File diff suppressed because one or more lines are too long

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{n_queens}-[class]{}-[func]{n_queens} --> <!-- [file]{n_queens}-[class]{}-[func]{n_queens} -->
https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20row%3A%20int,%0A%20%20%20%20n%3A%20int,%0A%20%20%20%20state%3A%20list%5Blist%5Bstr%5D%5D,%0A%20%20%20%20res%3A%20list%5Blist%5Blist%5Bstr%5D%5D%5D,%0A%20%20%20%20cols%3A%20list%5Bbool%5D,%0A%20%20%20%20diags1%3A%20list%5Bbool%5D,%0A%20%20%20%20diags2%3A%20list%5Bbool%5D,%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9AN%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E5%BD%93%E6%94%BE%E7%BD%AE%E5%AE%8C%E6%89%80%E6%9C%89%E8%A1%8C%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20row%20%3D%3D%20n%3A%0A%20%20%20%20%20%20%20%20res.append%28%5Blist%28row%29%20for%20row%20in%20state%5D%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E5%88%97%0A%20%20%20%20for%20col%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%AE%A1%E7%AE%97%E8%AF%A5%E6%A0%BC%E5%AD%90%E5%AF%B9%E5%BA%94%E7%9A%84%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E5%92%8C%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%0A%20%20%20%20%20%20%20%20diag1%20%3D%20row%20-%20col%20%2B%20n%20-%201%0A%20%20%20%20%20%20%20%20diag2%20%3D%20row%20%2B%20col%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%AE%B8%E8%AF%A5%E6%A0%BC%E5%AD%90%E6%89%80%E5%9C%A8%E5%88%97%E3%80%81%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E3%80%81%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E5%AD%98%E5%9C%A8%E7%9A%87%E5%90%8E%0A%20%20%20%20%20%20%20%20if%20not%20cols%5Bcol%5D%20and%20not%20diags1%5Bdiag1%5D%20and%20not%20diags2%5Bdiag2%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%B0%86%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E5%9C%A8%E8%AF%A5%E6%A0%BC%E5%AD%90%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22Q%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E6%94%BE%E7%BD%AE%E4%B8%8B%E4%B8%80%E8%A1%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28row%20%2B%201,%20n,%20state,%20res,%20cols,%20diags1,%20diags2%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E5%B0%86%E8%AF%A5%E6%A0%BC%E5%AD%90%E6%81%A2%E5%A4%8D%E4%B8%BA%E7%A9%BA%E4%BD%8D%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22%23%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20False%0A%0A%0Adef%20n_queens%28n%3A%20int%29%20-%3E%20list%5Blist%5Blist%5Bstr%5D%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%20N%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%20n*n%20%E5%A4%A7%E5%B0%8F%E7%9A%84%E6%A3%8B%E7%9B%98%EF%BC%8C%E5%85%B6%E4%B8%AD%20'Q'%20%E4%BB%A3%E8%A1%A8%E7%9A%87%E5%90%8E%EF%BC%8C'%23'%20%E4%BB%A3%E8%A1%A8%E7%A9%BA%E4%BD%8D%0A%20%20%20%20state%20%3D%20%5B%5B%22%23%22%20for%20_%20in%20range%28n%29%5D%20for%20_%20in%20range%28n%29%5D%0A%20%20%20%20cols%20%3D%20%5BFalse%5D%20*%20n%20%20%23%20%E8%AE%B0%E5%BD%95%E5%88%97%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags1%20%3D%20%5BFalse%5D%20*%20%282%20*%20n%20-%201%29%20%20%23%20%E8%AE%B0%E5%BD%95%E4%B8%BB%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags2%20%3D%20%5BFalse%5D%20*%20%282%20*%20n%20-%201%29%20%20%23%20%E8%AE%B0%E5%BD%95%E6%AC%A1%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%280,%20n,%20state,%20res,%20cols,%20diags1,%20diags2%29%0A%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%204%0A%20%20%20%20res%20%3D%20n_queens%28n%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%A3%8B%E7%9B%98%E9%95%BF%E5%AE%BD%E4%B8%BA%20%7Bn%7D%22%29%0A%20%20%20%20print%28f%22%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E6%96%B9%E6%A1%88%E5%85%B1%E6%9C%89%20%7Blen%28res%29%7D%20%E7%A7%8D%22%29%0A%20%20%20%20for%20state%20in%20res%3A%0A%20%20%20%20%20%20%20%20print%28%22--------------------%22%29%0A%20%20%20%20%20%20%20%20for%20row%20in%20state%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%28row%29&cumulative=false&curInstr=61&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20row%3A%20int%2C%0A%20%20%20%20n%3A%20int%2C%0A%20%20%20%20state%3A%20list%5Blist%5Bstr%5D%5D%2C%0A%20%20%20%20res%3A%20list%5Blist%5Blist%5Bstr%5D%5D%5D%2C%0A%20%20%20%20cols%3A%20list%5Bbool%5D%2C%0A%20%20%20%20diags1%3A%20list%5Bbool%5D%2C%0A%20%20%20%20diags2%3A%20list%5Bbool%5D%2C%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E6%BC%94%E7%AE%97%E6%B3%95%EF%BC%9AN%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E7%95%B6%E6%94%BE%E7%BD%AE%E5%AE%8C%E6%89%80%E6%9C%89%E8%A1%8C%E6%99%82%EF%BC%8C%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20if%20row%20%3D%3D%20n%3A%0A%20%20%20%20%20%20%20%20res.append%28%5Blist%28row%29%20for%20row%20in%20state%5D%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E8%B5%B0%E8%A8%AA%E6%89%80%E6%9C%89%E5%88%97%0A%20%20%20%20for%20col%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%A8%88%E7%AE%97%E8%A9%B2%E6%A0%BC%E5%AD%90%E5%B0%8D%E6%87%89%E7%9A%84%E4%B8%BB%E5%B0%8D%E8%A7%92%E7%B7%9A%E5%92%8C%E6%AC%A1%E5%B0%8D%E8%A7%92%E7%B7%9A%0A%20%20%20%20%20%20%20%20diag1%20%3D%20row%20-%20col%20%2B%20n%20-%201%0A%20%20%20%20%20%20%20%20diag2%20%3D%20row%20%2B%20col%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%A8%B1%E8%A9%B2%E6%A0%BC%E5%AD%90%E6%89%80%E5%9C%A8%E5%88%97%E3%80%81%E4%B8%BB%E5%B0%8D%E8%A7%92%E7%B7%9A%E3%80%81%E6%AC%A1%E5%B0%8D%E8%A7%92%E7%B7%9A%E4%B8%8A%E5%AD%98%E5%9C%A8%E7%9A%87%E5%90%8E%0A%20%20%20%20%20%20%20%20if%20not%20cols%5Bcol%5D%20and%20not%20diags1%5Bdiag1%5D%20and%20not%20diags2%5Bdiag2%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%98%97%E8%A9%A6%EF%BC%9A%E5%B0%87%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E5%9C%A8%E8%A9%B2%E6%A0%BC%E5%AD%90%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22Q%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E6%94%BE%E7%BD%AE%E4%B8%8B%E4%B8%80%E8%A1%8C%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28row%20%2B%201%2C%20n%2C%20state%2C%20res%2C%20cols%2C%20diags1%2C%20diags2%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E5%B0%87%E8%A9%B2%E6%A0%BC%E5%AD%90%E6%81%A2%E5%BE%A9%E7%82%BA%E7%A9%BA%E4%BD%8D%0A%20%20%20%20%20%20%20%20%20%20%20%20state%5Brow%5D%5Bcol%5D%20%3D%20%22%23%22%0A%20%20%20%20%20%20%20%20%20%20%20%20cols%5Bcol%5D%20%3D%20diags1%5Bdiag1%5D%20%3D%20diags2%5Bdiag2%5D%20%3D%20False%0A%0A%0Adef%20n_queens%28n%3A%20int%29%20-%3E%20list%5Blist%5Blist%5Bstr%5D%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%20N%20%E7%9A%87%E5%90%8E%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%20n%2An%20%E5%A4%A7%E5%B0%8F%E7%9A%84%E6%A3%8B%E7%9B%A4%EF%BC%8C%E5%85%B6%E4%B8%AD%20%27Q%27%20%E4%BB%A3%E8%A1%A8%E7%9A%87%E5%90%8E%EF%BC%8C%27%23%27%20%E4%BB%A3%E8%A1%A8%E7%A9%BA%E4%BD%8D%0A%20%20%20%20state%20%3D%20%5B%5B%22%23%22%20for%20_%20in%20range%28n%29%5D%20for%20_%20in%20range%28n%29%5D%0A%20%20%20%20cols%20%3D%20%5BFalse%5D%20%2A%20n%20%20%23%20%E8%A8%98%E9%8C%84%E5%88%97%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags1%20%3D%20%5BFalse%5D%20%2A%20%282%20%2A%20n%20-%201%29%20%20%23%20%E8%A8%98%E9%8C%84%E4%B8%BB%E5%B0%8D%E8%A7%92%E7%B7%9A%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20diags2%20%3D%20%5BFalse%5D%20%2A%20%282%20%2A%20n%20-%201%29%20%20%23%20%E8%A8%98%E9%8C%84%E6%AC%A1%E5%B0%8D%E8%A7%92%E7%B7%9A%E4%B8%8A%E6%98%AF%E5%90%A6%E6%9C%89%E7%9A%87%E5%90%8E%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%280%2C%20n%2C%20state%2C%20res%2C%20cols%2C%20diags1%2C%20diags2%29%0A%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%204%0A%20%20%20%20res%20%3D%20n_queens%28n%29%0A%0A%20%20%20%20print%28f%22%E8%BC%B8%E5%85%A5%E6%A3%8B%E7%9B%A4%E9%95%B7%E5%AF%AC%E7%82%BA%20%7Bn%7D%22%29%0A%20%20%20%20print%28f%22%E7%9A%87%E5%90%8E%E6%94%BE%E7%BD%AE%E6%96%B9%E6%A1%88%E5%85%B1%E6%9C%89%20%7Blen%28res%29%7D%20%E7%A8%AE%22%29%0A%20%20%20%20for%20state%20in%20res%3A%0A%20%20%20%20%20%20%20%20print%28%22--------------------%22%29%0A%20%20%20%20%20%20%20%20for%20row%20in%20state%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%28row%29&cumulative=false&curInstr=61&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{permutations_i}-[class]{}-[func]{permutations_i} --> <!-- [file]{permutations_i}-[class]{}-[func]{permutations_i} -->
https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D,%20choices%3A%20list%5Bint%5D,%20selected%3A%20list%5Bbool%5D,%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9A%E5%85%A8%E6%8E%92%E5%88%97%20I%22%22%22%0A%20%20%20%20%23%20%E5%BD%93%E7%8A%B6%E6%80%81%E9%95%BF%E5%BA%A6%E7%AD%89%E4%BA%8E%E5%85%83%E7%B4%A0%E6%95%B0%E9%87%8F%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20len%28state%29%20%3D%3D%20len%28choices%29%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%80%89%E6%8B%A9%0A%20%20%20%20for%20i,%20choice%20in%20enumerate%28choices%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%AE%B8%E9%87%8D%E5%A4%8D%E9%80%89%E6%8B%A9%E5%85%83%E7%B4%A0%0A%20%20%20%20%20%20%20%20if%20not%20selected%5Bi%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%81%9A%E5%87%BA%E9%80%89%E6%8B%A9%EF%BC%8C%E6%9B%B4%E6%96%B0%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20state.append%28choice%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%BF%9B%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BD%AE%E9%80%89%E6%8B%A9%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28state,%20choices,%20selected,%20res%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%94%80%E9%80%89%E6%8B%A9%EF%BC%8C%E6%81%A2%E5%A4%8D%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20permutations_i%28nums%3A%20list%5Bint%5D%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E5%85%A8%E6%8E%92%E5%88%97%20I%22%22%22%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%28state%3D%5B%5D,%20choices%3Dnums,%20selected%3D%5BFalse%5D%20*%20len%28nums%29,%20res%3Dres%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B1,%202,%203%5D%0A%0A%20%20%20%20res%20%3D%20permutations_i%28nums%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%95%B0%E7%BB%84%20nums%20%3D%20%7Bnums%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E6%8E%92%E5%88%97%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=13&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D%2C%20choices%3A%20list%5Bint%5D%2C%20selected%3A%20list%5Bbool%5D%2C%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E6%BC%94%E7%AE%97%E6%B3%95%EF%BC%9A%E5%85%A8%E6%8E%92%E5%88%97%20I%22%22%22%0A%20%20%20%20%23%20%E7%95%B6%E7%8B%80%E6%85%8B%E9%95%B7%E5%BA%A6%E7%AD%89%E6%96%BC%E5%85%83%E7%B4%A0%E6%95%B8%E9%87%8F%E6%99%82%EF%BC%8C%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20if%20len%28state%29%20%3D%3D%20len%28choices%29%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E8%B5%B0%E8%A8%AA%E6%89%80%E6%9C%89%E9%81%B8%E6%93%87%0A%20%20%20%20for%20i%2C%20choice%20in%20enumerate%28choices%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%A8%B1%E9%87%8D%E8%A4%87%E9%81%B8%E6%93%87%E5%85%83%E7%B4%A0%0A%20%20%20%20%20%20%20%20if%20not%20selected%5Bi%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%98%97%E8%A9%A6%EF%BC%9A%E5%81%9A%E5%87%BA%E9%81%B8%E6%93%87%EF%BC%8C%E6%9B%B4%E6%96%B0%E7%8B%80%E6%85%8B%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20state.append%28choice%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E9%80%B2%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BC%AA%E9%81%B8%E6%93%87%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28state%2C%20choices%2C%20selected%2C%20res%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%8A%B7%E9%81%B8%E6%93%87%EF%BC%8C%E6%81%A2%E5%BE%A9%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8B%80%E6%85%8B%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20permutations_i%28nums%3A%20list%5Bint%5D%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E5%85%A8%E6%8E%92%E5%88%97%20I%22%22%22%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%28state%3D%5B%5D%2C%20choices%3Dnums%2C%20selected%3D%5BFalse%5D%20%2A%20len%28nums%29%2C%20res%3Dres%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B1%2C%202%2C%203%5D%0A%0A%20%20%20%20res%20%3D%20permutations_i%28nums%29%0A%0A%20%20%20%20print%28f%22%E8%BC%B8%E5%85%A5%E9%99%A3%E5%88%97%20nums%20%3D%20%7Bnums%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E6%8E%92%E5%88%97%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=13&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{permutations_ii}-[class]{}-[func]{permutations_ii} --> <!-- [file]{permutations_ii}-[class]{}-[func]{permutations_ii} -->
https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D,%20choices%3A%20list%5Bint%5D,%20selected%3A%20list%5Bbool%5D,%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9A%E5%85%A8%E6%8E%92%E5%88%97%20II%22%22%22%0A%20%20%20%20%23%20%E5%BD%93%E7%8A%B6%E6%80%81%E9%95%BF%E5%BA%A6%E7%AD%89%E4%BA%8E%E5%85%83%E7%B4%A0%E6%95%B0%E9%87%8F%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20len%28state%29%20%3D%3D%20len%28choices%29%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%80%89%E6%8B%A9%0A%20%20%20%20duplicated%20%3D%20set%5Bint%5D%28%29%0A%20%20%20%20for%20i,%20choice%20in%20enumerate%28choices%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%AE%B8%E9%87%8D%E5%A4%8D%E9%80%89%E6%8B%A9%E5%85%83%E7%B4%A0%20%E4%B8%94%20%E4%B8%8D%E5%85%81%E8%AE%B8%E9%87%8D%E5%A4%8D%E9%80%89%E6%8B%A9%E7%9B%B8%E7%AD%89%E5%85%83%E7%B4%A0%0A%20%20%20%20%20%20%20%20if%20not%20selected%5Bi%5D%20and%20choice%20not%20in%20duplicated%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%81%9A%E5%87%BA%E9%80%89%E6%8B%A9%EF%BC%8C%E6%9B%B4%E6%96%B0%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20%20%20%20%20duplicated.add%28choice%29%20%20%23%20%E8%AE%B0%E5%BD%95%E9%80%89%E6%8B%A9%E8%BF%87%E7%9A%84%E5%85%83%E7%B4%A0%E5%80%BC%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20state.append%28choice%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%BF%9B%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BD%AE%E9%80%89%E6%8B%A9%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28state,%20choices,%20selected,%20res%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%94%80%E9%80%89%E6%8B%A9%EF%BC%8C%E6%81%A2%E5%A4%8D%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20permutations_ii%28nums%3A%20list%5Bint%5D%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E5%85%A8%E6%8E%92%E5%88%97%20II%22%22%22%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%28state%3D%5B%5D,%20choices%3Dnums,%20selected%3D%5BFalse%5D%20*%20len%28nums%29,%20res%3Dres%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B1,%202,%202%5D%0A%0A%20%20%20%20res%20%3D%20permutations_ii%28nums%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%95%B0%E7%BB%84%20nums%20%3D%20%7Bnums%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E6%8E%92%E5%88%97%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=13&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D%2C%20choices%3A%20list%5Bint%5D%2C%20selected%3A%20list%5Bbool%5D%2C%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E6%BC%94%E7%AE%97%E6%B3%95%EF%BC%9A%E5%85%A8%E6%8E%92%E5%88%97%20II%22%22%22%0A%20%20%20%20%23%20%E7%95%B6%E7%8B%80%E6%85%8B%E9%95%B7%E5%BA%A6%E7%AD%89%E6%96%BC%E5%85%83%E7%B4%A0%E6%95%B8%E9%87%8F%E6%99%82%EF%BC%8C%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20if%20len%28state%29%20%3D%3D%20len%28choices%29%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E8%B5%B0%E8%A8%AA%E6%89%80%E6%9C%89%E9%81%B8%E6%93%87%0A%20%20%20%20duplicated%20%3D%20set%5Bint%5D%28%29%0A%20%20%20%20for%20i%2C%20choice%20in%20enumerate%28choices%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%A8%B1%E9%87%8D%E8%A4%87%E9%81%B8%E6%93%87%E5%85%83%E7%B4%A0%20%E4%B8%94%20%E4%B8%8D%E5%85%81%E8%A8%B1%E9%87%8D%E8%A4%87%E9%81%B8%E6%93%87%E7%9B%B8%E7%AD%89%E5%85%83%E7%B4%A0%0A%20%20%20%20%20%20%20%20if%20not%20selected%5Bi%5D%20and%20choice%20not%20in%20duplicated%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%98%97%E8%A9%A6%EF%BC%9A%E5%81%9A%E5%87%BA%E9%81%B8%E6%93%87%EF%BC%8C%E6%9B%B4%E6%96%B0%E7%8B%80%E6%85%8B%0A%20%20%20%20%20%20%20%20%20%20%20%20duplicated.add%28choice%29%20%20%23%20%E8%A8%98%E9%8C%84%E9%81%B8%E6%93%87%E9%81%8E%E7%9A%84%E5%85%83%E7%B4%A0%E5%80%BC%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20state.append%28choice%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E9%80%B2%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BC%AA%E9%81%B8%E6%93%87%0A%20%20%20%20%20%20%20%20%20%20%20%20backtrack%28state%2C%20choices%2C%20selected%2C%20res%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%8A%B7%E9%81%B8%E6%93%87%EF%BC%8C%E6%81%A2%E5%BE%A9%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8B%80%E6%85%8B%0A%20%20%20%20%20%20%20%20%20%20%20%20selected%5Bi%5D%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20permutations_ii%28nums%3A%20list%5Bint%5D%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E5%85%A8%E6%8E%92%E5%88%97%20II%22%22%22%0A%20%20%20%20res%20%3D%20%5B%5D%0A%20%20%20%20backtrack%28state%3D%5B%5D%2C%20choices%3Dnums%2C%20selected%3D%5BFalse%5D%20%2A%20len%28nums%29%2C%20res%3Dres%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B1%2C%202%2C%202%5D%0A%0A%20%20%20%20res%20%3D%20permutations_ii%28nums%29%0A%0A%20%20%20%20print%28f%22%E8%BC%B8%E5%85%A5%E9%99%A3%E5%88%97%20nums%20%3D%20%7Bnums%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E6%8E%92%E5%88%97%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=13&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{preorder_traversal_i_compact}-[class]{}-[func]{pre_order} --> <!-- [file]{preorder_traversal_i_compact}-[class]{}-[func]{pre_order} -->
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20list_to_tree_dfs%28arr%3A%20list%5Bint%5D,%20i%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%86%E5%88%97%E8%A1%A8%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%9A%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E5%A6%82%E6%9E%9C%E7%B4%A2%E5%BC%95%E8%B6%85%E5%87%BA%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%EF%BC%8C%E6%88%96%E8%80%85%E5%AF%B9%E5%BA%94%E7%9A%84%E5%85%83%E7%B4%A0%E4%B8%BA%20None%20%EF%BC%8C%E5%88%99%E8%BF%94%E5%9B%9E%20None%0A%20%20%20%20if%20i%20%3C%200%20or%20i%20%3E%3D%20len%28arr%29%20or%20arr%5Bi%5D%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E5%BD%93%E5%89%8D%E8%8A%82%E7%82%B9%0A%20%20%20%20root%20%3D%20TreeNode%28arr%5Bi%5D%29%0A%20%20%20%20%23%20%E9%80%92%E5%BD%92%E6%9E%84%E5%BB%BA%E5%B7%A6%E5%8F%B3%E5%AD%90%E6%A0%91%0A%20%20%20%20root.left%20%3D%20list_to_tree_dfs%28arr,%202%20*%20i%20%2B%201%29%0A%20%20%20%20root.right%20%3D%20list_to_tree_dfs%28arr,%202%20*%20i%20%2B%202%29%0A%20%20%20%20return%20root%0A%0Adef%20list_to_tree%28arr%3A%20list%5Bint%5D%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%86%E5%88%97%E8%A1%A8%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%A0%91%22%22%22%0A%20%20%20%20return%20list_to_tree_dfs%28arr,%200%29%0A%0A%0Adef%20pre_order%28root%3A%20TreeNode%29%3A%0A%20%20%20%20%22%22%22%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%9A%E4%BE%8B%E9%A2%98%E4%B8%80%22%22%22%0A%20%20%20%20if%20root%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20if%20root.val%20%3D%3D%207%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20%20%20%20%20res.append%28root%29%0A%20%20%20%20pre_order%28root.left%29%0A%20%20%20%20pre_order%28root.right%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20root%20%3D%20list_to_tree%28%5B1,%207,%203,%204,%205,%206,%207%5D%29%0A%0A%20%20%20%20%23%20%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%0A%20%20%20%20res%20%3D%20list%5BTreeNode%5D%28%29%0A%20%20%20%20pre_order%28root%29%0A%0A%20%20%20%20print%28%22%5Cn%E8%BE%93%E5%87%BA%E6%89%80%E6%9C%89%E5%80%BC%E4%B8%BA%207%20%E7%9A%84%E8%8A%82%E7%82%B9%22%29%0A%20%20%20%20print%28%5Bnode.val%20for%20node%20in%20res%5D%29&cumulative=false&curInstr=126&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%85%83%E6%A8%B9%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0Adef%20list_to_tree_dfs%28arr%3A%20list%5Bint%5D%2C%20i%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%87%E4%B8%B2%E5%88%97%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%82%BA%E4%BA%8C%E5%85%83%E6%A8%B9%EF%BC%9A%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E5%A6%82%E6%9E%9C%E7%B4%A2%E5%BC%95%E8%B6%85%E5%87%BA%E9%99%A3%E5%88%97%E9%95%B7%E5%BA%A6%EF%BC%8C%E6%88%96%E8%80%85%E5%B0%8D%E6%87%89%E7%9A%84%E5%85%83%E7%B4%A0%E7%82%BA%20None%20%EF%BC%8C%E5%89%87%E8%BF%94%E5%9B%9E%20None%0A%20%20%20%20if%20i%20%3C%200%20or%20i%20%3E%3D%20len%28arr%29%20or%20arr%5Bi%5D%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%95%B6%E5%89%8D%E7%AF%80%E9%BB%9E%0A%20%20%20%20root%20%3D%20TreeNode%28arr%5Bi%5D%29%0A%20%20%20%20%23%20%E9%81%9E%E8%BF%B4%E6%A7%8B%E5%BB%BA%E5%B7%A6%E5%8F%B3%E5%AD%90%E6%A8%B9%0A%20%20%20%20root.left%20%3D%20list_to_tree_dfs%28arr%2C%202%20%2A%20i%20%2B%201%29%0A%20%20%20%20root.right%20%3D%20list_to_tree_dfs%28arr%2C%202%20%2A%20i%20%2B%202%29%0A%20%20%20%20return%20root%0A%0Adef%20list_to_tree%28arr%3A%20list%5Bint%5D%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%87%E4%B8%B2%E5%88%97%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%82%BA%E4%BA%8C%E5%85%83%E6%A8%B9%22%22%22%0A%20%20%20%20return%20list_to_tree_dfs%28arr%2C%200%29%0A%0A%0Adef%20pre_order%28root%3A%20TreeNode%29%3A%0A%20%20%20%20%22%22%22%E5%89%8D%E5%BA%8F%E8%B5%B0%E8%A8%AA%EF%BC%9A%E4%BE%8B%E9%A1%8C%E4%B8%80%22%22%22%0A%20%20%20%20if%20root%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20if%20root.val%20%3D%3D%207%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20%20%20%20%20res.append%28root%29%0A%20%20%20%20pre_order%28root.left%29%0A%20%20%20%20pre_order%28root.right%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20root%20%3D%20list_to_tree%28%5B1%2C%207%2C%203%2C%204%2C%205%2C%206%2C%207%5D%29%0A%0A%20%20%20%20%23%20%E5%89%8D%E5%BA%8F%E8%B5%B0%E8%A8%AA%0A%20%20%20%20res%20%3D%20list%5BTreeNode%5D%28%29%0A%20%20%20%20pre_order%28root%29%0A%0A%20%20%20%20print%28%22%5Cn%E8%BC%B8%E5%87%BA%E6%89%80%E6%9C%89%E5%80%BC%E7%82%BA%207%20%E7%9A%84%E7%AF%80%E9%BB%9E%22%29%0A%20%20%20%20print%28%5Bnode.val%20for%20node%20in%20res%5D%29&cumulative=false&curInstr=126&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{preorder_traversal_ii_compact}-[class]{}-[func]{pre_order} --> <!-- [file]{preorder_traversal_ii_compact}-[class]{}-[func]{pre_order} -->
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20list_to_tree_dfs%28arr%3A%20list%5Bint%5D,%20i%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%86%E5%88%97%E8%A1%A8%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%9A%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E5%A6%82%E6%9E%9C%E7%B4%A2%E5%BC%95%E8%B6%85%E5%87%BA%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%EF%BC%8C%E6%88%96%E8%80%85%E5%AF%B9%E5%BA%94%E7%9A%84%E5%85%83%E7%B4%A0%E4%B8%BA%20None%20%EF%BC%8C%E5%88%99%E8%BF%94%E5%9B%9E%20None%0A%20%20%20%20if%20i%20%3C%200%20or%20i%20%3E%3D%20len%28arr%29%20or%20arr%5Bi%5D%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E5%BD%93%E5%89%8D%E8%8A%82%E7%82%B9%0A%20%20%20%20root%20%3D%20TreeNode%28arr%5Bi%5D%29%0A%20%20%20%20%23%20%E9%80%92%E5%BD%92%E6%9E%84%E5%BB%BA%E5%B7%A6%E5%8F%B3%E5%AD%90%E6%A0%91%0A%20%20%20%20root.left%20%3D%20list_to_tree_dfs%28arr,%202%20*%20i%20%2B%201%29%0A%20%20%20%20root.right%20%3D%20list_to_tree_dfs%28arr,%202%20*%20i%20%2B%202%29%0A%20%20%20%20return%20root%0A%0Adef%20list_to_tree%28arr%3A%20list%5Bint%5D%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%86%E5%88%97%E8%A1%A8%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%A0%91%22%22%22%0A%20%20%20%20return%20list_to_tree_dfs%28arr,%200%29%0A%0A%0Adef%20pre_order%28root%3A%20TreeNode%29%3A%0A%20%20%20%20%22%22%22%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%9A%E4%BE%8B%E9%A2%98%E4%BA%8C%22%22%22%0A%20%20%20%20if%20root%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%0A%20%20%20%20path.append%28root%29%0A%20%20%20%20if%20root.val%20%3D%3D%207%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20%20%20%20%20res.append%28list%28path%29%29%0A%20%20%20%20pre_order%28root.left%29%0A%20%20%20%20pre_order%28root.right%29%0A%20%20%20%20%23%20%E5%9B%9E%E9%80%80%0A%20%20%20%20path.pop%28%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20root%20%3D%20list_to_tree%28%5B1,%207,%203,%204,%205,%206,%207%5D%29%0A%0A%20%20%20%20%23%20%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%0A%20%20%20%20path%20%3D%20list%5BTreeNode%5D%28%29%0A%20%20%20%20res%20%3D%20list%5Blist%5BTreeNode%5D%5D%28%29%0A%20%20%20%20pre_order%28root%29%0A%0A%20%20%20%20print%28%22%5Cn%E8%BE%93%E5%87%BA%E6%89%80%E6%9C%89%E6%A0%B9%E8%8A%82%E7%82%B9%E5%88%B0%E8%8A%82%E7%82%B9%207%20%E7%9A%84%E8%B7%AF%E5%BE%84%22%29%0A%20%20%20%20for%20path%20in%20res%3A%0A%20%20%20%20%20%20%20%20print%28%5Bnode.val%20for%20node%20in%20path%5D%29&cumulative=false&curInstr=126&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%85%83%E6%A8%B9%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0Adef%20list_to_tree_dfs%28arr%3A%20list%5Bint%5D%2C%20i%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%87%E4%B8%B2%E5%88%97%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%82%BA%E4%BA%8C%E5%85%83%E6%A8%B9%EF%BC%9A%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E5%A6%82%E6%9E%9C%E7%B4%A2%E5%BC%95%E8%B6%85%E5%87%BA%E9%99%A3%E5%88%97%E9%95%B7%E5%BA%A6%EF%BC%8C%E6%88%96%E8%80%85%E5%B0%8D%E6%87%89%E7%9A%84%E5%85%83%E7%B4%A0%E7%82%BA%20None%20%EF%BC%8C%E5%89%87%E8%BF%94%E5%9B%9E%20None%0A%20%20%20%20if%20i%20%3C%200%20or%20i%20%3E%3D%20len%28arr%29%20or%20arr%5Bi%5D%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%95%B6%E5%89%8D%E7%AF%80%E9%BB%9E%0A%20%20%20%20root%20%3D%20TreeNode%28arr%5Bi%5D%29%0A%20%20%20%20%23%20%E9%81%9E%E8%BF%B4%E6%A7%8B%E5%BB%BA%E5%B7%A6%E5%8F%B3%E5%AD%90%E6%A8%B9%0A%20%20%20%20root.left%20%3D%20list_to_tree_dfs%28arr%2C%202%20%2A%20i%20%2B%201%29%0A%20%20%20%20root.right%20%3D%20list_to_tree_dfs%28arr%2C%202%20%2A%20i%20%2B%202%29%0A%20%20%20%20return%20root%0A%0Adef%20list_to_tree%28arr%3A%20list%5Bint%5D%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%87%E4%B8%B2%E5%88%97%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%82%BA%E4%BA%8C%E5%85%83%E6%A8%B9%22%22%22%0A%20%20%20%20return%20list_to_tree_dfs%28arr%2C%200%29%0A%0A%0Adef%20pre_order%28root%3A%20TreeNode%29%3A%0A%20%20%20%20%22%22%22%E5%89%8D%E5%BA%8F%E8%B5%B0%E8%A8%AA%EF%BC%9A%E4%BE%8B%E9%A1%8C%E4%BA%8C%22%22%22%0A%20%20%20%20if%20root%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E5%98%97%E8%A9%A6%0A%20%20%20%20path.append%28root%29%0A%20%20%20%20if%20root.val%20%3D%3D%207%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20%20%20%20%20res.append%28list%28path%29%29%0A%20%20%20%20pre_order%28root.left%29%0A%20%20%20%20pre_order%28root.right%29%0A%20%20%20%20%23%20%E5%9B%9E%E9%80%80%0A%20%20%20%20path.pop%28%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20root%20%3D%20list_to_tree%28%5B1%2C%207%2C%203%2C%204%2C%205%2C%206%2C%207%5D%29%0A%0A%20%20%20%20%23%20%E5%89%8D%E5%BA%8F%E8%B5%B0%E8%A8%AA%0A%20%20%20%20path%20%3D%20list%5BTreeNode%5D%28%29%0A%20%20%20%20res%20%3D%20list%5Blist%5BTreeNode%5D%5D%28%29%0A%20%20%20%20pre_order%28root%29%0A%0A%20%20%20%20print%28%22%5Cn%E8%BC%B8%E5%87%BA%E6%89%80%E6%9C%89%E6%A0%B9%E7%AF%80%E9%BB%9E%E5%88%B0%E7%AF%80%E9%BB%9E%207%20%E7%9A%84%E8%B7%AF%E5%BE%91%22%29%0A%20%20%20%20for%20path%20in%20res%3A%0A%20%20%20%20%20%20%20%20print%28%5Bnode.val%20for%20node%20in%20path%5D%29&cumulative=false&curInstr=126&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{preorder_traversal_iii_compact}-[class]{}-[func]{pre_order} --> <!-- [file]{preorder_traversal_iii_compact}-[class]{}-[func]{pre_order} -->
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20list_to_tree_dfs%28arr%3A%20list%5Bint%5D,%20i%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%86%E5%88%97%E8%A1%A8%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%9A%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E5%A6%82%E6%9E%9C%E7%B4%A2%E5%BC%95%E8%B6%85%E5%87%BA%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%EF%BC%8C%E6%88%96%E8%80%85%E5%AF%B9%E5%BA%94%E7%9A%84%E5%85%83%E7%B4%A0%E4%B8%BA%20None%20%EF%BC%8C%E5%88%99%E8%BF%94%E5%9B%9E%20None%0A%20%20%20%20if%20i%20%3C%200%20or%20i%20%3E%3D%20len%28arr%29%20or%20arr%5Bi%5D%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E5%BD%93%E5%89%8D%E8%8A%82%E7%82%B9%0A%20%20%20%20root%20%3D%20TreeNode%28arr%5Bi%5D%29%0A%20%20%20%20%23%20%E9%80%92%E5%BD%92%E6%9E%84%E5%BB%BA%E5%B7%A6%E5%8F%B3%E5%AD%90%E6%A0%91%0A%20%20%20%20root.left%20%3D%20list_to_tree_dfs%28arr,%202%20*%20i%20%2B%201%29%0A%20%20%20%20root.right%20%3D%20list_to_tree_dfs%28arr,%202%20*%20i%20%2B%202%29%0A%20%20%20%20return%20root%0A%0Adef%20list_to_tree%28arr%3A%20list%5Bint%5D%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%86%E5%88%97%E8%A1%A8%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%A0%91%22%22%22%0A%20%20%20%20return%20list_to_tree_dfs%28arr,%200%29%0A%0A%0Adef%20pre_order%28root%3A%20TreeNode%29%3A%0A%20%20%20%20%22%22%22%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%9A%E4%BE%8B%E9%A2%98%E4%B8%89%22%22%22%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%0A%20%20%20%20if%20root%20is%20None%20or%20root.val%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%0A%20%20%20%20path.append%28root%29%0A%20%20%20%20if%20root.val%20%3D%3D%207%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20%20%20%20%20res.append%28list%28path%29%29%0A%20%20%20%20pre_order%28root.left%29%0A%20%20%20%20pre_order%28root.right%29%0A%20%20%20%20%23%20%E5%9B%9E%E9%80%80%0A%20%20%20%20path.pop%28%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20root%20%3D%20list_to_tree%28%5B1,%207,%203,%204,%205,%206,%207%5D%29%0A%0A%20%20%20%20%23%20%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%0A%20%20%20%20path%20%3D%20list%5BTreeNode%5D%28%29%0A%20%20%20%20res%20%3D%20list%5Blist%5BTreeNode%5D%5D%28%29%0A%20%20%20%20pre_order%28root%29%0A%0A%20%20%20%20print%28%22%5Cn%E8%BE%93%E5%87%BA%E6%89%80%E6%9C%89%E6%A0%B9%E8%8A%82%E7%82%B9%E5%88%B0%E8%8A%82%E7%82%B9%207%20%E7%9A%84%E8%B7%AF%E5%BE%84%EF%BC%8C%E8%B7%AF%E5%BE%84%E4%B8%AD%E4%B8%8D%E5%8C%85%E5%90%AB%E5%80%BC%E4%B8%BA%203%20%E7%9A%84%E8%8A%82%E7%82%B9%22%29%0A%20%20%20%20for%20path%20in%20res%3A%0A%20%20%20%20%20%20%20%20print%28%5Bnode.val%20for%20node%20in%20path%5D%29&cumulative=false&curInstr=126&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%85%83%E6%A8%B9%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0Adef%20list_to_tree_dfs%28arr%3A%20list%5Bint%5D%2C%20i%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%87%E4%B8%B2%E5%88%97%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%82%BA%E4%BA%8C%E5%85%83%E6%A8%B9%EF%BC%9A%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E5%A6%82%E6%9E%9C%E7%B4%A2%E5%BC%95%E8%B6%85%E5%87%BA%E9%99%A3%E5%88%97%E9%95%B7%E5%BA%A6%EF%BC%8C%E6%88%96%E8%80%85%E5%B0%8D%E6%87%89%E7%9A%84%E5%85%83%E7%B4%A0%E7%82%BA%20None%20%EF%BC%8C%E5%89%87%E8%BF%94%E5%9B%9E%20None%0A%20%20%20%20if%20i%20%3C%200%20or%20i%20%3E%3D%20len%28arr%29%20or%20arr%5Bi%5D%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%95%B6%E5%89%8D%E7%AF%80%E9%BB%9E%0A%20%20%20%20root%20%3D%20TreeNode%28arr%5Bi%5D%29%0A%20%20%20%20%23%20%E9%81%9E%E8%BF%B4%E6%A7%8B%E5%BB%BA%E5%B7%A6%E5%8F%B3%E5%AD%90%E6%A8%B9%0A%20%20%20%20root.left%20%3D%20list_to_tree_dfs%28arr%2C%202%20%2A%20i%20%2B%201%29%0A%20%20%20%20root.right%20%3D%20list_to_tree_dfs%28arr%2C%202%20%2A%20i%20%2B%202%29%0A%20%20%20%20return%20root%0A%0Adef%20list_to_tree%28arr%3A%20list%5Bint%5D%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E5%B0%87%E4%B8%B2%E5%88%97%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%82%BA%E4%BA%8C%E5%85%83%E6%A8%B9%22%22%22%0A%20%20%20%20return%20list_to_tree_dfs%28arr%2C%200%29%0A%0A%0Adef%20pre_order%28root%3A%20TreeNode%29%3A%0A%20%20%20%20%22%22%22%E5%89%8D%E5%BA%8F%E8%B5%B0%E8%A8%AA%EF%BC%9A%E4%BE%8B%E9%A1%8C%E4%B8%89%22%22%22%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%0A%20%20%20%20if%20root%20is%20None%20or%20root.val%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E5%98%97%E8%A9%A6%0A%20%20%20%20path.append%28root%29%0A%20%20%20%20if%20root.val%20%3D%3D%207%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20%20%20%20%20res.append%28list%28path%29%29%0A%20%20%20%20pre_order%28root.left%29%0A%20%20%20%20pre_order%28root.right%29%0A%20%20%20%20%23%20%E5%9B%9E%E9%80%80%0A%20%20%20%20path.pop%28%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20root%20%3D%20list_to_tree%28%5B1%2C%207%2C%203%2C%204%2C%205%2C%206%2C%207%5D%29%0A%0A%20%20%20%20%23%20%E5%89%8D%E5%BA%8F%E8%B5%B0%E8%A8%AA%0A%20%20%20%20path%20%3D%20list%5BTreeNode%5D%28%29%0A%20%20%20%20res%20%3D%20list%5Blist%5BTreeNode%5D%5D%28%29%0A%20%20%20%20pre_order%28root%29%0A%0A%20%20%20%20print%28%22%5Cn%E8%BC%B8%E5%87%BA%E6%89%80%E6%9C%89%E6%A0%B9%E7%AF%80%E9%BB%9E%E5%88%B0%E7%AF%80%E9%BB%9E%207%20%E7%9A%84%E8%B7%AF%E5%BE%91%EF%BC%8C%E8%B7%AF%E5%BE%91%E4%B8%AD%E4%B8%8D%E5%8C%85%E5%90%AB%E5%80%BC%E7%82%BA%203%20%E7%9A%84%E7%AF%80%E9%BB%9E%22%29%0A%20%20%20%20for%20path%20in%20res%3A%0A%20%20%20%20%20%20%20%20print%28%5Bnode.val%20for%20node%20in%20path%5D%29&cumulative=false&curInstr=126&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

File diff suppressed because one or more lines are too long

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{subset_sum_i}-[class]{}-[func]{subset_sum_i} --> <!-- [file]{subset_sum_i}-[class]{}-[func]{subset_sum_i} -->
https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D,%20target%3A%20int,%20choices%3A%20list%5Bint%5D,%20start%3A%20int,%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9A%E5%AD%90%E9%9B%86%E5%92%8C%20I%22%22%22%0A%20%20%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%E7%AD%89%E4%BA%8E%20target%20%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20target%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%80%89%E6%8B%A9%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%BA%8C%EF%BC%9A%E4%BB%8E%20start%20%E5%BC%80%E5%A7%8B%E9%81%8D%E5%8E%86%EF%BC%8C%E9%81%BF%E5%85%8D%E7%94%9F%E6%88%90%E9%87%8D%E5%A4%8D%E5%AD%90%E9%9B%86%0A%20%20%20%20for%20i%20in%20range%28start,%20len%28choices%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%B8%80%EF%BC%9A%E8%8B%A5%E5%AD%90%E9%9B%86%E5%92%8C%E8%B6%85%E8%BF%87%20target%20%EF%BC%8C%E5%88%99%E7%9B%B4%E6%8E%A5%E7%BB%93%E6%9D%9F%E5%BE%AA%E7%8E%AF%0A%20%20%20%20%20%20%20%20%23%20%E8%BF%99%E6%98%AF%E5%9B%A0%E4%B8%BA%E6%95%B0%E7%BB%84%E5%B7%B2%E6%8E%92%E5%BA%8F%EF%BC%8C%E5%90%8E%E8%BE%B9%E5%85%83%E7%B4%A0%E6%9B%B4%E5%A4%A7%EF%BC%8C%E5%AD%90%E9%9B%86%E5%92%8C%E4%B8%80%E5%AE%9A%E8%B6%85%E8%BF%87%20target%0A%20%20%20%20%20%20%20%20if%20target%20-%20choices%5Bi%5D%20%3C%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%81%9A%E5%87%BA%E9%80%89%E6%8B%A9%EF%BC%8C%E6%9B%B4%E6%96%B0%20target,%20start%0A%20%20%20%20%20%20%20%20state.append%28choices%5Bi%5D%29%0A%20%20%20%20%20%20%20%20%23%20%E8%BF%9B%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BD%AE%E9%80%89%E6%8B%A9%0A%20%20%20%20%20%20%20%20backtrack%28state,%20target%20-%20choices%5Bi%5D,%20choices,%20i,%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%94%80%E9%80%89%E6%8B%A9%EF%BC%8C%E6%81%A2%E5%A4%8D%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20subset_sum_i%28nums%3A%20list%5Bint%5D,%20target%3A%20int%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%E5%AD%90%E9%9B%86%E5%92%8C%20I%22%22%22%0A%20%20%20%20state%20%3D%20%5B%5D%20%20%23%20%E7%8A%B6%E6%80%81%EF%BC%88%E5%AD%90%E9%9B%86%EF%BC%89%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E5%AF%B9%20nums%20%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F%0A%20%20%20%20start%20%3D%200%20%20%23%20%E9%81%8D%E5%8E%86%E8%B5%B7%E5%A7%8B%E7%82%B9%0A%20%20%20%20res%20%3D%20%5B%5D%20%20%23%20%E7%BB%93%E6%9E%9C%E5%88%97%E8%A1%A8%EF%BC%88%E5%AD%90%E9%9B%86%E5%88%97%E8%A1%A8%EF%BC%89%0A%20%20%20%20backtrack%28state,%20target,%20nums,%20start,%20res%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B3,%204,%205%5D%0A%20%20%20%20target%20%3D%209%0A%20%20%20%20res%20%3D%20subset_sum_i%28nums,%20target%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%95%B0%E7%BB%84%20nums%20%3D%20%7Bnums%7D,%20target%20%3D%20%7Btarget%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E5%92%8C%E7%AD%89%E4%BA%8E%20%7Btarget%7D%20%E7%9A%84%E5%AD%90%E9%9B%86%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=16&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D%2C%20target%3A%20int%2C%20choices%3A%20list%5Bint%5D%2C%20start%3A%20int%2C%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E6%BC%94%E7%AE%97%E6%B3%95%EF%BC%9A%E5%AD%90%E9%9B%86%E5%92%8C%20I%22%22%22%0A%20%20%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%E7%AD%89%E6%96%BC%20target%20%E6%99%82%EF%BC%8C%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20if%20target%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E8%B5%B0%E8%A8%AA%E6%89%80%E6%9C%89%E9%81%B8%E6%93%87%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%BA%8C%EF%BC%9A%E5%BE%9E%20start%20%E9%96%8B%E5%A7%8B%E8%B5%B0%E8%A8%AA%EF%BC%8C%E9%81%BF%E5%85%8D%E7%94%9F%E6%88%90%E9%87%8D%E8%A4%87%E5%AD%90%E9%9B%86%0A%20%20%20%20for%20i%20in%20range%28start%2C%20len%28choices%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%B8%80%EF%BC%9A%E8%8B%A5%E5%AD%90%E9%9B%86%E5%92%8C%E8%B6%85%E9%81%8E%20target%20%EF%BC%8C%E5%89%87%E7%9B%B4%E6%8E%A5%E7%B5%90%E6%9D%9F%E8%BF%B4%E5%9C%88%0A%20%20%20%20%20%20%20%20%23%20%E9%80%99%E6%98%AF%E5%9B%A0%E7%82%BA%E9%99%A3%E5%88%97%E5%B7%B2%E6%8E%92%E5%BA%8F%EF%BC%8C%E5%BE%8C%E9%82%8A%E5%85%83%E7%B4%A0%E6%9B%B4%E5%A4%A7%EF%BC%8C%E5%AD%90%E9%9B%86%E5%92%8C%E4%B8%80%E5%AE%9A%E8%B6%85%E9%81%8E%20target%0A%20%20%20%20%20%20%20%20if%20target%20-%20choices%5Bi%5D%20%3C%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%23%20%E5%98%97%E8%A9%A6%EF%BC%9A%E5%81%9A%E5%87%BA%E9%81%B8%E6%93%87%EF%BC%8C%E6%9B%B4%E6%96%B0%20target%2C%20start%0A%20%20%20%20%20%20%20%20state.append%28choices%5Bi%5D%29%0A%20%20%20%20%20%20%20%20%23%20%E9%80%B2%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BC%AA%E9%81%B8%E6%93%87%0A%20%20%20%20%20%20%20%20backtrack%28state%2C%20target%20-%20choices%5Bi%5D%2C%20choices%2C%20i%2C%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%8A%B7%E9%81%B8%E6%93%87%EF%BC%8C%E6%81%A2%E5%BE%A9%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8B%80%E6%85%8B%0A%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20subset_sum_i%28nums%3A%20list%5Bint%5D%2C%20target%3A%20int%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%E5%AD%90%E9%9B%86%E5%92%8C%20I%22%22%22%0A%20%20%20%20state%20%3D%20%5B%5D%20%20%23%20%E7%8B%80%E6%85%8B%EF%BC%88%E5%AD%90%E9%9B%86%EF%BC%89%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E5%B0%8D%20nums%20%E9%80%B2%E8%A1%8C%E6%8E%92%E5%BA%8F%0A%20%20%20%20start%20%3D%200%20%20%23%20%E8%B5%B0%E8%A8%AA%E8%B5%B7%E5%A7%8B%E9%BB%9E%0A%20%20%20%20res%20%3D%20%5B%5D%20%20%23%20%E7%B5%90%E6%9E%9C%E4%B8%B2%E5%88%97%EF%BC%88%E5%AD%90%E9%9B%86%E4%B8%B2%E5%88%97%EF%BC%89%0A%20%20%20%20backtrack%28state%2C%20target%2C%20nums%2C%20start%2C%20res%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B3%2C%204%2C%205%5D%0A%20%20%20%20target%20%3D%209%0A%20%20%20%20res%20%3D%20subset_sum_i%28nums%2C%20target%29%0A%0A%20%20%20%20print%28f%22%E8%BC%B8%E5%85%A5%E9%99%A3%E5%88%97%20nums%20%3D%20%7Bnums%7D%2C%20target%20%3D%20%7Btarget%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E5%92%8C%E7%AD%89%E6%96%BC%20%7Btarget%7D%20%E7%9A%84%E5%AD%90%E9%9B%86%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=16&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{subset_sum_i_naive}-[class]{}-[func]{subset_sum_i_naive} --> <!-- [file]{subset_sum_i_naive}-[class]{}-[func]{subset_sum_i_naive} -->
https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D,%0A%20%20%20%20target%3A%20int,%0A%20%20%20%20total%3A%20int,%0A%20%20%20%20choices%3A%20list%5Bint%5D,%0A%20%20%20%20res%3A%20list%5Blist%5Bint%5D%5D,%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9A%E5%AD%90%E9%9B%86%E5%92%8C%20I%22%22%22%0A%20%20%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%E7%AD%89%E4%BA%8E%20target%20%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20total%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%80%89%E6%8B%A9%0A%20%20%20%20for%20i%20in%20range%28len%28choices%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E8%8B%A5%E5%AD%90%E9%9B%86%E5%92%8C%E8%B6%85%E8%BF%87%20target%20%EF%BC%8C%E5%88%99%E8%B7%B3%E8%BF%87%E8%AF%A5%E9%80%89%E6%8B%A9%0A%20%20%20%20%20%20%20%20if%20total%20%2B%20choices%5Bi%5D%20%3E%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%81%9A%E5%87%BA%E9%80%89%E6%8B%A9%EF%BC%8C%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%E5%92%8C%20total%0A%20%20%20%20%20%20%20%20state.append%28choices%5Bi%5D%29%0A%20%20%20%20%20%20%20%20%23%20%E8%BF%9B%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BD%AE%E9%80%89%E6%8B%A9%0A%20%20%20%20%20%20%20%20backtrack%28state,%20target,%20total%20%2B%20choices%5Bi%5D,%20choices,%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%94%80%E9%80%89%E6%8B%A9%EF%BC%8C%E6%81%A2%E5%A4%8D%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20subset_sum_i_naive%28nums%3A%20list%5Bint%5D,%20target%3A%20int%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%E5%AD%90%E9%9B%86%E5%92%8C%20I%EF%BC%88%E5%8C%85%E5%90%AB%E9%87%8D%E5%A4%8D%E5%AD%90%E9%9B%86%EF%BC%89%22%22%22%0A%20%20%20%20state%20%3D%20%5B%5D%20%20%23%20%E7%8A%B6%E6%80%81%EF%BC%88%E5%AD%90%E9%9B%86%EF%BC%89%0A%20%20%20%20total%20%3D%200%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%0A%20%20%20%20res%20%3D%20%5B%5D%20%20%23%20%E7%BB%93%E6%9E%9C%E5%88%97%E8%A1%A8%EF%BC%88%E5%AD%90%E9%9B%86%E5%88%97%E8%A1%A8%EF%BC%89%0A%20%20%20%20backtrack%28state,%20target,%20total,%20nums,%20res%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B3,%204,%205%5D%0A%20%20%20%20target%20%3D%209%0A%20%20%20%20res%20%3D%20subset_sum_i_naive%28nums,%20target%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%95%B0%E7%BB%84%20nums%20%3D%20%7Bnums%7D,%20target%20%3D%20%7Btarget%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E5%92%8C%E7%AD%89%E4%BA%8E%20%7Btarget%7D%20%E7%9A%84%E5%AD%90%E9%9B%86%20res%20%3D%20%7Bres%7D%22%29%0A%20%20%20%20print%28f%22%E8%AF%B7%E6%B3%A8%E6%84%8F%EF%BC%8C%E8%AF%A5%E6%96%B9%E6%B3%95%E8%BE%93%E5%87%BA%E7%9A%84%E7%BB%93%E6%9E%9C%E5%8C%85%E5%90%AB%E9%87%8D%E5%A4%8D%E9%9B%86%E5%90%88%22%29&cumulative=false&curInstr=16&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D%2C%0A%20%20%20%20target%3A%20int%2C%0A%20%20%20%20total%3A%20int%2C%0A%20%20%20%20choices%3A%20list%5Bint%5D%2C%0A%20%20%20%20res%3A%20list%5Blist%5Bint%5D%5D%2C%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E6%BC%94%E7%AE%97%E6%B3%95%EF%BC%9A%E5%AD%90%E9%9B%86%E5%92%8C%20I%22%22%22%0A%20%20%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%E7%AD%89%E6%96%BC%20target%20%E6%99%82%EF%BC%8C%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20if%20total%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E8%B5%B0%E8%A8%AA%E6%89%80%E6%9C%89%E9%81%B8%E6%93%87%0A%20%20%20%20for%20i%20in%20range%28len%28choices%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E8%8B%A5%E5%AD%90%E9%9B%86%E5%92%8C%E8%B6%85%E9%81%8E%20target%20%EF%BC%8C%E5%89%87%E8%B7%B3%E9%81%8E%E8%A9%B2%E9%81%B8%E6%93%87%0A%20%20%20%20%20%20%20%20if%20total%20%2B%20choices%5Bi%5D%20%3E%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%23%20%E5%98%97%E8%A9%A6%EF%BC%9A%E5%81%9A%E5%87%BA%E9%81%B8%E6%93%87%EF%BC%8C%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%E5%92%8C%20total%0A%20%20%20%20%20%20%20%20state.append%28choices%5Bi%5D%29%0A%20%20%20%20%20%20%20%20%23%20%E9%80%B2%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BC%AA%E9%81%B8%E6%93%87%0A%20%20%20%20%20%20%20%20backtrack%28state%2C%20target%2C%20total%20%2B%20choices%5Bi%5D%2C%20choices%2C%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%8A%B7%E9%81%B8%E6%93%87%EF%BC%8C%E6%81%A2%E5%BE%A9%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8B%80%E6%85%8B%0A%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20subset_sum_i_naive%28nums%3A%20list%5Bint%5D%2C%20target%3A%20int%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%E5%AD%90%E9%9B%86%E5%92%8C%20I%EF%BC%88%E5%8C%85%E5%90%AB%E9%87%8D%E8%A4%87%E5%AD%90%E9%9B%86%EF%BC%89%22%22%22%0A%20%20%20%20state%20%3D%20%5B%5D%20%20%23%20%E7%8B%80%E6%85%8B%EF%BC%88%E5%AD%90%E9%9B%86%EF%BC%89%0A%20%20%20%20total%20%3D%200%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%0A%20%20%20%20res%20%3D%20%5B%5D%20%20%23%20%E7%B5%90%E6%9E%9C%E4%B8%B2%E5%88%97%EF%BC%88%E5%AD%90%E9%9B%86%E4%B8%B2%E5%88%97%EF%BC%89%0A%20%20%20%20backtrack%28state%2C%20target%2C%20total%2C%20nums%2C%20res%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B3%2C%204%2C%205%5D%0A%20%20%20%20target%20%3D%209%0A%20%20%20%20res%20%3D%20subset_sum_i_naive%28nums%2C%20target%29%0A%0A%20%20%20%20print%28f%22%E8%BC%B8%E5%85%A5%E9%99%A3%E5%88%97%20nums%20%3D%20%7Bnums%7D%2C%20target%20%3D%20%7Btarget%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E5%92%8C%E7%AD%89%E6%96%BC%20%7Btarget%7D%20%E7%9A%84%E5%AD%90%E9%9B%86%20res%20%3D%20%7Bres%7D%22%29%0A%20%20%20%20print%28f%22%E8%AB%8B%E6%B3%A8%E6%84%8F%EF%BC%8C%E8%A9%B2%E6%96%B9%E6%B3%95%E8%BC%B8%E5%87%BA%E7%9A%84%E7%B5%90%E6%9E%9C%E5%8C%85%E5%90%AB%E9%87%8D%E8%A4%87%E9%9B%86%E5%90%88%22%29&cumulative=false&curInstr=16&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,4 +5,4 @@
---> --->
<!-- [file]{subset_sum_ii}-[class]{}-[func]{subset_sum_ii} --> <!-- [file]{subset_sum_ii}-[class]{}-[func]{subset_sum_ii} -->
https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D,%20target%3A%20int,%20choices%3A%20list%5Bint%5D,%20start%3A%20int,%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%EF%BC%9A%E5%AD%90%E9%9B%86%E5%92%8C%20II%22%22%22%0A%20%20%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%E7%AD%89%E4%BA%8E%20target%20%E6%97%B6%EF%BC%8C%E8%AE%B0%E5%BD%95%E8%A7%A3%0A%20%20%20%20if%20target%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%80%89%E6%8B%A9%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%BA%8C%EF%BC%9A%E4%BB%8E%20start%20%E5%BC%80%E5%A7%8B%E9%81%8D%E5%8E%86%EF%BC%8C%E9%81%BF%E5%85%8D%E7%94%9F%E6%88%90%E9%87%8D%E5%A4%8D%E5%AD%90%E9%9B%86%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%B8%89%EF%BC%9A%E4%BB%8E%20start%20%E5%BC%80%E5%A7%8B%E9%81%8D%E5%8E%86%EF%BC%8C%E9%81%BF%E5%85%8D%E9%87%8D%E5%A4%8D%E9%80%89%E6%8B%A9%E5%90%8C%E4%B8%80%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20i%20in%20range%28start,%20len%28choices%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%B8%80%EF%BC%9A%E8%8B%A5%E5%AD%90%E9%9B%86%E5%92%8C%E8%B6%85%E8%BF%87%20target%20%EF%BC%8C%E5%88%99%E7%9B%B4%E6%8E%A5%E7%BB%93%E6%9D%9F%E5%BE%AA%E7%8E%AF%0A%20%20%20%20%20%20%20%20%23%20%E8%BF%99%E6%98%AF%E5%9B%A0%E4%B8%BA%E6%95%B0%E7%BB%84%E5%B7%B2%E6%8E%92%E5%BA%8F%EF%BC%8C%E5%90%8E%E8%BE%B9%E5%85%83%E7%B4%A0%E6%9B%B4%E5%A4%A7%EF%BC%8C%E5%AD%90%E9%9B%86%E5%92%8C%E4%B8%80%E5%AE%9A%E8%B6%85%E8%BF%87%20target%0A%20%20%20%20%20%20%20%20if%20target%20-%20choices%5Bi%5D%20%3C%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E5%9B%9B%EF%BC%9A%E5%A6%82%E6%9E%9C%E8%AF%A5%E5%85%83%E7%B4%A0%E4%B8%8E%E5%B7%A6%E8%BE%B9%E5%85%83%E7%B4%A0%E7%9B%B8%E7%AD%89%EF%BC%8C%E8%AF%B4%E6%98%8E%E8%AF%A5%E6%90%9C%E7%B4%A2%E5%88%86%E6%94%AF%E9%87%8D%E5%A4%8D%EF%BC%8C%E7%9B%B4%E6%8E%A5%E8%B7%B3%E8%BF%87%0A%20%20%20%20%20%20%20%20if%20i%20%3E%20start%20and%20choices%5Bi%5D%20%3D%3D%20choices%5Bi%20-%201%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%81%9A%E5%87%BA%E9%80%89%E6%8B%A9%EF%BC%8C%E6%9B%B4%E6%96%B0%20target,%20start%0A%20%20%20%20%20%20%20%20state.append%28choices%5Bi%5D%29%0A%20%20%20%20%20%20%20%20%23%20%E8%BF%9B%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BD%AE%E9%80%89%E6%8B%A9%0A%20%20%20%20%20%20%20%20backtrack%28state,%20target%20-%20choices%5Bi%5D,%20choices,%20i%20%2B%201,%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%94%80%E9%80%89%E6%8B%A9%EF%BC%8C%E6%81%A2%E5%A4%8D%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20subset_sum_ii%28nums%3A%20list%5Bint%5D,%20target%3A%20int%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%E5%AD%90%E9%9B%86%E5%92%8C%20II%22%22%22%0A%20%20%20%20state%20%3D%20%5B%5D%20%20%23%20%E7%8A%B6%E6%80%81%EF%BC%88%E5%AD%90%E9%9B%86%EF%BC%89%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E5%AF%B9%20nums%20%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F%0A%20%20%20%20start%20%3D%200%20%20%23%20%E9%81%8D%E5%8E%86%E8%B5%B7%E5%A7%8B%E7%82%B9%0A%20%20%20%20res%20%3D%20%5B%5D%20%20%23%20%E7%BB%93%E6%9E%9C%E5%88%97%E8%A1%A8%EF%BC%88%E5%AD%90%E9%9B%86%E5%88%97%E8%A1%A8%EF%BC%89%0A%20%20%20%20backtrack%28state,%20target,%20nums,%20start,%20res%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B4,%204,%205%5D%0A%20%20%20%20target%20%3D%209%0A%20%20%20%20res%20%3D%20subset_sum_ii%28nums,%20target%29%0A%0A%20%20%20%20print%28f%22%E8%BE%93%E5%85%A5%E6%95%B0%E7%BB%84%20nums%20%3D%20%7Bnums%7D,%20target%20%3D%20%7Btarget%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E5%92%8C%E7%AD%89%E4%BA%8E%20%7Btarget%7D%20%E7%9A%84%E5%AD%90%E9%9B%86%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=16&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20backtrack%28%0A%20%20%20%20state%3A%20list%5Bint%5D%2C%20target%3A%20int%2C%20choices%3A%20list%5Bint%5D%2C%20start%3A%20int%2C%20res%3A%20list%5Blist%5Bint%5D%5D%0A%29%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%E6%BC%94%E7%AE%97%E6%B3%95%EF%BC%9A%E5%AD%90%E9%9B%86%E5%92%8C%20II%22%22%22%0A%20%20%20%20%23%20%E5%AD%90%E9%9B%86%E5%92%8C%E7%AD%89%E6%96%BC%20target%20%E6%99%82%EF%BC%8C%E8%A8%98%E9%8C%84%E8%A7%A3%0A%20%20%20%20if%20target%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20res.append%28list%28state%29%29%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20%E8%B5%B0%E8%A8%AA%E6%89%80%E6%9C%89%E9%81%B8%E6%93%87%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%BA%8C%EF%BC%9A%E5%BE%9E%20start%20%E9%96%8B%E5%A7%8B%E8%B5%B0%E8%A8%AA%EF%BC%8C%E9%81%BF%E5%85%8D%E7%94%9F%E6%88%90%E9%87%8D%E8%A4%87%E5%AD%90%E9%9B%86%0A%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%B8%89%EF%BC%9A%E5%BE%9E%20start%20%E9%96%8B%E5%A7%8B%E8%B5%B0%E8%A8%AA%EF%BC%8C%E9%81%BF%E5%85%8D%E9%87%8D%E8%A4%87%E9%81%B8%E6%93%87%E5%90%8C%E4%B8%80%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20i%20in%20range%28start%2C%20len%28choices%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E4%B8%80%EF%BC%9A%E8%8B%A5%E5%AD%90%E9%9B%86%E5%92%8C%E8%B6%85%E9%81%8E%20target%20%EF%BC%8C%E5%89%87%E7%9B%B4%E6%8E%A5%E7%B5%90%E6%9D%9F%E8%BF%B4%E5%9C%88%0A%20%20%20%20%20%20%20%20%23%20%E9%80%99%E6%98%AF%E5%9B%A0%E7%82%BA%E9%99%A3%E5%88%97%E5%B7%B2%E6%8E%92%E5%BA%8F%EF%BC%8C%E5%BE%8C%E9%82%8A%E5%85%83%E7%B4%A0%E6%9B%B4%E5%A4%A7%EF%BC%8C%E5%AD%90%E9%9B%86%E5%92%8C%E4%B8%80%E5%AE%9A%E8%B6%85%E9%81%8E%20target%0A%20%20%20%20%20%20%20%20if%20target%20-%20choices%5Bi%5D%20%3C%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%E5%9B%9B%EF%BC%9A%E5%A6%82%E6%9E%9C%E8%A9%B2%E5%85%83%E7%B4%A0%E8%88%87%E5%B7%A6%E9%82%8A%E5%85%83%E7%B4%A0%E7%9B%B8%E7%AD%89%EF%BC%8C%E8%AA%AA%E6%98%8E%E8%A9%B2%E6%90%9C%E5%B0%8B%E5%88%86%E6%94%AF%E9%87%8D%E8%A4%87%EF%BC%8C%E7%9B%B4%E6%8E%A5%E8%B7%B3%E9%81%8E%0A%20%20%20%20%20%20%20%20if%20i%20%3E%20start%20and%20choices%5Bi%5D%20%3D%3D%20choices%5Bi%20-%201%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%23%20%E5%98%97%E8%A9%A6%EF%BC%9A%E5%81%9A%E5%87%BA%E9%81%B8%E6%93%87%EF%BC%8C%E6%9B%B4%E6%96%B0%20target%2C%20start%0A%20%20%20%20%20%20%20%20state.append%28choices%5Bi%5D%29%0A%20%20%20%20%20%20%20%20%23%20%E9%80%B2%E8%A1%8C%E4%B8%8B%E4%B8%80%E8%BC%AA%E9%81%B8%E6%93%87%0A%20%20%20%20%20%20%20%20backtrack%28state%2C%20target%20-%20choices%5Bi%5D%2C%20choices%2C%20i%20%2B%201%2C%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%EF%BC%9A%E6%92%A4%E9%8A%B7%E9%81%B8%E6%93%87%EF%BC%8C%E6%81%A2%E5%BE%A9%E5%88%B0%E4%B9%8B%E5%89%8D%E7%9A%84%E7%8B%80%E6%85%8B%0A%20%20%20%20%20%20%20%20state.pop%28%29%0A%0A%0Adef%20subset_sum_ii%28nums%3A%20list%5Bint%5D%2C%20target%3A%20int%29%20-%3E%20list%5Blist%5Bint%5D%5D%3A%0A%20%20%20%20%22%22%22%E6%B1%82%E8%A7%A3%E5%AD%90%E9%9B%86%E5%92%8C%20II%22%22%22%0A%20%20%20%20state%20%3D%20%5B%5D%20%20%23%20%E7%8B%80%E6%85%8B%EF%BC%88%E5%AD%90%E9%9B%86%EF%BC%89%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E5%B0%8D%20nums%20%E9%80%B2%E8%A1%8C%E6%8E%92%E5%BA%8F%0A%20%20%20%20start%20%3D%200%20%20%23%20%E8%B5%B0%E8%A8%AA%E8%B5%B7%E5%A7%8B%E9%BB%9E%0A%20%20%20%20res%20%3D%20%5B%5D%20%20%23%20%E7%B5%90%E6%9E%9C%E4%B8%B2%E5%88%97%EF%BC%88%E5%AD%90%E9%9B%86%E4%B8%B2%E5%88%97%EF%BC%89%0A%20%20%20%20backtrack%28state%2C%20target%2C%20nums%2C%20start%2C%20res%29%0A%20%20%20%20return%20res%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B4%2C%204%2C%205%5D%0A%20%20%20%20target%20%3D%209%0A%20%20%20%20res%20%3D%20subset_sum_ii%28nums%2C%20target%29%0A%0A%20%20%20%20print%28f%22%E8%BC%B8%E5%85%A5%E9%99%A3%E5%88%97%20nums%20%3D%20%7Bnums%7D%2C%20target%20%3D%20%7Btarget%7D%22%29%0A%20%20%20%20print%28f%22%E6%89%80%E6%9C%89%E5%92%8C%E7%AD%89%E6%96%BC%20%7Btarget%7D%20%E7%9A%84%E5%AD%90%E9%9B%86%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=16&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,25 +5,25 @@
---> --->
<!-- [file]{iteration}-[class]{}-[func]{for_loop} --> <!-- [file]{iteration}-[class]{}-[func]{for_loop} -->
https://pythontutor.com/render.html#code=def%20for_loop%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22for%20%E5%BE%AA%E7%8E%AF%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%B1%82%E5%92%8C%201,%202,%20...,%20n-1,%20n%0A%20%20%20%20for%20i%20in%20range%281,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20for_loop%28n%29%0A%20%20%20%20print%28f%22%5Cnfor%20%E5%BE%AA%E7%8E%AF%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D& https://pythontutor.com/render.html#code=def%20for_loop%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22for%20%E8%BF%B4%E5%9C%88%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20%23%20%E8%BF%B4%E5%9C%88%E6%B1%82%E5%92%8C%201%2C%202%2C%20...%2C%20n-1%2C%20n%0A%20%20%20%20for%20i%20in%20range%281%2C%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20for_loop%28n%29%0A%20%20%20%20print%28f%22%5Cnfor%20%E8%BF%B4%E5%9C%88%E7%9A%84%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&
<!-- [file]{iteration}-[class]{}-[func]{while_loop} --> <!-- [file]{iteration}-[class]{}-[func]{while_loop} -->
https://pythontutor.com/render.html#code=def%20while_loop%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22while%20%E5%BE%AA%E7%8E%AF%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20i%20%3D%201%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8F%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%B1%82%E5%92%8C%201,%202,%20...,%20n-1,%20n%0A%20%20%20%20while%20i%20%3C%3D%20n%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20%20%20%20%20i%20%2B%3D%201%20%20%23%20%E6%9B%B4%E6%96%B0%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8F%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20while_loop%28n%29%0A%20%20%20%20print%28f%22%5Cnwhile%20%E5%BE%AA%E7%8E%AF%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20while_loop%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22while%20%E8%BF%B4%E5%9C%88%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20i%20%3D%201%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%A2%9D%E4%BB%B6%E8%AE%8A%E6%95%B8%0A%20%20%20%20%23%20%E8%BF%B4%E5%9C%88%E6%B1%82%E5%92%8C%201%2C%202%2C%20...%2C%20n-1%2C%20n%0A%20%20%20%20while%20i%20%3C%3D%20n%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20%20%20%20%20i%20%2B%3D%201%20%20%23%20%E6%9B%B4%E6%96%B0%E6%A2%9D%E4%BB%B6%E8%AE%8A%E6%95%B8%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20while_loop%28n%29%0A%20%20%20%20print%28f%22%5Cnwhile%20%E8%BF%B4%E5%9C%88%E7%9A%84%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{iteration}-[class]{}-[func]{while_loop_ii} --> <!-- [file]{iteration}-[class]{}-[func]{while_loop_ii} -->
https://pythontutor.com/render.html#code=def%20while_loop_ii%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22while%20%E5%BE%AA%E7%8E%AF%EF%BC%88%E4%B8%A4%E6%AC%A1%E6%9B%B4%E6%96%B0%EF%BC%89%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20i%20%3D%201%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8F%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%B1%82%E5%92%8C%201,%204,%2010,%20...%0A%20%20%20%20while%20i%20%3C%3D%20n%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8F%0A%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20i%20*%3D%202%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20while_loop_ii%28n%29%0A%20%20%20%20print%28f%22%5Cnwhile%20%E5%BE%AA%E7%8E%AF%EF%BC%88%E4%B8%A4%E6%AC%A1%E6%9B%B4%E6%96%B0%EF%BC%89%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20while_loop_ii%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22while%20%E8%BF%B4%E5%9C%88%EF%BC%88%E5%85%A9%E6%AC%A1%E6%9B%B4%E6%96%B0%EF%BC%89%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20i%20%3D%201%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%A2%9D%E4%BB%B6%E8%AE%8A%E6%95%B8%0A%20%20%20%20%23%20%E8%BF%B4%E5%9C%88%E6%B1%82%E5%92%8C%201%2C%204%2C%2010%2C%20...%0A%20%20%20%20while%20i%20%3C%3D%20n%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E6%A2%9D%E4%BB%B6%E8%AE%8A%E6%95%B8%0A%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20i%20%2A%3D%202%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20while_loop_ii%28n%29%0A%20%20%20%20print%28f%22%5Cnwhile%20%E8%BF%B4%E5%9C%88%EF%BC%88%E5%85%A9%E6%AC%A1%E6%9B%B4%E6%96%B0%EF%BC%89%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{iteration}-[class]{}-[func]{nested_for_loop} --> <!-- [file]{iteration}-[class]{}-[func]{nested_for_loop} -->
https://pythontutor.com/render.html#code=def%20nested_for_loop%28n%3A%20int%29%20-%3E%20str%3A%0A%20%20%20%20%22%22%22%E5%8F%8C%E5%B1%82%20for%20%E5%BE%AA%E7%8E%AF%22%22%22%0A%20%20%20%20res%20%3D%20%22%22%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%20i%20%3D%201,%202,%20...,%20n-1,%20n%0A%20%20%20%20for%20i%20in%20range%281,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%20j%20%3D%201,%202,%20...,%20n-1,%20n%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%281,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20res%20%2B%3D%20f%22%28%7Bi%7D,%20%7Bj%7D%29,%20%22%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20nested_for_loop%28n%29%0A%20%20%20%20print%28f%22%5Cn%E5%8F%8C%E5%B1%82%20for%20%E5%BE%AA%E7%8E%AF%E7%9A%84%E9%81%8D%E5%8E%86%E7%BB%93%E6%9E%9C%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20nested_for_loop%28n%3A%20int%29%20-%3E%20str%3A%0A%20%20%20%20%22%22%22%E9%9B%99%E5%B1%A4%20for%20%E8%BF%B4%E5%9C%88%22%22%22%0A%20%20%20%20res%20%3D%20%22%22%0A%20%20%20%20%23%20%E8%BF%B4%E5%9C%88%20i%20%3D%201%2C%202%2C%20...%2C%20n-1%2C%20n%0A%20%20%20%20for%20i%20in%20range%281%2C%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%BF%B4%E5%9C%88%20j%20%3D%201%2C%202%2C%20...%2C%20n-1%2C%20n%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%281%2C%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20res%20%2B%3D%20f%22%28%7Bi%7D%2C%20%7Bj%7D%29%2C%20%22%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20nested_for_loop%28n%29%0A%20%20%20%20print%28f%22%5Cn%E9%9B%99%E5%B1%A4%20for%20%E8%BF%B4%E5%9C%88%E7%9A%84%E8%B5%B0%E8%A8%AA%E7%B5%90%E6%9E%9C%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{recursion}-[class]{}-[func]{recur} --> <!-- [file]{recursion}-[class]{}-[func]{recur} -->
https://pythontutor.com/render.html#code=def%20recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20%23%20%E9%80%92%EF%BC%9A%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%0A%20%20%20%20res%20%3D%20recur%28n%20-%201%29%0A%20%20%20%20%23%20%E5%BD%92%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%0A%20%20%20%20return%20n%20%2B%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E7%B5%82%E6%AD%A2%E6%A2%9D%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20%23%20%E9%81%9E%EF%BC%9A%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%0A%20%20%20%20res%20%3D%20recur%28n%20-%201%29%0A%20%20%20%20%23%20%E8%BF%B4%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%B5%90%E6%9E%9C%0A%20%20%20%20return%20n%20%2B%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E9%81%9E%E8%BF%B4%E5%87%BD%E5%BC%8F%E7%9A%84%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{recursion}-[class]{}-[func]{tail_recur} --> <!-- [file]{recursion}-[class]{}-[func]{tail_recur} -->
https://pythontutor.com/render.html#code=def%20tail_recur%28n,%20res%29%3A%0A%20%20%20%20%22%22%22%E5%B0%BE%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20res%0A%20%20%20%20%23%20%E5%B0%BE%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%0A%20%20%20%20return%20tail_recur%28n%20-%201,%20res%20%2B%20n%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20tail_recur%28n,%200%29%0A%20%20%20%20print%28f%22%5Cn%E5%B0%BE%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20tail_recur%28n%2C%20res%29%3A%0A%20%20%20%20%22%22%22%E5%B0%BE%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E7%B5%82%E6%AD%A2%E6%A2%9D%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20res%0A%20%20%20%20%23%20%E5%B0%BE%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%0A%20%20%20%20return%20tail_recur%28n%20-%201%2C%20res%20%2B%20n%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20tail_recur%28n%2C%200%29%0A%20%20%20%20print%28f%22%5Cn%E5%B0%BE%E9%81%9E%E8%BF%B4%E5%87%BD%E5%BC%8F%E7%9A%84%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{recursion}-[class]{}-[func]{fib} --> <!-- [file]{recursion}-[class]{}-[func]{fib} -->
https://pythontutor.com/render.html#code=def%20fib%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97%EF%BC%9A%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%20f%281%29%20%3D%200,%20f%282%29%20%3D%201%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%20-%201%0A%20%20%20%20%23%20%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%20f%28n%29%20%3D%20f%28n-1%29%20%2B%20f%28n-2%29%0A%20%20%20%20res%20%3D%20fib%28n%20-%201%29%20%2B%20fib%28n%20-%202%29%0A%20%20%20%20%23%20%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%20f%28n%29%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20fib%28n%29%0A%20%20%20%20print%28f%22%5Cn%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97%E7%9A%84%E7%AC%AC%20%7Bn%7D%20%E9%A1%B9%E4%B8%BA%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20fib%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E8%B2%BB%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B8%E5%88%97%EF%BC%9A%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E7%B5%82%E6%AD%A2%E6%A2%9D%E4%BB%B6%20f%281%29%20%3D%200%2C%20f%282%29%20%3D%201%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%20-%201%0A%20%20%20%20%23%20%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%20f%28n%29%20%3D%20f%28n-1%29%20%2B%20f%28n-2%29%0A%20%20%20%20res%20%3D%20fib%28n%20-%201%29%20%2B%20fib%28n%20-%202%29%0A%20%20%20%20%23%20%E8%BF%94%E5%9B%9E%E7%B5%90%E6%9E%9C%20f%28n%29%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20fib%28n%29%0A%20%20%20%20print%28f%22%5Cn%E8%B2%BB%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B8%E5%88%97%E7%9A%84%E7%AC%AC%20%7Bn%7D%20%E9%A0%85%E7%82%BA%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{recursion}-[class]{}-[func]{for_loop_recur} --> <!-- [file]{recursion}-[class]{}-[func]{for_loop_recur} -->
https://pythontutor.com/render.html#code=def%20for_loop_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%8B%9F%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E4%BD%BF%E7%94%A8%E4%B8%80%E4%B8%AA%E6%98%BE%E5%BC%8F%E7%9A%84%E6%A0%88%E6%9D%A5%E6%A8%A1%E6%8B%9F%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E6%A0%88%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20%23%20%E9%80%92%EF%BC%9A%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%0A%20%20%20%20for%20i%20in%20range%28n,%200,%20-1%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E2%80%9C%E5%85%A5%E6%A0%88%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%8B%9F%E2%80%9C%E9%80%92%E2%80%9D%0A%20%20%20%20%20%20%20%20stack.append%28i%29%0A%20%20%20%20%23%20%E5%BD%92%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%0A%20%20%20%20while%20stack%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E2%80%9C%E5%87%BA%E6%A0%88%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%8B%9F%E2%80%9C%E5%BD%92%E2%80%9D%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20stack.pop%28%29%0A%20%20%20%20%23%20res%20%3D%201%2B2%2B3%2B...%2Bn%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20for_loop_recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%8B%9F%E9%80%92%E5%BD%92%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20for_loop_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%93%AC%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E4%BD%BF%E7%94%A8%E4%B8%80%E5%80%8B%E9%A1%AF%E5%BC%8F%E7%9A%84%E5%A0%86%E7%96%8A%E4%BE%86%E6%A8%A1%E6%93%AC%E7%B3%BB%E7%B5%B1%E5%91%BC%E5%8F%AB%E5%A0%86%E7%96%8A%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20%23%20%E9%81%9E%EF%BC%9A%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%0A%20%20%20%20for%20i%20in%20range%28n%2C%200%2C%20-1%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%8F%E9%81%8E%E2%80%9C%E5%85%A5%E5%A0%86%E7%96%8A%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%93%AC%E2%80%9C%E9%81%9E%E2%80%9D%0A%20%20%20%20%20%20%20%20stack.append%28i%29%0A%20%20%20%20%23%20%E8%BF%B4%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%B5%90%E6%9E%9C%0A%20%20%20%20while%20stack%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%8F%E9%81%8E%E2%80%9C%E5%87%BA%E5%A0%86%E7%96%8A%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%93%AC%E2%80%9C%E8%BF%B4%E2%80%9D%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20stack.pop%28%29%0A%20%20%20%20%23%20res%20%3D%201%2B2%2B3%2B...%2Bn%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20for_loop_recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%93%AC%E9%81%9E%E8%BF%B4%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View File

@@ -5,13 +5,13 @@
---> --->
<!-- [file]{recursion}-[class]{}-[func]{recur} --> <!-- [file]{recursion}-[class]{}-[func]{recur} -->
https://pythontutor.com/render.html#code=def%20recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20%23%20%E9%80%92%EF%BC%9A%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%0A%20%20%20%20res%20%3D%20recur%28n%20-%201%29%0A%20%20%20%20%23%20%E5%BD%92%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%0A%20%20%20%20return%20n%20%2B%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E7%B5%82%E6%AD%A2%E6%A2%9D%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20%23%20%E9%81%9E%EF%BC%9A%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%0A%20%20%20%20res%20%3D%20recur%28n%20-%201%29%0A%20%20%20%20%23%20%E8%BF%B4%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%B5%90%E6%9E%9C%0A%20%20%20%20return%20n%20%2B%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E9%81%9E%E8%BF%B4%E5%87%BD%E5%BC%8F%E7%9A%84%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{recursion}-[class]{}-[func]{tail_recur} --> <!-- [file]{recursion}-[class]{}-[func]{tail_recur} -->
https://pythontutor.com/render.html#code=def%20tail_recur%28n,%20res%29%3A%0A%20%20%20%20%22%22%22%E5%B0%BE%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20res%0A%20%20%20%20%23%20%E5%B0%BE%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%0A%20%20%20%20return%20tail_recur%28n%20-%201,%20res%20%2B%20n%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20tail_recur%28n,%200%29%0A%20%20%20%20print%28f%22%5Cn%E5%B0%BE%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20tail_recur%28n%2C%20res%29%3A%0A%20%20%20%20%22%22%22%E5%B0%BE%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E7%B5%82%E6%AD%A2%E6%A2%9D%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20res%0A%20%20%20%20%23%20%E5%B0%BE%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%0A%20%20%20%20return%20tail_recur%28n%20-%201%2C%20res%20%2B%20n%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20tail_recur%28n%2C%200%29%0A%20%20%20%20print%28f%22%5Cn%E5%B0%BE%E9%81%9E%E8%BF%B4%E5%87%BD%E5%BC%8F%E7%9A%84%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{recursion}-[class]{}-[func]{fib} --> <!-- [file]{recursion}-[class]{}-[func]{fib} -->
https://pythontutor.com/render.html#code=def%20fib%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97%EF%BC%9A%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%20f%281%29%20%3D%200,%20f%282%29%20%3D%201%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%20-%201%0A%20%20%20%20%23%20%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%20f%28n%29%20%3D%20f%28n-1%29%20%2B%20f%28n-2%29%0A%20%20%20%20res%20%3D%20fib%28n%20-%201%29%20%2B%20fib%28n%20-%202%29%0A%20%20%20%20%23%20%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%20f%28n%29%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20fib%28n%29%0A%20%20%20%20print%28f%22%5Cn%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97%E7%9A%84%E7%AC%AC%20%7Bn%7D%20%E9%A1%B9%E4%B8%BA%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20fib%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E8%B2%BB%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B8%E5%88%97%EF%BC%9A%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E7%B5%82%E6%AD%A2%E6%A2%9D%E4%BB%B6%20f%281%29%20%3D%200%2C%20f%282%29%20%3D%201%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%20-%201%0A%20%20%20%20%23%20%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%20f%28n%29%20%3D%20f%28n-1%29%20%2B%20f%28n-2%29%0A%20%20%20%20res%20%3D%20fib%28n%20-%201%29%20%2B%20fib%28n%20-%202%29%0A%20%20%20%20%23%20%E8%BF%94%E5%9B%9E%E7%B5%90%E6%9E%9C%20f%28n%29%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20fib%28n%29%0A%20%20%20%20print%28f%22%5Cn%E8%B2%BB%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B8%E5%88%97%E7%9A%84%E7%AC%AC%20%7Bn%7D%20%E9%A0%85%E7%82%BA%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
<!-- [file]{recursion}-[class]{}-[func]{for_loop_recur} --> <!-- [file]{recursion}-[class]{}-[func]{for_loop_recur} -->
https://pythontutor.com/render.html#code=def%20for_loop_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%8B%9F%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E4%BD%BF%E7%94%A8%E4%B8%80%E4%B8%AA%E6%98%BE%E5%BC%8F%E7%9A%84%E6%A0%88%E6%9D%A5%E6%A8%A1%E6%8B%9F%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E6%A0%88%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20%23%20%E9%80%92%EF%BC%9A%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%0A%20%20%20%20for%20i%20in%20range%28n,%200,%20-1%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E2%80%9C%E5%85%A5%E6%A0%88%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%8B%9F%E2%80%9C%E9%80%92%E2%80%9D%0A%20%20%20%20%20%20%20%20stack.append%28i%29%0A%20%20%20%20%23%20%E5%BD%92%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%0A%20%20%20%20while%20stack%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E2%80%9C%E5%87%BA%E6%A0%88%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%8B%9F%E2%80%9C%E5%BD%92%E2%80%9D%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20stack.pop%28%29%0A%20%20%20%20%23%20res%20%3D%201%2B2%2B3%2B...%2Bn%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20for_loop_recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%8B%9F%E9%80%92%E5%BD%92%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false https://pythontutor.com/render.html#code=def%20for_loop_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%93%AC%E9%81%9E%E8%BF%B4%22%22%22%0A%20%20%20%20%23%20%E4%BD%BF%E7%94%A8%E4%B8%80%E5%80%8B%E9%A1%AF%E5%BC%8F%E7%9A%84%E5%A0%86%E7%96%8A%E4%BE%86%E6%A8%A1%E6%93%AC%E7%B3%BB%E7%B5%B1%E5%91%BC%E5%8F%AB%E5%A0%86%E7%96%8A%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20%23%20%E9%81%9E%EF%BC%9A%E9%81%9E%E8%BF%B4%E5%91%BC%E5%8F%AB%0A%20%20%20%20for%20i%20in%20range%28n%2C%200%2C%20-1%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%8F%E9%81%8E%E2%80%9C%E5%85%A5%E5%A0%86%E7%96%8A%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%93%AC%E2%80%9C%E9%81%9E%E2%80%9D%0A%20%20%20%20%20%20%20%20stack.append%28i%29%0A%20%20%20%20%23%20%E8%BF%B4%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%B5%90%E6%9E%9C%0A%20%20%20%20while%20stack%3A%0A%20%20%20%20%20%20%20%20%23%20%E9%80%8F%E9%81%8E%E2%80%9C%E5%87%BA%E5%A0%86%E7%96%8A%E6%93%8D%E4%BD%9C%E2%80%9D%E6%A8%A1%E6%93%AC%E2%80%9C%E8%BF%B4%E2%80%9D%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20stack.pop%28%29%0A%20%20%20%20%23%20res%20%3D%201%2B2%2B3%2B...%2Bn%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20for_loop_recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E4%BD%BF%E7%94%A8%E8%BF%AD%E4%BB%A3%E6%A8%A1%E6%93%AC%E9%81%9E%E8%BF%B4%E6%B1%82%E5%92%8C%E7%B5%90%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

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