当前位置:网站首页>Use__ slots__ And__ dict__ To save space (it's simply a qualitative leap, and leetcode's personal test is effective)
Use__ slots__ And__ dict__ To save space (it's simply a qualitative leap, and leetcode's personal test is effective)
2022-07-27 10:50:00 【Little light_】
Definition
__slots__ and __dict__ Are two special class attributes .
1.__slots__
__slots__ The function of class attribute is to specify all contained in the instance of the current class all attribute , Note that all , It can only be __slots__ The specified property , Cannot contain other attributes , You cannot create a new attribute . During operation , The meaning of its existence is to tell the interpreter : All instance properties in this class are here !
2.__dict__
__dict__ Is used to exist instance properties . That is to say , Any properties of the current instance will be stored in this dictionary .__dict__ There is an improved access speed of attributes , But it also brings additional space consumption , because __dict__ It is essentially a dictionary , And dictionaries are very space consuming .
test
Use __slots__ Specify only a few attributes that need to be used , If there is no need to use ed attribute , Then directly empty , In this way, no additional attributes will be created every time the object is created ( Include __dict__ attribute ), Thus, the space is greatly saved .
This is the question :LCS 01. Download plug-ins
Source code of problem solving :
from collections import deque class Node: __slots__=('l','r','speed','val') def __init__(self, l=None, r=None, speed=None, val=None): self.l = l self.r = r self.speed = speed self.val = val class Solution: __slots__=() def leastMinutes(self, n: int) -> int: if n==1:return 1 root = Node() root.l = Node(None, None, 2, 0) # Left child chooses broadband double root.r = Node(None, None, 1, 1) # The right child chooses to download directly que = deque() que.append(root.l) que.append(root.r) N = 1 while True: node_tmp = [] flag = 0 while len(que) > 0: cur_node = que.popleft() node_tmp.append(cur_node) new_l_val = cur_node.val if new_l_val >= n: flag = 1 N+=1 break else: new_l_speed = cur_node.speed * 2 cur_node.l = Node(None, None, new_l_speed, new_l_val) new_r_val = cur_node.val + cur_node.speed if new_r_val >= n: flag = 1 N+=1 break else: new_r_speed = cur_node.speed cur_node.r = Node(None, None, new_r_speed, new_r_val) if flag: break else: N += 1 while node_tmp: node = node_tmp.pop() if node.l: que.append(node.l) if node.r: que.append(node.r) return N
Synchronous update in personal blog system : Use __slots__ and __dict__ To save space ( It's a qualitative leap ,LeetCode Close test effectively )
边栏推荐
- [intensive reading of thesis]bert
- How to turn off the application of computer self startup
- Document intelligent multimodal pre training model layoutlmv3: both versatility and superiority
- It is thought-provoking: is syntax really important? Qiu Xipeng group proposed a powerful baseline for aspect based emotional analysis
- [Linux] install redis
- 招聘顶尖人才!旷视科技“MegEagle创视者计划”正式启动
- TDengine 商业生态合作伙伴招募开启
- Detailed analysis of graphs of echats diagram les miserables (chord diagram)
- Want to speed up the vit model with one click? Try this open source tool!
- jvm--字节码浅析
猜你喜欢

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
[intensive reading of thesis]bert

MySQL must know and know!!! Reading this article is enough!!!
![[Flink] Flink builds clusters in standalone mode](/img/5b/e566fdd2792b5cda7d37d308ee32e2.png)
[Flink] Flink builds clusters in standalone mode

Beijing publicized the spot check of 8 batches of children's shoes, and qierte was listed as unqualified

免费 DIY 之旅问题

FTP server

phpstudy中Apache无法启动

Document intelligent multimodal pre training model layoutlmv3: both versatility and superiority

No Identifier specified for entity的解决办法
随机推荐
Distributed block device replication: client
Sorting out some open source projects of speech recognition
想要一键加速ViT模型?试试这个开源工具!
OpenAtom OpenHarmony分论坛,今天14:00见!附大事记精彩发布
Detailed explanation of status code meaning
MySQL master-slave architecture, read-write separation, and high availability architecture
Shardingproxy sub database and table actual combat and comparison of similar products
Matlab draws the system response under different damping
ctf (hardrce)
phpstudy中Apache无法启动
Kgdb debug kernel cannot execute breakpoints and kdb-22:permisson denied
Redis data structure analysis (II)
[brother hero's June training] day 26: check the collection
Eslint的报错信息Module Error (from ./node_modules/[email protected]@eslint-loader/index.js)解决方法
File upload vulnerability bypass method
Key points of ES6 class inheritance
NodeJS中Error: getaddrinfo ENOTFOUND localhost
【Liunx】安装Redis
[brother hero June training] day 28: dynamic planning
Different binary conversion of MATLAB


