Python
2Ululu
这个作者很懒,什么都没留下…
展开
-
python3记忆化的方法
Leetcode python记忆化原创 2022-07-03 19:01:45 · 329 阅读 · 1 评论 -
区间操作的python3板子 LeetCode 715. Range 模块
注意事项:区间都是左闭又开[x,y)from sortedcontainers import SortedListclass RangeModule: def __init__(self): self.rl=SortedList() self.count=0 def addRange(self, left: int, right: int) -> None: #[left,right) right-=1原创 2022-05-16 16:34:19 · 290 阅读 · 0 评论 -
Leetcode 433. 最小基因变化 「BFS」&「双向 BFS」
BFSclass Solution: def minMutation(self, start: str, end: str, bank: List[str]) -> int: s,bank=set(),set(bank) if start==end:return 0 q=deque() q.append((start,0)) s.add(start) while q:原创 2022-05-07 11:33:03 · 320 阅读 · 0 评论 -
pytorch固定网络参数
在微调的时候,特征提取层可能已经收敛不再需要backward更新,这个时候我们需要去固定它的参数,具体有两种方法:1.requires_grad_(False),具体代码如下:import torchdef init_weights(m): if type(m) == torch.nn.Linear : m.weight.data=torch.ones_like(m.weight) m.bias.data = torch.ones_like(m.bias)原创 2021-10-22 14:44:22 · 2837 阅读 · 0 评论 -
python3中实现自定义类的排序(不使用cmp_to_key)
不使用functools.cmp_to_key,重写自定义类的<或者>运算符1.重写 '<'符号class node: def __init__(self,s,a): self.s=s self.a=a def __lt__(self, other): if self.s==other.s: return self.a<other.a else:原创 2021-04-13 16:10:37 · 358 阅读 · 0 评论 -
LeetCode 179. 最大数 python3解法(自定义比较实例)
题目链接:https://leetcode-cn.com/problems/largest-number/cmp想要实现的目的都是从大到小降序排列注意:C++和python的逻辑是相反的python中:s1>s2 返回的是-1s1<s2 返回的是1C++中:s1>s2 返回的是trueelse 返回的是falsepython代码:def cmp(a,b): s1=str(a)+str(b) s2=str(b)+...原创 2021-04-12 12:31:27 · 244 阅读 · 0 评论 -
python3中自定义排序 和优先队列
functools模板中的方法:import functoolsdef cmp(x,y): if x<y: return -1 elif x>y: return 1 else: return 0def mycmp(self,other): if self.x==other.x: ...原创 2019-04-02 17:02:07 · 2254 阅读 · 0 评论 -
leetcode 矩阵注水 python
用list模拟一个队列就行代码如下:class Solution: def waterInjection(self, matrix, R, C):# # Write your code here n=len(matrix) dx=[-1,0,0,1] dy=[0,1,-1,0] flag=0 ...原创 2019-04-02 16:27:10 · 255 阅读 · 0 评论 -
lintcode 丑数 python
暴力如下:class Solution: """ @param n: An integer @return: return a integer as description. """ def istrue(self,x): a=[i for i in range(3)] a[0],a[1],a[2]=2,3,5 ...原创 2019-04-01 19:30:18 · 133 阅读 · 0 评论 -
python3 sort 去除了cmp怎么自定义排序(一个降序,一个升序)
开始我学习的是C/C++到了python3突然发现结构体排序竟然没有了cmp我的妈耶!!!我的内心是崩溃的。。。。其实我去网上也看了很多关于sort,sorted的讲解,讲解的都是比较好的,多个关键字排序可以用到 operator.itemgetter或者operator.attrgetter 需要引入operator模板operator.itemgetter传入的参数是数字...原创 2018-12-22 16:22:54 · 4667 阅读 · 2 评论