首页 > 编程语言 >#yyds干货盘点# LeetCode程序员面试金典:堆盘子

#yyds干货盘点# LeetCode程序员面试金典:堆盘子

时间:2022-12-16 22:34:53浏览次数:46  
标签:yyds popAt int stackList pop stack 金典 push LeetCode

题目:

堆盘子。设想有一堆盘子,堆太高可能会倒下来。因此,在现实生活中,盘子堆到一定高度时,我们就会另外堆一堆盘子。请实现数据结构SetOfStacks,模拟这种行为。SetOfStacks应该由多个栈组成,并且在前一个栈填满时新建一个栈。此外,SetOfStacks.push()和SetOfStacks.pop()应该与普通栈的操作方法相同(也就是说,pop()返回的值,应该跟只有一个栈时的情况一样)。 进阶:实现一个popAt(int index)方法,根据指定的子栈,执行pop操作。

当某个栈为空时,应当删除该栈。当栈中没有元素或不存在该栈时,pop,popAt 应返回 -1.

示例1:

输入:

["StackOfPlates", "push", "push", "popAt", "pop", "pop"]

[[1], [1], [2], [1], [], []]

输出:

[null, null, null, 2, 1, -1]

示例2:

输入:

["StackOfPlates", "push", "push", "push", "popAt", "popAt", "popAt"]

[[2], [1], [2], [3], [0], [0], [0]]

输出:

[null, null, null, null, 2, 1, 3]

代码实现:

class StackOfPlates {

private List<Stack<Integer>> stackList;
private int cap;

public StackOfPlates(int cap) {
stackList = new ArrayList<>();
this.cap = cap;
}

public void push(int val) {
if (cap <= 0) {
return;
}

if (stackList.isEmpty() || stackList.get(stackList.size() - 1).size() == cap) {
Stack<Integer> stack = new Stack<>();
stack.push(val);
stackList.add(stack);
return;
}

stackList.get(stackList.size() - 1).push(val);
}

public int pop() {
return popAt(stackList.size() - 1);
}

public int popAt(int index) {
if (index < 0 || index >= stackList.size()) {
return -1;
}

Stack<Integer> stack = stackList.get(index);
if (stack.isEmpty()) {
return -1;
}

int res = stack.pop();

if (stack.isEmpty()) {
stackList.remove(index);
}

return res;
}
}

标签:yyds,popAt,int,stackList,pop,stack,金典,push,LeetCode
From: https://blog.51cto.com/u_13321676/5948669

相关文章

  • LEETCODE 222. 完全二叉树的节点个数
    递归递归很简单,遍历整棵树即可,代码复杂度为O(n)点击查看代码funccountNodes(root*TreeNode)int{ifroot==nil{return0}return1+coun......
  • 【LeetCode】题解合集(JavaScript版)
    前言:今年断断续续写了些LeetCode题目,一方面是为了一个比较现实的问题——面试,最重要的是要复习之前学习的数据结构与算法。后面有时间还会接续刷题…题解合集:题号题目题解1......
  • LeetCode 53_最大子数组和
    LeetCode53:最大子数组和题目给你一个整数数组nums,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。子数组是数组中的一个连续部分。示例......
  • [LeetCode] 1785. Minimum Elements to Add to Form a Given Sum
    Youaregivenanintegerarray nums andtwointegers limit and goal.Thearray nums hasaninterestingpropertythat abs(nums[i])<=limit.Return the......
  • #yyds干货盘点#PHP的_initialize() 区别 __construct()
    _initialize()方法是在任何方法执行之前,都要执行的,当然也包括__construct构造函数。也就是说如果存在_initialize()函数,调用对象的任何方法都会导致_initialize()函数的自......
  • [LeetCode]004-寻找两个正序数组的中位数
    >>>传送门题目给定两个大小分别为m和n的正序(从小到大)数组 nums1和 nums2。请你找出并返回这两个正序数组的中位数。算法的时间复杂度应该为O(log(m+n))。......
  • LeetCode HOT 100:旋转图像
    题目:48.旋转图像题目描述:给你一个正方形矩阵数组,将其中的所有元素都顺时针旋转90度,得到旋转之后的矩阵数组。本题要求必须在原地修改,不能使用额外空间。思路:看到这个......
  • LeetCode 题解 2487. 从链表中移除节点
    2487.从链表中移除节点-力扣(Leetcode)题解思路一:递归逆序structListNode*removeNodes(structListNode*head){if(head->next==NULL)//遍历到链表末尾......
  • LeetCode HOT 100:全排列
    题目:46.全排列题目描述:给你一个没有重复元素的数组,返回其所有可能的全排列。全排列是什么呢?举个简单的例子,数组[1,2,3],其中一个排列为[2,1,3],该数组所有的全排列为[......
  • #yyds干货盘点#【愚公系列】2022年12月 微信小程序-图片加载和全屏适配问题
    前言在使用图片问题中可能会遇到各种各样的问题,比如图片加载不出来,图片显示在不同机型效果不同,图片加载展示问题等等。微信小程序image相关属性如下:属性类型默认值......