原文链接: https://leetcode-cn.com/problems/sort-of-stacks-lcci 英文原文Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (su…
标签详情
栈 分类
原文链接: https://leetcode-cn.com/problems/stack-of-plates-lcci 英文原文Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshol…
原文链接: https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof 中文题目用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 ) 示例 1: 输入: ["CQueue","appendTail","deleteHead","deleteH…
原文链接: https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof 中文题目输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表长度 <= 10000 通过代码 高赞题解方法一:递归法解题思路: 利用递归: 先走至链表末端,回溯时依次将节点值加入列表 ,这样就可以实现链表值的倒序输出。 Python …
原文链接: https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof 中文题目定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。 示例: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minS…
原文链接: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof 中文题目输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。 为了让您更好地理解问题,以下面的二叉搜索树为例: 我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于双向循环链表,第一个节点的前驱是最后一个节点,最后一个节…
原文链接: https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof 中文题目输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。 示例 1: 输入:pushed = [1,2,3,4,…
原文链接: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof 中文题目输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。 参考以下这颗二叉搜索树: 5 / \ 2 6 / \ 1 3 示例 1: 输入: [1,6,3,2,5] 输出: false 示例 2: 输入: [1,3,2,6,5…
原文链接: https://leetcode-cn.com/problems/sub-sort-lcci 英文原文Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n - m (that is, find the smallest suc…
原文链接: https://leetcode-cn.com/problems/binode-lcci 英文原文The data structure TreeNode is used for binary tree, but it can also used to represent a single linked list (where left is null, and right is the next node in the list). Implement a method to con…
原文链接: https://leetcode-cn.com/problems/calculator-lcci 英文原文Given an arithmetic equation consisting of positive integers, +, -, * and / (no paren­theses), compute the result. The expression string contains only non-negative integers, +, -, *, / op…
原文链接: https://leetcode-cn.com/problems/lMSNwu 中文题目给定两个 非空链表 l1和 l2 来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。 可以假设除了数字 0 之外,这两个数字都不会以零开头。 示例1: 输入:l1 = [7,2,4,3], l2 = [5,6,4] 输出:[7,8,0,7] 示例2: 输入:l1 = [2,4,3], l2 = [5,6,4] 输出:[8,0,7] 示例3…
原文链接: https://leetcode-cn.com/problems/LGjMqU 中文题目给定一个单链表 L 的头节点 head ,单链表 L 表示为: L0 → L1 → … → Ln-1 → Ln 请将其重新排列后变为: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → … 不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示…
原文链接: https://leetcode-cn.com/problems/aMhZSa 中文题目给定一个链表的 头节点 head ,请判断其是否为回文链表。 如果一个链表是回文,那么链表节点序列从前往后看和从后往前看是相同的。 示例 1: 输入: head = [1,2,3,3,2,1] 输出: true 示例 2: 输入: head = [1,2] 输出: false 提示: 链表 L 的长度范围为 [1, 105] 0 <= node.val <= 9 进阶:能否用 O(n) 时间复杂度…
原文链接: https://leetcode-cn.com/problems/8Zf90G 中文题目根据 逆波兰表示法,求该后缀表达式的计算结果。 有效的算符包括 +、-、*、/ 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。 说明: 整数除法只保留整数部分。 给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。 示例 1: 输入:tokens = ["2","1","+","3","*"] 输出:9 解释:该算式转化为常见的中缀算术表达式为:((2 …
原文链接: https://leetcode-cn.com/problems/XagZNi 中文题目给定一个整数数组 asteroids,表示在同一行的小行星。 对于数组中的每一个元素,其绝对值表示小行星的大小,正负表示小行星的移动方向(正表示向右移动,负表示向左移动)。每一颗小行星以相同的速度移动。 找出碰撞后剩下的所有小行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。 示例 1: 输入:asteroids…
原文链接: https://leetcode-cn.com/problems/iIQa4I 中文题目请根据每日 气温 列表 temperatures ,重新生成一个列表,要求其对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。 示例 1: 输入: temperatures = [73,74,75,71,69,72,76,73] 输出: [1,1,4,2,1,1,0,0] 示例 2: 输入: temperatures = [30,40…
原文链接: https://leetcode-cn.com/problems/PLYXKQ 中文题目给定一个由 0 和 1 组成的矩阵 matrix ,找出只包含 1 的最大矩形,并返回其面积。 注意:此题 matrix 输入格式为一维 01 字符串数组。 示例 1: 输入:matrix = ["10100","10111","11111","10010"] 输出:6 解释:最大矩形如上图所示。 示例 2: 输入:matrix = [] 输出:0 示例 3: 输入:matrix = ["0"] …
原文链接: https://leetcode-cn.com/problems/NYBBNL 中文题目给你一棵二叉搜索树,请 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点。 示例 1: 输入:root = [5,3,6,2,4,null,8,1,null,null,null,7,9] 输出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9] 示例 2: …
原文链接: https://leetcode-cn.com/problems/kTOapQ 中文题目实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。 boolean hasNext() 如果向指针右侧遍历存在…
上一页
4 / 4