1381。设计一个具有增量操作的堆栈
难度:中等
主题:数组、堆栈、设计
设计一个支持对其元素进行增量操作的堆栈。
实现 CustomStack 类:
示例1:
["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 stk = new CustomStack(3); // Stack is Empty [] stk.push(1); // stack becomes [1] stk.push(2); // stack becomes [1, 2] stk.pop(); // return 2 --> Return top of the stack 2, stack becomes [1] stk.push(2); // stack becomes [1, 2] stk.push(3); // stack becomes [1, 2, 3] stk.push(4); // stack still [1, 2, 3], Do not add another elements as size is 4 stk.increment(5, 100); // stack becomes [101, 102, 103] stk.increment(2, 100); // stack becomes [201, 202, 103] stk.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202] stk.pop(); // return 202 --> Return top of the stack 202, stack becomes [201] stk.pop(); // return 201 --> Return top of the stack 201, stack becomes [] stk.pop(); // return -1 --> Stack is empty return -1.
约束:
提示:
解决方案:
我们可以遵循典型的堆栈实现,但使用额外的方法,允许将底部的 k 个元素增加给定值。增量操作将迭代堆栈的前 k 个元素并将值添加到每个元素。
我们将在 PHP 5.6 中实现这个堆栈,使用数组来表示堆栈。核心操作是:
让我们用 PHP 实现这个解决方案:1381。设计一个具有增量操作的堆栈
<?php class CustomStack { /** * @var array */ private $stack; /** * @var int */ private $maxSize; /** * Constructor to initialize the stack with a given maxSize * * @param Integer $maxSize */ function __construct($maxSize) { ... ... ... /** * go to ./solution.php */ } /** * Push an element to the stack if it has not reached the maxSize * * @param Integer $x * @return NULL */ function push($x) { ... ... ... /** * go to ./solution.php */ } /** * Pop the top element from the stack and return it, return -1 if the stack is empty * * @return Integer */ function pop() { ... ... ... /** * go to ./solution.php */ } /** * Increment the bottom k elements of the stack by val * * @param Integer $k * @param Integer $val * @return NULL */ function increment($k, $val) { ... ... ... /** * go to ./solution.php */ } } /** * Your CustomStack object will be instantiated and called as such: * $obj = CustomStack($maxSize); * $obj->push($x); * $ret_2 = $obj->pop(); * $obj->increment($k, $val); */ // Example usage $customStack = new CustomStack(3); // Stack is Empty [] $customStack->push(1); // stack becomes [1] $customStack->push(2); // stack becomes [1, 2] echo $customStack->pop() . "\n"; // return 2, stack becomes [1] $customStack->push(2); // stack becomes [1, 2] $customStack->push(3); // stack becomes [1, 2, 3] $customStack->push(4); // stack still [1, 2, 3], maxSize is 3 $customStack->increment(5, 100); // stack becomes [101, 102, 103] $customStack->increment(2, 100); // stack becomes [201, 202, 103] echo $customStack->pop() . "\n"; // return 103, stack becomes [201, 202] echo $customStack->pop() . "\n"; // return 202, stack becomes [201] echo $customStack->pop() . "\n"; // return 201, stack becomes [] echo $customStack->pop() . "\n"; // return -1, stack is empty ?>
推($x):
pop():
增量($k, $val):
对于输入操作:
["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]
此输出基于以下内容:
联系链接
如果您发现本系列有帮助,请考虑在 GitHub 上给 存储库 一个星号或在您最喜欢的社交网络上分享该帖子?。您的支持对我来说意义重大!
如果您想要更多类似的有用内容,请随时关注我:
以上是设计一个具有增量操作的堆栈的详细内容。更多信息请关注PHP中文网其他相关文章!