C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class CustomStack {
public:
CustomStack(int maxSize) {
}
void push(int x) {
}
int pop() {
}
void increment(int k, int val) {
}
};
/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack* obj = new CustomStack(maxSize);
* obj->push(x);
* int param_2 = obj->pop();
* obj->increment(k,val);
*/
运行代码
提交
golang 解法, 执行用时: 24 ms, 内存消耗: 6.8 MB, 提交时间: 2022-11-19 18:25:04
type CustomStack struct {
maxLen int
arr []int
}
func Constructor(maxSize int) CustomStack {
cs := CustomStack{
maxLen: maxSize,
arr: make([]int, 0),
}
return cs
}
func (this *CustomStack) Push(x int) {
if len(this.arr) < this.maxLen {
this.arr = append(this.arr, x)
}
}
func (this *CustomStack) Pop() int {
if len(this.arr) == 0 {
return -1
}
num := this.arr[len(this.arr)-1]
this.arr = this.arr[:len(this.arr)-1]
return num
}
func (this *CustomStack) Increment(k int, val int) {
if len(this.arr) <= k {
for i := 0; i < len(this.arr);i++ {
this.arr[i] += val
}
} else {
for i := 0; i < k;i++ {
this.arr[i] += val
}
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* obj := Constructor(maxSize);
* obj.Push(x);
* param_2 := obj.Pop();
* obj.Increment(k,val);
*/
python3 解法, 执行用时: 72 ms, 内存消耗: 15.9 MB, 提交时间: 2022-11-19 18:24:30
class CustomStack:
def __init__(self, maxSize: int):
self.stk = [0] * maxSize
self.add = [0] * maxSize
self.top = -1
def push(self, x: int) -> None:
if self.top != len(self.stk) - 1:
self.top += 1
self.stk[self.top] = x
def pop(self) -> int:
if self.top == -1:
return -1
ret = self.stk[self.top] + self.add[self.top]
if self.top != 0:
self.add[self.top - 1] += self.add[self.top]
self.add[self.top] = 0
self.top -= 1
return ret
def increment(self, k: int, val: int) -> None:
lim = min(k - 1, self.top)
if lim >= 0:
self.add[lim] += val
# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)
python3 解法, 执行用时: 104 ms, 内存消耗: 15.9 MB, 提交时间: 2022-11-19 18:24:14
class CustomStack:
def __init__(self, maxSize: int):
self.stk = [0] * maxSize
self.top = -1
def push(self, x: int) -> None:
if self.top != len(self.stk) - 1:
self.top += 1
self.stk[self.top] = x
def pop(self) -> int:
if self.top == -1:
return -1
self.top -= 1
return self.stk[self.top + 1]
def increment(self, k: int, val: int) -> None:
lim = min(k, self.top + 1)
for i in range(lim):
self.stk[i] += val
# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)