Skip to content

Commit 1dbfff0

Browse files
committed
Deployed 96f9308 with MkDocs version: 1.0.4
1 parent fd713d9 commit 1dbfff0

File tree

5 files changed

+49
-40
lines changed

5 files changed

+49
-40
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
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>
179+
<li class="toctree-l2"><a href="#python-listdict">python list/dict 排序技巧</a></li>
180180

181181

182182
<li class="toctree-l2"><a href="#_2">链表题目调试函数</a></li>
@@ -197,7 +197,7 @@
197197
<li class="toctree-l2"><a href="#python_2">python 交换列表元素的坑</a></li>
198198

199199

200-
<li class="toctree-l2"><a href="#_4">兼容代码提交格式</a></li>
200+
<li class="toctree-l2"><a href="#acm">兼容代码ACM/核心提交格式</a></li>
201201

202202

203203
</ul>
@@ -311,9 +311,9 @@ <h1 id="python">Python 刷题常用内置算法和数据结构</h1>
311311
<h1 id="_1">一些坑</h1>
312312
<p>如果你使用 python2 or python3 刷题(比如力扣leetcode),有一些坑或者技巧需要注意:</p>
313313
<ul>
314-
<li>python3 和 python2 的 dict 有所用不同,python3.7 之后的 dict 会保持插入顺序, python2 不要依赖 dict 迭代顺序,请使用 OrderedDict</li>
314+
<li>python3 和 python2 的 dict 有所用不同,python3.7 之后的 dict 会保持插入顺序(不是字典序), python2 不要依赖 dict 迭代顺序,请使用 OrderedDict</li>
315315
<li>正确初始化一个二维数组:<code>dp = [[0 for _ in range(col)] for _ in range(row)]</code>,不要用 <code>dp = [[0] * n] * m</code>, 否则里边都
316-
引用的同一个 list,修改一个都会变</li>
316+
引用的同一个 list,修改一个都会变<code>[[val]*cols for _ in range(rows)]</code> 也行</li>
317317
<li>python在数值范围建议用:<code>MAXINT = 2**63-1; MININT = -2**63</code> 。因为 python2 sys.maxint 和 python3 sys.maxsize 不统一</li>
318318
<li>优先级队列:使用内置queue.PriorityQueue or heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现,下边有代码演示</li>
319319
<li>python3 的 functools 模块自带了 cache(等价于lru_cache(maxsize=None)) 和 lru_cache 装饰器,在一些需要递归记忆化搜索的时候会很方便</li>
@@ -339,16 +339,25 @@ <h1 id="python-int">python int 值范围</h1>
339339
MAXINT = (1&lt;&lt;63) - 1
340340
MININT = ~MAXINT
341341
</code></pre>
342-
<h1 id="python-dict">python dict 排序</h1>
343-
<pre><code class="language-py"># 补充 python 根据 key,value 排序字典的
342+
<h1 id="python-listdict">python list/dict 排序技巧</h1>
343+
<pre><code class="language-py"># python 根据 key,value 排序字典
344344
d = {'d': 4, 'a': 1, 'b': 2, 'c':3}
345-
# sort by key and reverse
345+
# dict sort by key and reverse
346346
dict(sorted(d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
347347
dict(sorted(d.items(), reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
348348

349-
# sort by value and reverse
349+
# dict sort by value and reverse
350350
dict(sorted(d.items(), key = lambda kv:kv[1])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
351351
dict(sorted(d.items(), key = lambda kv:kv[1], reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
352+
353+
# 排序嵌套 list
354+
l = [('a', 1), ('c', 2), ('b',3)]
355+
sorted(l, key=lambda p:p[0]) # 根据第1个值排序,[('a', 1), ('b', 3), ('c', 2)]
356+
sorted(l, key=lambda p:p[1]) # 根据第2个值排序,[('a', 1), ('c', 2), ('b', 3)]
357+
358+
# 同时获取最大值的下标和值
359+
l = [1,2,5,4,3]
360+
maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5
352361
</code></pre>
353362
<h1 id="_2">链表题目调试函数</h1>
354363
<pre><code class="language-py"># 编写链表题目经常用到的一些通用函数和调试函数,定义等,方便代码调试
@@ -444,11 +453,11 @@ <h1 id="_3">内置库实现优先级队列的三种方式</h1>
444453
def __lt__(self, other): # heapq 源码实现只用了 小于 比较,这里定义了就可以 push 一个 item 类
445454
return self.weight &lt; other.weight
446455

447-
def __eq__(self, other): # 这个可以省略,只要定义了 __lt__ 魔法函数就可以了
448-
return self.weight == other.weight
449-
450-
def __str__(self):
451-
return '{}:{}'.format(self.key,self.weight)
456+
# def __eq__(self, other): # 这个可以省略,只要定义了 __lt__ 魔法函数就可以了
457+
# return self.weight == other.weight
458+
#
459+
# def __str__(self):
460+
# return '{}:{}'.format(self.key,self.weight)
452461

453462
# Item.__lt__ = lambda self, other: self.weight &lt; other.weight # 对于已有的类,直接加一句就可以实现作为 heap item 了
454463

@@ -683,7 +692,7 @@ <h1 id="python_2">python 交换列表元素的坑</h1>
683692

684693
return n+1
685694
</code></pre>
686-
<h1 id="_4">兼容代码提交格式</h1>
695+
<h1 id="acm">兼容代码ACM/核心提交格式</h1>
687696
<p>注意牛客网有两种模式,一种是和 leetcode 一样的提交(无需处理输入),只需要提交核心代码。
688697
一种是 ACM 模式,还需要自己处理输入和输出。
689698
建议使用这种兼容写法,同样的题目可以同时提交到 牛客、leetcode 和 acwing(python3)。

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-20 04:04:25
602+
Build Date UTC : 2022-03-04 10:24:47
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-20</lastmod>
5+
<lastmod>2022-03-04</lastmod>
66
<changefreq>daily</changefreq>
77
</url>
88
<url>
99
<loc>None</loc>
10-
<lastmod>2022-02-20</lastmod>
10+
<lastmod>2022-03-04</lastmod>
1111
<changefreq>daily</changefreq>
1212
</url>
1313
<url>
1414
<loc>None</loc>
15-
<lastmod>2022-02-20</lastmod>
15+
<lastmod>2022-03-04</lastmod>
1616
<changefreq>daily</changefreq>
1717
</url>
1818
<url>
1919
<loc>None</loc>
20-
<lastmod>2022-02-20</lastmod>
20+
<lastmod>2022-03-04</lastmod>
2121
<changefreq>daily</changefreq>
2222
</url>
2323
<url>
2424
<loc>None</loc>
25-
<lastmod>2022-02-20</lastmod>
25+
<lastmod>2022-03-04</lastmod>
2626
<changefreq>daily</changefreq>
2727
</url>
2828
<url>
2929
<loc>None</loc>
30-
<lastmod>2022-02-20</lastmod>
30+
<lastmod>2022-03-04</lastmod>
3131
<changefreq>daily</changefreq>
3232
</url>
3333
<url>
3434
<loc>None</loc>
35-
<lastmod>2022-02-20</lastmod>
35+
<lastmod>2022-03-04</lastmod>
3636
<changefreq>daily</changefreq>
3737
</url>
3838
<url>
3939
<loc>None</loc>
40-
<lastmod>2022-02-20</lastmod>
40+
<lastmod>2022-03-04</lastmod>
4141
<changefreq>daily</changefreq>
4242
</url>
4343
<url>
4444
<loc>None</loc>
45-
<lastmod>2022-02-20</lastmod>
45+
<lastmod>2022-03-04</lastmod>
4646
<changefreq>daily</changefreq>
4747
</url>
4848
<url>
4949
<loc>None</loc>
50-
<lastmod>2022-02-20</lastmod>
50+
<lastmod>2022-03-04</lastmod>
5151
<changefreq>daily</changefreq>
5252
</url>
5353
<url>
5454
<loc>None</loc>
55-
<lastmod>2022-02-20</lastmod>
55+
<lastmod>2022-03-04</lastmod>
5656
<changefreq>daily</changefreq>
5757
</url>
5858
<url>
5959
<loc>None</loc>
60-
<lastmod>2022-02-20</lastmod>
60+
<lastmod>2022-03-04</lastmod>
6161
<changefreq>daily</changefreq>
6262
</url>
6363
<url>
6464
<loc>None</loc>
65-
<lastmod>2022-02-20</lastmod>
65+
<lastmod>2022-03-04</lastmod>
6666
<changefreq>daily</changefreq>
6767
</url>
6868
<url>
6969
<loc>None</loc>
70-
<lastmod>2022-02-20</lastmod>
70+
<lastmod>2022-03-04</lastmod>
7171
<changefreq>daily</changefreq>
7272
</url>
7373
<url>
7474
<loc>None</loc>
75-
<lastmod>2022-02-20</lastmod>
75+
<lastmod>2022-03-04</lastmod>
7676
<changefreq>daily</changefreq>
7777
</url>
7878
<url>
7979
<loc>None</loc>
80-
<lastmod>2022-02-20</lastmod>
80+
<lastmod>2022-03-04</lastmod>
8181
<changefreq>daily</changefreq>
8282
</url>
8383
<url>
8484
<loc>None</loc>
85-
<lastmod>2022-02-20</lastmod>
85+
<lastmod>2022-03-04</lastmod>
8686
<changefreq>daily</changefreq>
8787
</url>
8888
<url>
8989
<loc>None</loc>
90-
<lastmod>2022-02-20</lastmod>
90+
<lastmod>2022-03-04</lastmod>
9191
<changefreq>daily</changefreq>
9292
</url>
9393
<url>
9494
<loc>None</loc>
95-
<lastmod>2022-02-20</lastmod>
95+
<lastmod>2022-03-04</lastmod>
9696
<changefreq>daily</changefreq>
9797
</url>
9898
<url>
9999
<loc>None</loc>
100-
<lastmod>2022-02-20</lastmod>
100+
<lastmod>2022-03-04</lastmod>
101101
<changefreq>daily</changefreq>
102102
</url>
103103
<url>
104104
<loc>None</loc>
105-
<lastmod>2022-02-20</lastmod>
105+
<lastmod>2022-03-04</lastmod>
106106
<changefreq>daily</changefreq>
107107
</url>
108108
<url>
109109
<loc>None</loc>
110-
<lastmod>2022-02-20</lastmod>
110+
<lastmod>2022-03-04</lastmod>
111111
<changefreq>daily</changefreq>
112112
</url>
113113
<url>
114114
<loc>None</loc>
115-
<lastmod>2022-02-20</lastmod>
115+
<lastmod>2022-03-04</lastmod>
116116
<changefreq>daily</changefreq>
117117
</url>
118118
<url>
119119
<loc>None</loc>
120-
<lastmod>2022-02-20</lastmod>
120+
<lastmod>2022-03-04</lastmod>
121121
<changefreq>daily</changefreq>
122122
</url>
123123
</urlset>

sitemap.xml.gz

2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)