当前位置:网站首页>Leetcode 1381. 设计一个支持增量操作的栈
Leetcode 1381. 设计一个支持增量操作的栈
2022-06-27 17:57:00 【我是胖虎啊】
题目名称
1381. 设计一个支持增量操作的栈
题目链接
https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
题目思路
Python3用数组模拟栈操作(先进后出的特点)
code for Python3
class CustomStack:
def __init__(self, maxSize: int):
self.arr = []
self.maxSize = maxSize
def push(self, x: int) -> None:
if len(self.arr) < maxSize:
self.arr.append(x)
def pop(self) -> int:
if self.arr:
return self.arr.pop()
else:
return -1
def increment(self, k: int, val: int) -> None:
if not self.arr:
return
elif len(self.arr) <= k:
for i in range(len(self.arr)):
self.arr[i] += val
else:
for i in range(k):
self.arr[i] += val
复杂度分析
- 时间复杂度: O(N)
- 空间复杂度: O(1)
边栏推荐
- What is ICMP? What is the relationship between Ping and ICMP?
- 使用logrotate对宝塔的网站日志进行自动切割
- One to one relationship
- Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
- Where to look at high-yield bank financial products?
- NVIDIA Clara-AGX-Developer-Kit installation
- labelimg使用指南
- Current market situation and development prospect forecast of the global ductless heating, ventilation and air conditioning system industry in 2022
- 基于STM32F103ZET6库函数外部中断实验
- Error reported by Huada MCU Keil_ Weak's solution
猜你喜欢

过关斩将,擒“指针”(下)

429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题

What is ICMP? What is the relationship between Ping and ICMP?

指针和结构体

如何利用 RPA 实现自动化获客?

Blink SQL内置函数大全

明美新能源冲刺深交所:年应收账款超6亿 拟募资4.5亿

基于STM32F103ZET6库函数蜂鸣器实验
随机推荐
(LC)46. 全排列
图扑数字孪生智慧能源一体化管控平台
一种朴素的消失点计算方法
1029 Median
SQL Server - Window Function - 解决连续N条记录过滤问题
深度学习和神经网络的介绍
基于STM32F103ZET6库函数按键输入实验
Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集
On thread safety
从感知机到前馈神经网络的数学推导
Crawl national laws and Regulations Database
mime.type文件内容
One to one relationship
Erreur Keil de Huada Single Chip Computer La solution de Weak
买股票在券商经理的开户链接上开户安全吗?求大神赐教
A simple calculation method of vanishing point
金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
Seven phases of CMS implementation
Hanoi塔问题