当前位置:网站首页>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 )
边栏推荐
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
- 分布式块设备复制:客户端
- warning package.json: No license field报错
- MySQL index, transaction and storage engine
- [brother hero June training] day 23: dictionary tree
- Free DIY trip
- Metaaploit post penetration technology knowledge
- phpstudy中Apache无法启动
- Matlab- draw date and duration diagram
- Tensorflow notes - basic functions and concepts
猜你喜欢

Alibaba mailbox web login turn processing

phpstudy中Apache无法启动

A few simple steps to realize the sharing network for industrial raspberry pie

warning package. Json: no license field error

这种动态规划你见过吗——状态机动态规划之股票问题(上)

Solved syntaxerror: (Unicode error) 'Unicode scape' codec can't decode bytes in position 2-3: truncated

PHP generates text and image watermarks

Gamer questions

【Flink】Flink进行Standalone模式的集群搭建

Overview of user space lock on mobile platform
随机推荐
Echats关系图les-miserables的图表详细解析(和弦图)
File upload vulnerability bypass method
[brother hero June training] day 23: dictionary tree
Want to speed up the vit model with one click? Try this open source tool!
招聘顶尖人才!旷视科技“MegEagle创视者计划”正式启动
免费 DIY 之旅问题
简单几步教您实现为工业树莓派共享网络
[brother hero's June training] day 26: check the collection
Codeforces Round #807 (Div 2.) AB
Server access speed
TDengine 商业生态合作伙伴招募开启
一次跨域问题的记录
数据类型与变量
flask_restful中的输出域(Resource、fields、marshal、marshal_with)
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
SQL injection
Key points of ES6 class inheritance
Establishment of NFS server
ECCV 2022 | 同时完成四项跟踪任务!Unicorn: 迈向目标跟踪的大统一


