单值输入input()、int(input()
确定值个数:map(int, input().split())
未知个数的一行存于列表 :list(map(int, input().split()) or list(int(i) for i in input().split())
二维数组输入:
n = int(input())
arr = []
for i in range(n) :
arr.append(list(map(int, input().split())))
未声明终止的输入
try: while True: s = input() except: pass'运行
end = "xxxx" :输出字符串以xxxx结尾
print自带换行
“xxx”.join(strlist) : 将字符串列表中字符串之间以"xxx"连接返回新的字符串
format格式输出:{[field_name][!conversion][:format_spec]}
+,- , %占位符类似于c,但要把真实值统一放在%()中
保留小数可以用round函数,round(v, 2)
f-string
f'{field_name [:format_spec]}'format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= digit+
grouping_option ::= "_" | ","
precision ::= digit+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"


初始化、复制与声明全局变量
初始化
[0] * nlist([0] * n for _ in range(m))复制
=都是浅拷贝全局变量:当函数需要改变外部变量的引用时,应该在函数体内将变量声明为global
栈与队列
比较函数cmp和sort、sorted
应用于key参数,使用functools的cmp_to_key方法,cmp函数自定义,当返回正数则交换,负数不交换
字典
get(key, defaultnum)下面假设对nums数组排序, l为左边界, r为右边界
待排序数组为nums, 左边界为l,右边界为r
可利用切片整体赋值
总结:快排属于尾递归,对应于树的遍历属于前序遍历,归并排序是首递归,对应于后序遍历
Python不讲高精度
数值数组A,前缀和数组S,满足如下定义
S[i] = A[0] + A[1] +…+A[i]
应用:求一段数的和
A[i ~ j] = S[j] - S[i - 1]
使得原本O(n)的操作,只需要O(1)
差分是前缀和的逆过程,假设数组A和数组B,
数组B的前i个元素和恰好等于A数组的第i个元素。我们称B数组为A数组的差分数组
应用:可以对一段连续的数进行统一操作
双指针的作用
对暴力遍历的优化
模板
j = 0 或者 右边界
for i in range(len(nums)) :
while (j 满足边界条件 and check(i, j)) : 或者 if。。。
控制边界
题目逻辑
这题是我的老熟人了,在刷代码随想录的时候就遇到了,贪心!贪心!贪心!
这题由于只让求个数所以处理逻辑相对简单一点
queue que #用于宽搜
graph g
hash_map[] #用于存每一步的结果
que.push(root)
while len(que) != 0 :
q = que.pop()
if q exits path : #如果q存在通往下个节点的路
do something #vertical sngx 题目逻辑,状态转移
que.push(q)
hash_map(graph, q) # 记录状态
模拟C语言的
do....while(..)
while True :
do something
if something :
break
可以对列表切片赋值,也可以对列表赋值切片
对于图中方向的模拟可以使用一个DIRC = [[-1, 0],[0, 1], [1, 0], [0, -1]]和模运算来控制方位走向
字符串模拟二维数组n * m,对于字符串中索引x,对应于二维数组是 [x // n, y % m],二维数组中[x, y],对应于字符串索引为x * n + y
字符串不能修改!!!!