mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-04 03:00:06 +08:00
deploy
This commit is contained in:
@@ -60,7 +60,18 @@
|
||||
|
||||
|
||||
|
||||
</head>
|
||||
<link href="../../assets/stylesheets/glightbox.min.css" rel="stylesheet"/><style>
|
||||
html.glightbox-open { overflow: initial; height: 100%; }
|
||||
.gslide-title { margin-top: 0px; user-select: text; }
|
||||
.gslide-desc { color: #666; user-select: text; }
|
||||
.gslide-image img { background: white; }
|
||||
|
||||
.gscrollbar-fixer { padding-right: 15px; }
|
||||
.gdesc-inner { font-size: 0.75rem; }
|
||||
body[data-md-color-scheme="slate"] .gdesc-inner { background: var(--md-default-bg-color);}
|
||||
body[data-md-color-scheme="slate"] .gslide-title { color: var(--md-default-fg-color);}
|
||||
body[data-md-color-scheme="slate"] .gslide-desc { color: var(--md-default-fg-color);}
|
||||
</style> <script src="../../assets/javascripts/glightbox.min.js"></script></head>
|
||||
|
||||
|
||||
|
||||
@@ -1875,14 +1886,6 @@
|
||||
10.2 二分查找插入点
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -1903,14 +1906,6 @@
|
||||
10.3 二分查找边界
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2332,14 +2327,6 @@
|
||||
第 12 章 分治
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -2371,14 +2358,6 @@
|
||||
12.1 分治算法
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2399,14 +2378,6 @@
|
||||
12.2 分治搜索策略
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2436,14 +2407,6 @@
|
||||
12.3 构建树问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
</label>
|
||||
@@ -2455,14 +2418,6 @@
|
||||
12.3 构建树问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -2531,14 +2486,6 @@
|
||||
12.4 汉诺塔问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2559,14 +2506,6 @@
|
||||
12.5 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2798,14 +2737,6 @@
|
||||
第 14 章 动态规划
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -2837,14 +2768,6 @@
|
||||
14.1 初探动态规划
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2865,14 +2788,6 @@
|
||||
14.2 DP 问题特性
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2893,14 +2808,6 @@
|
||||
14.3 DP 解题思路
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2921,14 +2828,6 @@
|
||||
14.4 0-1 背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2949,14 +2848,6 @@
|
||||
14.5 完全背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2977,14 +2868,6 @@
|
||||
14.6 编辑距离问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3005,14 +2888,6 @@
|
||||
14.7 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3071,14 +2946,6 @@
|
||||
第 15 章 贪心
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -3110,14 +2977,6 @@
|
||||
15.1 贪心算法
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3138,14 +2997,6 @@
|
||||
15.2 分数背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3166,14 +3017,6 @@
|
||||
15.3 最大容量问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3194,14 +3037,6 @@
|
||||
15.4 最大切分乘积问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3222,14 +3057,6 @@
|
||||
15.5 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3514,7 +3341,7 @@
|
||||
<p class="admonition-title">Question</p>
|
||||
<p>给定一个二叉树的前序遍历 <code>preorder</code> 和中序遍历 <code>inorder</code> ,请从中构建二叉树,返回二叉树的根节点。</p>
|
||||
</div>
|
||||
<p><img alt="构建二叉树的示例数据" src="../build_binary_tree_problem.assets/build_tree_example.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/build_tree_example.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="构建二叉树的示例数据" src="../build_binary_tree_problem.assets/build_tree_example.png" /></a></p>
|
||||
<p align="center"> 图 12-5 构建二叉树的示例数据 </p>
|
||||
|
||||
<h3 id="1">1. 判断是否为分治问题<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
|
||||
@@ -3537,7 +3364,7 @@
|
||||
<li>查找根节点 3 在 <code>inorder</code> 中的索引,利用该索引可将 <code>inorder</code> 划分为 <code>[ 9 | 3 | 1 2 7 ]</code> 。</li>
|
||||
<li>根据 <code>inorder</code> 划分结果,易得左子树和右子树的节点数量分别为 1 和 3 ,从而可将 <code>preorder</code> 划分为 <code>[ 3 | 9 | 2 1 7 ]</code> 。</li>
|
||||
</ol>
|
||||
<p><img alt="在前序和中序遍历中划分子树" src="../build_binary_tree_problem.assets/build_tree_preorder_inorder_division.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/build_tree_preorder_inorder_division.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="在前序和中序遍历中划分子树" src="../build_binary_tree_problem.assets/build_tree_preorder_inorder_division.png" /></a></p>
|
||||
<p align="center"> 图 12-6 在前序和中序遍历中划分子树 </p>
|
||||
|
||||
<h3 id="3">3. 基于变量描述子树区间<a class="headerlink" href="#3" title="Permanent link">¶</a></h3>
|
||||
@@ -3579,7 +3406,7 @@
|
||||
</table>
|
||||
</div>
|
||||
<p>请注意,右子树根节点索引中的 <span class="arithmatex">\((m-l)\)</span> 的含义是“左子树的节点数量”,建议配合图 12-7 理解。</p>
|
||||
<p><img alt="根节点和左右子树的索引区间表示" src="../build_binary_tree_problem.assets/build_tree_division_pointers.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/build_tree_division_pointers.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="根节点和左右子树的索引区间表示" src="../build_binary_tree_problem.assets/build_tree_division_pointers.png" /></a></p>
|
||||
<p align="center"> 图 12-7 根节点和左右子树的索引区间表示 </p>
|
||||
|
||||
<h3 id="4">4. 代码实现<a class="headerlink" href="#4" title="Permanent link">¶</a></h3>
|
||||
@@ -3914,38 +3741,38 @@
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:9"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1"><1></label><label for="__tabbed_2_2"><2></label><label for="__tabbed_2_3"><3></label><label for="__tabbed_2_4"><4></label><label for="__tabbed_2_5"><5></label><label for="__tabbed_2_6"><6></label><label for="__tabbed_2_7"><7></label><label for="__tabbed_2_8"><8></label><label for="__tabbed_2_9"><9></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="构建二叉树的递归过程" src="../build_binary_tree_problem.assets/built_tree_step1.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step1.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="构建二叉树的递归过程" src="../build_binary_tree_problem.assets/built_tree_step1.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step2" src="../build_binary_tree_problem.assets/built_tree_step2.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step2.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step2" src="../build_binary_tree_problem.assets/built_tree_step2.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step3" src="../build_binary_tree_problem.assets/built_tree_step3.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step3.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step3" src="../build_binary_tree_problem.assets/built_tree_step3.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step4" src="../build_binary_tree_problem.assets/built_tree_step4.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step4.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step4" src="../build_binary_tree_problem.assets/built_tree_step4.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step5" src="../build_binary_tree_problem.assets/built_tree_step5.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step5.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step5" src="../build_binary_tree_problem.assets/built_tree_step5.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step6" src="../build_binary_tree_problem.assets/built_tree_step6.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step6.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step6" src="../build_binary_tree_problem.assets/built_tree_step6.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step7" src="../build_binary_tree_problem.assets/built_tree_step7.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step7.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step7" src="../build_binary_tree_problem.assets/built_tree_step7.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step8" src="../build_binary_tree_problem.assets/built_tree_step8.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step8.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step8" src="../build_binary_tree_problem.assets/built_tree_step8.png" /></a></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="built_tree_step9" src="../build_binary_tree_problem.assets/built_tree_step9.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_step9.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="built_tree_step9" src="../build_binary_tree_problem.assets/built_tree_step9.png" /></a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p align="center"> 图 12-8 构建二叉树的递归过程 </p>
|
||||
|
||||
<p>每个递归函数内的前序遍历 <code>preorder</code> 和中序遍历 <code>inorder</code> 的划分结果如图 12-9 所示。</p>
|
||||
<p><img alt="每个递归函数中的划分结果" src="../build_binary_tree_problem.assets/built_tree_overall.png" /></p>
|
||||
<p><a class="glightbox" href="../build_binary_tree_problem.assets/built_tree_overall.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="每个递归函数中的划分结果" src="../build_binary_tree_problem.assets/built_tree_overall.png" /></a></p>
|
||||
<p align="center"> 图 12-9 每个递归函数中的划分结果 </p>
|
||||
|
||||
<p>设树的节点数量为 <span class="arithmatex">\(n\)</span> ,初始化每一个节点(执行一个递归函数 <code>dfs()</code> )使用 <span class="arithmatex">\(O(1)\)</span> 时间。<strong>因此总体时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> 。</p>
|
||||
@@ -4113,10 +3940,15 @@ aria-label="页脚"
|
||||
<div class="md-copyright">
|
||||
|
||||
<div class="md-copyright__highlight">
|
||||
Copyright © 2023 Krahets
|
||||
Copyright © 2022 - 2023 Krahets
|
||||
</div>
|
||||
|
||||
|
||||
Made with
|
||||
<a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
|
||||
Material for MkDocs
|
||||
</a>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Social links -->
|
||||
@@ -4185,5 +4017,5 @@ aria-label="页脚"
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
<script>document$.subscribe(() => {const lightbox = GLightbox({"touchNavigation": true, "loop": false, "zoomable": true, "draggable": false, "openEffect": "zoom", "closeEffect": "zoom", "slideEffect": "none"});})</script></body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user