原文链接: https://leetcode-cn.com/problems/finding-pairs-with-a-certain-sum 英文原文You are given two integer arrays nums1 and nums2. You are tasked to implement a data structure that supports queries of two types: Add a positive integer to an element of a g…
标签详情
设计 分类
原文链接: https://leetcode-cn.com/problems/design-movie-rental-system 英文原文You have a movie renting company consisting of n shops. You want to implement a renting system that supports searching for, booking, and returning movies. The system should also su…
原文链接: https://leetcode-cn.com/problems/operations-on-tree 英文原文You are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array parent where parent[i] is the parent of the ith node. The root of the tree is node 0, so parent[0] …
原文链接: https://leetcode-cn.com/problems/detect-squares 英文原文You are given a stream of points on the X-Y plane. Design an algorithm that: Adds new points from the stream into a data structure. Duplicate points are allowed and should be treated as differ…
原文链接: https://leetcode-cn.com/problems/stock-price-fluctuation 英文原文You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp. Unfortunately due to the volat…
原文链接: https://leetcode-cn.com/problems/walking-robot-simulation-ii 英文原文A width x height grid is on an XY-plane with the bottom-left cell at (0, 0) and the top-right cell at (width - 1, height - 1). The grid is aligned with the four cardinal direction…
原文链接: https://leetcode-cn.com/problems/min-stack-lcci 英文原文How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time. Example: MinStack minS…
原文链接: https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci 英文原文Implement a MyQueue class which implements a queue using two stacks. Example: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // return 1 queue.po…
原文链接: https://leetcode-cn.com/problems/three-in-one-lcci 英文原文Describe how you could use a single array to implement three stacks. You should implement push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum) methods. stackNum is the index…
原文链接: 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/words-frequency-lcci 英文原文Design a method to find the frequency of occurrences of any given word in a book. What if we were running this algorithm multiple times? You should implement following methods: WordsFreq…
原文链接: 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/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/xu-lie-hua-er-cha-shu-lcof 中文题目请实现两个函数,分别用来序列化和反序列化二叉树。 你需要设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。 提示:输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这…
原文链接: https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof 中文题目请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。 若队列为空,pop_front 和 max_value 需要返回 -1 示例 1: 输入: ["MaxQueue","push_back","push_back","max_value","…
原文链接: https://leetcode-cn.com/problems/operations-lcci 英文原文Write methods to implement the multiply, subtract, and divide operations for integers. The results of all of these are integers. Use only the add operator. You should implement following meth…
原文链接: https://leetcode-cn.com/problems/lru-cache-lcci 英文原文Design and build a "least recently used" cache, which evicts the least recently used item. The cache should map from keys to values (allowing you to insert and retrieve a value associ­ated…
原文链接: https://leetcode-cn.com/problems/continuous-median-lcci 英文原文Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated. Median is the middle value in an ordered integ…
原文链接: https://leetcode-cn.com/problems/rank-from-stream-lcci 英文原文Imagine you are reading in a stream of integers. Periodically, you wish to be able to look up the rank of a number x (the number of values less than or equal to x). lmplement the data s…
原文链接: https://leetcode-cn.com/problems/IQvJ9i 英文原文 中文题目秋日市集上有个奇怪的黑盒,黑盒的主视图为 n\*m 的矩形。从黑盒的主视图来看,黑盒的上面和下面各均匀分布有 m 个小孔,黑盒的左面和右面各均匀分布有 n 个小孔。黑盒左上角小孔序号为 0,按顺时针编号,总共有 2*(m+n) 个小孔。每个小孔均可以打开或者关闭,初始时,所有小孔均处于关闭状态。每个小孔上的盖子均为镜面材质。例如一个 2\*3 的黑盒主视图与其小孔分布如图所示: {:he…
原文链接: https://leetcode-cn.com/problems/O4NDxx 中文题目给定一个二维矩阵 matrix,以下类型的多个请求: 计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2) 。 实现 NumMatrix 类: NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化 int sumRegion(int row1, int col1, int row2, int col2…
原文链接: https://leetcode-cn.com/problems/FortPu 中文题目设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构: insert(val):当元素 val 不存在时返回 true ,并向集合中插入该项,否则返回 false 。 remove(val):当元素 val 存在时返回 true ,并从集合中移除该项,否则返回 false 。 getRandom:随机返回现有集合中的一项。每个元素应该有 相同的概率 被返回。 示例 : 输入: in…
原文链接: https://leetcode-cn.com/problems/OrIXps 中文题目 运用所掌握的数据结构,设计和实现一个 LRU (Least Recently Used,最近最少使用) 缓存机制 。 实现 LRUCache 类: LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 void put(int key, in…
原文链接: https://leetcode-cn.com/problems/qIsx9U 中文题目给定一个整数数据流和一个窗口大小,根据该滑动窗口的大小,计算滑动窗口里所有数字的平均值。 实现 MovingAverage 类: MovingAverage(int size) 用窗口大小 size 初始化对象。 double next(int val) 成员函数 next 每次调用的时候都会往滑动窗口增加一个整数,请计算并返回数据流中最后 size 个值的移动平均值,即滑动窗口里所有数字的平均值…
原文链接: https://leetcode-cn.com/problems/H8086Q 中文题目写一个 RecentCounter 类来计算特定时间范围内最近的请求。 请实现 RecentCounter 类: RecentCounter() 初始化计数器,请求数为 0 。 int ping(int t) 在时间 t 添加一个新请求,其中 t 表示以毫秒为单位的某个时间,并返回过去 3000 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 [t-3000, t] 内发生的请求数。 保证…
原文链接: https://leetcode-cn.com/problems/NaqhDT 中文题目完全二叉树是每一层(除最后一层外)都是完全填充(即,节点数达到最大,第 n 层有 2n-1 个节点)的,并且所有的节点都尽可能地集中在左侧。 设计一个用完全二叉树初始化的数据结构 CBTInserter,它支持以下几种操作: CBTInserter(TreeNode root) 使用根节点为 root 的给定树初始化该数据结构; CBTInserter.insert(int v) 向树中插入一个新…
原文链接: https://leetcode-cn.com/problems/h54YBf 中文题目序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。 请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。 示例 1: 输入:root = [1,2,3,…
原文链接: https://leetcode-cn.com/problems/kTOapQ 中文题目实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。 boolean hasNext() 如果向指针右侧遍历存在…
原文链接: https://leetcode-cn.com/problems/jBjn9C 中文题目设计一个找到数据流中第 k 大元素的类(class)。注意是排序后的第 k 大元素,不是第 k 个不同的元素。 请实现 KthLargest 类: KthLargest(int k, int[] nums) 使用整数 k 和整数流 nums 初始化对象。 int add(int val) 将 val 插入数据流 nums 后,返回当前数据流中第 k 大的元素。 示例: 输入: ["KthLarge…