Skip to content

Commit c9600e3

Browse files
committed
Deployed 8fc6d7d with MkDocs version: 1.0.4
1 parent 2f69503 commit c9600e3

File tree

5 files changed

+89
-56
lines changed

5 files changed

+89
-56
lines changed

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

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@
179179
<li class="toctree-l2"><a href="#python-int">python int 值范围</a></li>
180180

181181

182-
<li class="toctree-l2"><a href="#python-listdict">python list/dict 排序等技巧</a></li>
182+
<li class="toctree-l2"><a href="#python_2">python 负数位运算的坑</a></li>
183+
184+
185+
<li class="toctree-l2"><a href="#python-list">python list 技巧</a></li>
186+
187+
188+
<li class="toctree-l2"><a href="#python-dict">python dict 技巧</a></li>
183189

184190

185191
<li class="toctree-l2"><a href="#_2">链表题目调试函数</a></li>
@@ -188,7 +194,7 @@
188194
<li class="toctree-l2"><a href="#_3">内置库实现优先级队列的三种方式</a></li>
189195

190196

191-
<li class="toctree-l2"><a href="#python_2">python 如何实现最大堆</a></li>
197+
<li class="toctree-l2"><a href="#python_3">python 如何实现最大堆</a></li>
192198

193199

194200
<li class="toctree-l2"><a href="#lru_cachecache">lru_cache/cache 优化记忆化搜索</a></li>
@@ -197,7 +203,7 @@
197203
<li class="toctree-l2"><a href="#leetcode">leetcode 二叉树调试函数</a></li>
198204

199205

200-
<li class="toctree-l2"><a href="#python_3">python 交换列表元素的坑</a></li>
206+
<li class="toctree-l2"><a href="#python_4">python 交换列表元素的坑(交换副作用)</a></li>
201207

202208

203209
<li class="toctree-l2"><a href="#acm">兼容代码ACM/核心提交格式</a></li>
@@ -341,7 +347,7 @@ <h1 id="python_1">python 递归暴栈(栈溢出)</h1>
341347
sys.setrecursionlimit(100000) # 设置函数栈深度足够大,避免栈溢出错误
342348
</code></pre>
343349
<h1 id="python-int">python int 值范围</h1>
344-
<pre><code># 乘方 (比较推荐,py2/3 都兼容不容易出错)
350+
<pre><code># 乘方 (比较推荐⭐️,py2/3 都兼容不容易出错)
345351
MAXINT = 2**63-1
346352
MININT = -2**63
347353

@@ -357,18 +363,23 @@ <h1 id="python-int">python int 值范围</h1>
357363
MAXINT = (1&lt;&lt;63) - 1
358364
MININT = ~MAXINT
359365
</code></pre>
360-
<h1 id="python-listdict">python list/dict 排序等技巧</h1>
361-
<pre><code class="language-py"># python 根据 key,value 排序字典
362-
d = {'d': 4, 'a': 1, 'b': 2, 'c':3}
363-
# dict sort by key and reverse
364-
dict(sorted(d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
365-
dict(sorted(d.items(), reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
366-
367-
# dict sort by value and reverse
368-
dict(sorted(d.items(), key = lambda kv:kv[1])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
369-
dict(sorted(d.items(), key = lambda kv:kv[1], reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
370-
371-
# 排序嵌套 list
366+
<h1 id="python_2">python 负数位运算的坑</h1>
367+
<ol>
368+
<li>Python3 中的整型是补码形式存储的</li>
369+
<li>Python3 中 bin 一个负数(十进制表示),输出的是它的原码的二进制表示加上个负号</li>
370+
<li>为了获得负数(十进制表示)的补码,需要手动将其和十六进制数 0xffffffff 进行按位与操作,得到结果是个十六进制数,再交给 bin() 进行输出,
371+
得到的才是你想要的补码表示。</li>
372+
</ol>
373+
<pre><code class="language-py"># 整数转换 https://leetcode-cn.com/problems/convert-integer-lcci/
374+
class Solution:
375+
def convertInteger(self, A: int, B: int) -&gt; int:
376+
return bin((A &amp; 0xffffffff) ^ (B &amp; 0xffffffff)).count('1')
377+
</code></pre>
378+
<p>参考:
379+
- https://www.runoob.com/w3cnote/python-negative-storage.html
380+
- https://leetcode-cn.com/problems/convert-integer-lcci/solution/python3-zhu-yi-qi-dui-yu-fu-shu-de-cun-chu-fang-sh/</p>
381+
<h1 id="python-list">python list 技巧</h1>
382+
<pre><code class="language-py"># 排序嵌套 list
372383
l = [('a', 1), ('c', 2), ('b',3)]
373384
sorted(l, key=lambda p:p[0]) # 根据第1个值排序,[('a', 1), ('b', 3), ('c', 2)]
374385
sorted(l, key=lambda p:p[1]) # 根据第2个值排序,[('a', 1), ('c', 2), ('b', 3)]
@@ -377,25 +388,44 @@ <h1 id="python-listdict">python list/dict 排序等技巧</h1>
377388
l = [1,2,5,4,3]
378389
maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5
379390

380-
# 获取字典对应的最大值对应的 key,value
381-
mydict = {'A':4,'B':10,'C':0,'D':87}
382-
maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum.
383-
maxk, maxv = maximum, mydict[maximum]
384-
# 或者
385-
maxk, maxv = max(mydict.items(), key=lambda k: k[1])
386-
387-
# python3 排序list自定义函数(python2 直接用 cmp 参数)
391+
# python3 排序list自定义函数(python2 直接用 cmp 参数, python3 需要用 cmp_to_key 转成 key 参数)
388392
from functools import cmp_to_key
389393
nums = [3,2,1,4,5]
390-
sorted(nums, key= cmp_to_key(lambda a,b: a-b) ) # [1 ,2 ,3, 4, 5]
391-
sorted(nums, key= cmp_to_key(lambda a,b: b-a) ) # [5, 4, 3, 2, 1]
394+
sorted(nums, key=cmp_to_key(lambda a,b: a-b) ) # [1 ,2 ,3, 4, 5]
395+
sorted(nums, key=cmp_to_key(lambda a,b: b-a) ) # [5, 4, 3, 2, 1]
392396

393397
# 一行代码判断列表是否有序
394398
issorted = all(l[i] &lt;= l[i+1] for i in range(len(l) - 1))
395399

396400
# python3 一行代码求前缀和
397401
from itertools import accumulate
398402
presums = list(accumulate([1,2,3])) # [1, 3, 6]
403+
404+
# 一行代码求矩阵元素总和 https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python
405+
allsum = sum(map(sum, matrix)) # 或者 allsum = sum((sum(row) for row in matrix))
406+
</code></pre>
407+
<h1 id="python-dict">python dict 技巧</h1>
408+
<pre><code class="language-py"># python 根据 key,value 排序字典
409+
d = {'d': 4, 'a': 1, 'b': 2, 'c':3}
410+
# dict sort by **key** and reverse
411+
dict(sorted(d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
412+
dict(sorted(d.items(), reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
413+
414+
# dict sort by **value** and reverse
415+
dict(sorted(d.items(), key = lambda kv:kv[1])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
416+
dict(sorted(d.items(), key = lambda kv:kv[1], reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
417+
418+
# 获取字典对应的最大值对应的 key,value
419+
mydict = {'A':4,'B':10,'C':0,'D':87}
420+
maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum.
421+
maxk, maxv = maximum, mydict[maximum]
422+
# 或者
423+
maxk, maxv = max(mydict.items(), key=lambda k: k[1])
424+
425+
# 支持默认值的有序字典 (OrderedDict and defaultdict)
426+
# https://stackoverflow.com/questions/6190331/how-to-implement-an-ordered-default-dict
427+
od = OrderedDict() # collections.OrderedDict()
428+
od[i] = od.get(i, 0) + 1 # 间接实现了 defaultdict(int) ,同时保持了插入字典的 key 顺序
399429
</code></pre>
400430
<h1 id="_2">链表题目调试函数</h1>
401431
<pre><code class="language-py"># 编写链表题目经常用到的一些通用函数和调试函数,定义等,方便代码调试
@@ -512,7 +542,7 @@ <h1 id="_3">内置库实现优先级队列的三种方式</h1>
512542
while pq:
513543
print(heapq.heappop(pq))
514544
</code></pre>
515-
<h1 id="python_2">python 如何实现最大堆</h1>
545+
<h1 id="python_3">python 如何实现最大堆</h1>
516546
<p>python自带了heapq 模块实现了最小堆(min-heaq),但是如果想要实现最大堆(max-heap),有几种实现方式:</p>
517547
<ol>
518548
<li>对放入的数字取反。比如 10 放入 -10 ,然后取出来的时候再取反。个人倾向于这种,可以自己封装一个类防止来回取反搞晕</li>
@@ -705,7 +735,7 @@ <h1 id="leetcode">leetcode 二叉树调试函数</h1>
705735
cur.right = right
706736
return root
707737
</code></pre>
708-
<h1 id="python_3">python 交换列表元素的坑</h1>
738+
<h1 id="python_4">python 交换列表元素的坑(交换副作用)</h1>
709739
<pre><code># 41. 缺失的第一个正数 https://leetcode-cn.com/problems/first-missing-positive/
710740
class Solution(object):
711741
def firstMissingPositive(self, nums):
@@ -725,7 +755,7 @@ <h1 id="python_3">python 交换列表元素的坑</h1>
725755
while 1 &lt;= nums[i] &lt;= n and nums[nums[i]-1] != nums[i]:
726756
# NOTE: 注意这一句交换右边有副作用的,不能颠倒!!!
727757
# nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i] # 这么写死循环!
728-
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
758+
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1] # 有副作用的放前边
729759
for i in range(n):
730760
if nums[i] != i+1:
731761
return i+1

index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,13 @@ <h2 id="_10">参考教材和链接</h2>
373373
和算法比较熟悉的同学,或者是有其他语言编程经验的同学。本书是英文版,缺点是书中错误真的很多,代码有些无法运行而且不够 Pythonic。该书 <a href="http://bcs.wiley.com/he-bcs/Books?action=resource&amp;bcsId=9003&amp;itemId=0470618299&amp;resourceId=35653">勘误</a></p>
374374
<p><a href="https://book.douban.com/subject/20432061/">《算法导论》第三版</a>: 喜欢数学证明和板砖书的同学可以参考,有很多高级主题。使用伪代码可以很快翻译成 Python</p>
375375
<h2 id="_11">算法可视化</h2>
376-
<p>学习算法的过程中有时候会比较抽象,这里给大家推荐一些可视化的网站,方便更直观地理解各种算法和数据结构的执行步骤:</p>
376+
<p>学习算法的过程中有时候会比较抽象,这里给大家推荐一些可视化的网站,方便更直观地理解各种算法和数据结构的执行步骤:
377+
遇到一个算法或数据结构,你可以 google 搜索 "名称+ visualization" 找到一些可视化网站方便理解,比如学习跳跃表的时候笔者就
378+
可以通过 goole "skip list visualization" 搜到一些可视化网站帮助你理解它的工作原理。</p>
377379
<ul>
378380
<li>https://github.com/algorithm-visualizer/algorithm-visualizer</li>
379381
<li>https://www.cs.usfca.edu/~galles/visualization/Algorithms.html</li>
382+
<li>https://cmps-people.ok.ubc.ca/ylucet/DS/Algorithms.html</li>
380383
<li>https://runestone.academy/runestone/books/published/pythonds/index.html#</li>
381384
</ul>
382385
<h2 id="_12">讲课形式</h2>
@@ -599,5 +602,5 @@ <h2 id="_22">本电子书制作和写作方式</h2>
599602

600603
<!--
601604
MkDocs version : 1.0.4
602-
Build Date UTC : 2022-04-26 01:31:17
605+
Build Date UTC : 2022-04-28 10:22:45
603606
-->

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

sitemap.xml.gz

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)