Avatar

Organizations

3 results for 设计
  • Range 模块是跟踪数字范围的模块。设计一个数据结构来跟踪表示为 半开区间的范围并查询它们。

    半开区间 [left, right)  表示所有  left <= x < right  的实数 x

    实现 RangeModule 类:

    • RangeModule()  初始化数据结构的对象。
    • void addRange(int left, int right) 添加 半开区间 [left, right),跟踪该区间中的每个实数。添加与当前跟踪的数字部分重叠的区间时,应当添加在区间  [left, right)  中尚未跟踪的任何数字到该区间中。
    • boolean queryRange(int left, int right)  只有在当前正在跟踪区间  [left, right)  中的每一个实数时,才返回true ,否则返回 false
    • void removeRange(int left, int right)  停止跟踪 半开区间 [left, right)  中当前正在跟踪的每个实数。
    leetcode 困难 设计 线段树 有序集合 Created Mon, 19 Sep 2022 21:11:46 +0800
  • 实现RandomizedSet 类:

    • RandomizedSet() 初始化 RandomizedSet 对象
    • bool insert(int val) 当元素 val 不存在时,向集合中插入该项,并返回 true ;否则,返回 false
    • bool remove(int val) 当元素 val 存在时,从集合中移除该项,并返回 true ;否则,返回 false
    • int getRandom() 随机返回现有集合中的一项(测试用例保证调用此方法时集合中至少存在一个元素)。每个元素应该有 相同的概率 被返回。

    你必须实现类的所有函数,并满足每个函数的 平均 时间复杂度为 O(1)

    leetcode 中等 设计 数组 哈希表 Created Wed, 13 Apr 2022 15:22:28 +0800
  • Given an integer array nums, handle multiple queries of the following types:

    1. Update the value of an element in nums.
    2. Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.

    Implement the NumArray class:

    • NumArray(int[] nums) Initializes the object with the integer array nums.
    • void update(int index, int val) Updates the value of nums[index] to be val.
    • int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).
    leetcode 中等 设计 树状数组 线段树 Created Mon, 04 Apr 2022 12:46:58 +0800