列表

详情


1381. 设计一个支持增量操作的栈

请你设计一个支持下述操作的栈。

实现自定义栈类 CustomStack

 

示例:

输入:
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
输出:
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
解释:
CustomStack customStack = new CustomStack(3); // 栈是空的 []
customStack.push(1);                          // 栈变为 [1]
customStack.push(2);                          // 栈变为 [1, 2]
customStack.pop();                            // 返回 2 --> 返回栈顶值 2,栈变为 [1]
customStack.push(2);                          // 栈变为 [1, 2]
customStack.push(3);                          // 栈变为 [1, 2, 3]
customStack.push(4);                          // 栈仍然是 [1, 2, 3],不能添加其他元素使栈大小变为 4
customStack.increment(5, 100);                // 栈变为 [101, 102, 103]
customStack.increment(2, 100);                // 栈变为 [201, 202, 103]
customStack.pop();                            // 返回 103 --> 返回栈顶值 103,栈变为 [201, 202]
customStack.pop();                            // 返回 202 --> 返回栈顶值 202,栈变为 [201]
customStack.pop();                            // 返回 201 --> 返回栈顶值 201,栈变为 []
customStack.pop();                            // 返回 -1 --> 栈为空,返回 -1

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
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)

上一题