Skip to content

Commit 5a9920b

Browse files
committed
Deployed 3f118f5 with MkDocs version: 1.0.4
1 parent 4a80d96 commit 5a9920b

File tree

5 files changed

+157
-26
lines changed

5 files changed

+157
-26
lines changed

19_python内置常用算法和数据结构/builtins/index.html

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
<li class="toctree-l2"><a href="#python-int">python int 值范围</a></li>
177177

178178

179+
<li class="toctree-l2"><a href="#python-dict">python dict 排序</a></li>
180+
181+
179182
<li class="toctree-l2"><a href="#_2">链表题目调试函数</a></li>
180183

181184

@@ -185,6 +188,9 @@
185188
<li class="toctree-l2"><a href="#lru_cachecache">lru_cache/cache 优化记忆化搜索</a></li>
186189

187190

191+
<li class="toctree-l2"><a href="#leetcode">leetcode 二叉树调试函数</a></li>
192+
193+
188194
</ul>
189195
</li>
190196

@@ -320,6 +326,17 @@ <h1 id="python-int">python int 值范围</h1>
320326
MAXINT = (1&lt;&lt;63) - 1
321327
MININT = ~MAXINT
322328
</code></pre>
329+
<h1 id="python-dict">python dict 排序</h1>
330+
<pre><code class="language-py"># 补充 python 根据 key,value 排序字典的
331+
d = {'d': 4, 'a': 1, 'b': 2, 'c':3}
332+
# sort by key and reverse
333+
dict(sorted(d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
334+
dict(sorted(d.items(), reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
335+
336+
# sort by value and reverse
337+
dict(sorted(d.items(), key = lambda kv:kv[1])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
338+
dict(sorted(d.items(), key = lambda kv:kv[1], reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
339+
</code></pre>
323340
<h1 id="_2">链表题目调试函数</h1>
324341
<pre><code class="language-py"># 编写链表题目经常用到的一些通用函数和调试函数,定义等,方便代码调试
325342

@@ -470,6 +487,120 @@ <h1 id="lru_cachecache">lru_cache/cache 优化记忆化搜索</h1>
470487

471488
return dfs(root)
472489

490+
</code></pre>
491+
<h1 id="leetcode">leetcode 二叉树调试函数</h1>
492+
<pre><code class="language-py">&quot;&quot;&quot;
493+
二叉树树相关问题调试函数
494+
&quot;&quot;&quot;
495+
496+
497+
class TreeNode(object): # leetcode tree 节点定义
498+
def __init__(self, val=0, left=None, right=None):
499+
self.val = val
500+
self.left = left
501+
self.right = right
502+
503+
def __str__(self):
504+
return &quot;TreeNode:{} left:{} right:{}&quot;.format(self.val, self.left, self.right)
505+
__repr__ = __str__
506+
507+
508+
def gen_tree_from_lc_input(vals_str): # [1,2,3] -&gt; root TreeNode
509+
&quot;&quot;&quot; 根据 输入生成一个 tree,返回 root 节点,注意输入字符串
510+
# [450] 删除二叉搜索树中的节点
511+
# https://leetcode-cn.com/problems/delete-node-in-a-bst/description/
512+
# 比如 450 题目单测代码可以这么写
513+
def test():
514+
s = Solution()
515+
root = gen_tree_from_lc_input(&quot;[2,1]&quot;)
516+
key = 1
517+
res = &quot;[2]&quot;
518+
assert to_lc_tree_str(s.deleteNode(root, key)) == res
519+
&quot;&quot;&quot;
520+
import ast
521+
valids = vals_str.replace(&quot;null&quot;, &quot;None&quot;)
522+
vals = ast.literal_eval(valids)
523+
# 以下就是 gen_tree 函数的内容,为了方便单独使用不调用函数了
524+
if not vals:
525+
return None
526+
nodemap = {}
527+
for i in range(len(vals)):
528+
if vals[i] is not None: # 一开始写的 if vals[i],但是 0 节点就错了!!!
529+
nodemap[i] = TreeNode(vals[i])
530+
else:
531+
nodemap[i] = None
532+
533+
root = nodemap[0]
534+
for i in range(len(vals)):
535+
l = 2*i + 1
536+
r = 2*i + 2
537+
cur = nodemap.get(i, None)
538+
left = nodemap.get(l, None)
539+
right = nodemap.get(r, None)
540+
if cur:
541+
cur.left = left
542+
cur.right = right
543+
return root
544+
545+
546+
def to_lc_tree_str(root): # root TreeNode -&gt; [1,2,3,null]
547+
&quot;&quot;&quot;返回层序序列化后的树字符串,可以和 leetcode 输出结果比对字符串&quot;&quot;&quot;
548+
import json
549+
if not root:
550+
return '[]'
551+
curnodes = [root]
552+
res = [root.val]
553+
while curnodes:
554+
nextnodes = []
555+
for node in curnodes:
556+
if node:
557+
if node.left:
558+
nextnodes.append(node.left)
559+
res.append(node.left.val)
560+
else:
561+
nextnodes.append(None)
562+
res.append(None)
563+
if node.right:
564+
nextnodes.append(node.right)
565+
res.append(node.right.val)
566+
else:
567+
nextnodes.append(None)
568+
res.append(None)
569+
curnodes = nextnodes
570+
571+
while res[-1] is None: # 最后空节点去掉
572+
res.pop()
573+
s = json.dumps(res)
574+
s = s.replace(&quot; &quot;, &quot;&quot;)
575+
return s
576+
577+
578+
def gen_tree(vals):
579+
&quot;&quot;&quot;
580+
根据层序遍历结果生成二叉树并且返回 root。
581+
把题目中输入 null 换成 None
582+
vals = [1,2,3,None,5]
583+
&quot;&quot;&quot;
584+
if not vals:
585+
return None
586+
nodemap = {}
587+
for i in range(len(vals)):
588+
if vals[i]:
589+
nodemap[i] = TreeNode(vals[i])
590+
else:
591+
nodemap[i] = None
592+
593+
root = nodemap[0]
594+
for i in range(len(vals)):
595+
l = 2*i + 1
596+
r = 2*i + 2
597+
cur = nodemap.get(i, None)
598+
left = nodemap.get(l, None)
599+
right = nodemap.get(r, None)
600+
if cur:
601+
cur.left = left
602+
cur.right = right
603+
return root
473604
</code></pre>
474605

475606
</div>

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,5 +599,5 @@ <h2 id="_22">本电子书制作和写作方式</h2>
599599

600600
<!--
601601
MkDocs version : 1.0.4
602-
Build Date UTC : 2022-02-02 08:39:34
602+
Build Date UTC : 2022-02-11 11:03:08
603603
-->

search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

sitemap.xml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,122 @@
22
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
33
<url>
44
<loc>None</loc>
5-
<lastmod>2022-02-02</lastmod>
5+
<lastmod>2022-02-11</lastmod>
66
<changefreq>daily</changefreq>
77
</url>
88
<url>
99
<loc>None</loc>
10-
<lastmod>2022-02-02</lastmod>
10+
<lastmod>2022-02-11</lastmod>
1111
<changefreq>daily</changefreq>
1212
</url>
1313
<url>
1414
<loc>None</loc>
15-
<lastmod>2022-02-02</lastmod>
15+
<lastmod>2022-02-11</lastmod>
1616
<changefreq>daily</changefreq>
1717
</url>
1818
<url>
1919
<loc>None</loc>
20-
<lastmod>2022-02-02</lastmod>
20+
<lastmod>2022-02-11</lastmod>
2121
<changefreq>daily</changefreq>
2222
</url>
2323
<url>
2424
<loc>None</loc>
25-
<lastmod>2022-02-02</lastmod>
25+
<lastmod>2022-02-11</lastmod>
2626
<changefreq>daily</changefreq>
2727
</url>
2828
<url>
2929
<loc>None</loc>
30-
<lastmod>2022-02-02</lastmod>
30+
<lastmod>2022-02-11</lastmod>
3131
<changefreq>daily</changefreq>
3232
</url>
3333
<url>
3434
<loc>None</loc>
35-
<lastmod>2022-02-02</lastmod>
35+
<lastmod>2022-02-11</lastmod>
3636
<changefreq>daily</changefreq>
3737
</url>
3838
<url>
3939
<loc>None</loc>
40-
<lastmod>2022-02-02</lastmod>
40+
<lastmod>2022-02-11</lastmod>
4141
<changefreq>daily</changefreq>
4242
</url>
4343
<url>
4444
<loc>None</loc>
45-
<lastmod>2022-02-02</lastmod>
45+
<lastmod>2022-02-11</lastmod>
4646
<changefreq>daily</changefreq>
4747
</url>
4848
<url>
4949
<loc>None</loc>
50-
<lastmod>2022-02-02</lastmod>
50+
<lastmod>2022-02-11</lastmod>
5151
<changefreq>daily</changefreq>
5252
</url>
5353
<url>
5454
<loc>None</loc>
55-
<lastmod>2022-02-02</lastmod>
55+
<lastmod>2022-02-11</lastmod>
5656
<changefreq>daily</changefreq>
5757
</url>
5858
<url>
5959
<loc>None</loc>
60-
<lastmod>2022-02-02</lastmod>
60+
<lastmod>2022-02-11</lastmod>
6161
<changefreq>daily</changefreq>
6262
</url>
6363
<url>
6464
<loc>None</loc>
65-
<lastmod>2022-02-02</lastmod>
65+
<lastmod>2022-02-11</lastmod>
6666
<changefreq>daily</changefreq>
6767
</url>
6868
<url>
6969
<loc>None</loc>
70-
<lastmod>2022-02-02</lastmod>
70+
<lastmod>2022-02-11</lastmod>
7171
<changefreq>daily</changefreq>
7272
</url>
7373
<url>
7474
<loc>None</loc>
75-
<lastmod>2022-02-02</lastmod>
75+
<lastmod>2022-02-11</lastmod>
7676
<changefreq>daily</changefreq>
7777
</url>
7878
<url>
7979
<loc>None</loc>
80-
<lastmod>2022-02-02</lastmod>
80+
<lastmod>2022-02-11</lastmod>
8181
<changefreq>daily</changefreq>
8282
</url>
8383
<url>
8484
<loc>None</loc>
85-
<lastmod>2022-02-02</lastmod>
85+
<lastmod>2022-02-11</lastmod>
8686
<changefreq>daily</changefreq>
8787
</url>
8888
<url>
8989
<loc>None</loc>
90-
<lastmod>2022-02-02</lastmod>
90+
<lastmod>2022-02-11</lastmod>
9191
<changefreq>daily</changefreq>
9292
</url>
9393
<url>
9494
<loc>None</loc>
95-
<lastmod>2022-02-02</lastmod>
95+
<lastmod>2022-02-11</lastmod>
9696
<changefreq>daily</changefreq>
9797
</url>
9898
<url>
9999
<loc>None</loc>
100-
<lastmod>2022-02-02</lastmod>
100+
<lastmod>2022-02-11</lastmod>
101101
<changefreq>daily</changefreq>
102102
</url>
103103
<url>
104104
<loc>None</loc>
105-
<lastmod>2022-02-02</lastmod>
105+
<lastmod>2022-02-11</lastmod>
106106
<changefreq>daily</changefreq>
107107
</url>
108108
<url>
109109
<loc>None</loc>
110-
<lastmod>2022-02-02</lastmod>
110+
<lastmod>2022-02-11</lastmod>
111111
<changefreq>daily</changefreq>
112112
</url>
113113
<url>
114114
<loc>None</loc>
115-
<lastmod>2022-02-02</lastmod>
115+
<lastmod>2022-02-11</lastmod>
116116
<changefreq>daily</changefreq>
117117
</url>
118118
<url>
119119
<loc>None</loc>
120-
<lastmod>2022-02-02</lastmod>
120+
<lastmod>2022-02-11</lastmod>
121121
<changefreq>daily</changefreq>
122122
</url>
123123
</urlset>

sitemap.xml.gz

2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)