179
179
< li class ="toctree-l2 "> < a href ="#python-int "> python int 值范围</ a > </ li >
180
180
181
181
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 >
183
189
184
190
185
191
< li class ="toctree-l2 "> < a href ="#_2 "> 链表题目调试函数</ a > </ li >
188
194
< li class ="toctree-l2 "> < a href ="#_3 "> 内置库实现优先级队列的三种方式</ a > </ li >
189
195
190
196
191
- < li class ="toctree-l2 "> < a href ="#python_2 "> python 如何实现最大堆</ a > </ li >
197
+ < li class ="toctree-l2 "> < a href ="#python_3 "> python 如何实现最大堆</ a > </ li >
192
198
193
199
194
200
< li class ="toctree-l2 "> < a href ="#lru_cachecache "> lru_cache/cache 优化记忆化搜索</ a > </ li >
197
203
< li class ="toctree-l2 "> < a href ="#leetcode "> leetcode 二叉树调试函数</ a > </ li >
198
204
199
205
200
- < li class ="toctree-l2 "> < a href ="#python_3 "> python 交换列表元素的坑</ a > </ li >
206
+ < li class ="toctree-l2 "> < a href ="#python_4 "> python 交换列表元素的坑(交换副作用) </ a > </ li >
201
207
202
208
203
209
< li class ="toctree-l2 "> < a href ="#acm "> 兼容代码ACM/核心提交格式</ a > </ li >
@@ -341,7 +347,7 @@ <h1 id="python_1">python 递归暴栈(栈溢出)</h1>
341
347
sys.setrecursionlimit(100000) # 设置函数栈深度足够大,避免栈溢出错误
342
348
</ code > </ pre >
343
349
< h1 id ="python-int "> python int 值范围</ h1 >
344
- < pre > < code > # 乘方 (比较推荐,py2/3 都兼容不容易出错)
350
+ < pre > < code > # 乘方 (比较推荐⭐️ ,py2/3 都兼容不容易出错)
345
351
MAXINT = 2**63-1
346
352
MININT = -2**63
347
353
@@ -357,18 +363,23 @@ <h1 id="python-int">python int 值范围</h1>
357
363
MAXINT = (1<<63) - 1
358
364
MININT = ~MAXINT
359
365
</ 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) -> int:
376
+ return bin((A & 0xffffffff) ^ (B & 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
372
383
l = [('a', 1), ('c', 2), ('b',3)]
373
384
sorted(l, key=lambda p:p[0]) # 根据第1个值排序,[('a', 1), ('b', 3), ('c', 2)]
374
385
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>
377
388
l = [1,2,5,4,3]
378
389
maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5
379
390
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 参数)
388
392
from functools import cmp_to_key
389
393
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]
392
396
393
397
# 一行代码判断列表是否有序
394
398
issorted = all(l[i] <= l[i+1] for i in range(len(l) - 1))
395
399
396
400
# python3 一行代码求前缀和
397
401
from itertools import accumulate
398
402
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 顺序
399
429
</ code > </ pre >
400
430
< h1 id ="_2 "> 链表题目调试函数</ h1 >
401
431
< pre > < code class ="language-py "> # 编写链表题目经常用到的一些通用函数和调试函数,定义等,方便代码调试
@@ -512,7 +542,7 @@ <h1 id="_3">内置库实现优先级队列的三种方式</h1>
512
542
while pq:
513
543
print(heapq.heappop(pq))
514
544
</ code > </ pre >
515
- < h1 id ="python_2 "> python 如何实现最大堆</ h1 >
545
+ < h1 id ="python_3 "> python 如何实现最大堆</ h1 >
516
546
< p > python自带了heapq 模块实现了最小堆(min-heaq),但是如果想要实现最大堆(max-heap),有几种实现方式:</ p >
517
547
< ol >
518
548
< li > 对放入的数字取反。比如 10 放入 -10 ,然后取出来的时候再取反。个人倾向于这种,可以自己封装一个类防止来回取反搞晕</ li >
@@ -705,7 +735,7 @@ <h1 id="leetcode">leetcode 二叉树调试函数</h1>
705
735
cur.right = right
706
736
return root
707
737
</ code > </ pre >
708
- < h1 id ="python_3 "> python 交换列表元素的坑</ h1 >
738
+ < h1 id ="python_4 "> python 交换列表元素的坑(交换副作用) </ h1 >
709
739
< pre > < code > # 41. 缺失的第一个正数 https://leetcode-cn.com/problems/first-missing-positive/
710
740
class Solution(object):
711
741
def firstMissingPositive(self, nums):
@@ -725,7 +755,7 @@ <h1 id="python_3">python 交换列表元素的坑</h1>
725
755
while 1 <= nums[i] <= n and nums[nums[i]-1] != nums[i]:
726
756
# NOTE: 注意这一句交换右边有副作用的,不能颠倒!!!
727
757
# 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] # 有副作用的放前边
729
759
for i in range(n):
730
760
if nums[i] != i+1:
731
761
return i+1
0 commit comments