当前位置:网站首页>使用__slots__和__dict__来节省空间(简直就是质的飞越,LeetCode亲测有效)
使用__slots__和__dict__来节省空间(简直就是质的飞越,LeetCode亲测有效)
2022-07-17 04:08:00 【little亮_】
定义
__slots__和__dict__都是两个特殊的类属性。
1.__slots__
__slots__类属性的作用是指定当前类的实例所有包含的所有属性,注意是所有,就是只能是__slots__所指定的属性,不能包含其他以外的属性,也不能新建属性。在运行的时候,它存在的意义就在于告诉解释器:这个类中的所有实例属性都在这儿了!
2.__dict__
__dict__是用来存在实例属性的。就是说,当前实例有什么属性都会存储在这个字典中。__dict__存在提升了属性的访问速度,但是同时也带来了额外的空间消耗,因为__dict__它本质上就是一个字典,而字典是非常消耗空间的。
测试
使用__slots__指定仅仅需要用到的几个属性,如果没有需要用到ed属性,则直接置空,这样在每次创建对象就不会创建额外的属性(包括__dict__属性),从而大大节省了空间。
这题是:LCS 01. 下载插件
解题源码:
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) # 左孩子选择宽带加倍 root.r = Node(None, None, 1, 1) # 右孩子选择直接下载 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
同步更新于个人博客系统:使用__slots__和__dict__来节省空间(简直就是质的飞越,LeetCode亲测有效)
边栏推荐
- If by frame package name modifier
- OSPF anti ring
- Chapter 0 performance platform godeye source code analysis - Course Introduction
- [database] must know at the end of the term ----- Chapter VII database integrity
- [database] must know and know at the end of the period ----- Chapter 12 database recovery
- Cocos creator 3.0 Basics - common operations
- Set administrator permissions for idea and console
- Leetcode7 DFS + dynamic programming + double pointer
- In the era of super video, what is the solution to the data flood?
- ospf防环
猜你喜欢

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(1)开发概要

06 Maui, WPF uses MVVM toolkit framework to build MVVM program

论文研究NLP

Optimization and configuration of OSPF

Wechat e-book reading applet graduation project (6) opening defense ppt

小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT

小程序毕设作品之微信电子书阅读小程序毕业设计(8)毕业设计论文模板
![[database] knowledge and skills at the end of the term ----- Chapter 9 database design](/img/8d/90ccb1eac114706d27fccc1250b58c.png)
[database] knowledge and skills at the end of the term ----- Chapter 9 database design

In tech 2022 | Intel technology product innovation quick view

OSPF路由控制,防环相关知识
随机推荐
OSPF basic optimization
OSPF anti ring
Leetcode7 DFS + dynamic programming + double pointer
Chapter 3 performance platform godeye source code analysis - memory module
[database] must know and be able at the end of the term ----- Chapter 10 database programming
Impersonate the server for requests
Vs Code common shortcut keys
Hello World driver
Eas (energy aware scheduling) green energy-saving scheduler
STM32——定时器系列(二)通用定时器
Wechat online education video on demand learning applet graduation design (1) development outline
若依框架包名修改器
ospf防环
[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?
Simulation Implementation of library function
详解Pod和容器资源管理和分配(CPU和内存分配,临时存储管理)
Cocos creator 3.0 foundation - event system
Avplayer adds playback progress monitoring
How does the enterprise post office set up SPF records?
MAUI 框架入門學習05 MVVM數據模型理解


