设计数据结构的几道题

This commit is contained in:
游由 2022-08-03 17:25:47 +08:00
parent b102c44cfd
commit fdc76326f3
6 changed files with 252 additions and 7 deletions

View File

@ -1,6 +1,9 @@
mod q0622;
mod q0707;
mod q0208;
mod q0225;
mod q0232;
mod q0297;
mod q0622;
mod q0705;
mod q0706;
mod q0707;
mod q0715;

View File

@ -1,9 +1,9 @@
use std::rc::Rc;
use std::cell::RefCell;
/// 208.实现 Trie (前缀树)
/// [208.实现 Trie (前缀树)](https://leetcode-cn.com/problems/implement-trie-prefix-tree/)
///
/// [原题链接](https://leetcode-cn.com/problems/implement-trie-prefix-tree/)
/// [剑指 Offer II 062. 实现前缀树](https://leetcode.cn/problems/QC3q1f/)
///
/// 2021-04-14 20:20:51
///
@ -29,7 +29,7 @@ use std::cell::RefCell;
/// `[null, null, true, false, true, null, true]`
///
/// 解释
/// ```c++
/// ```java
/// Trie trie = new Trie();
/// trie.insert("apple");
/// trie.search("apple"); // 返回 True

84
rust/src/design/q0225.rs Normal file
View File

@ -0,0 +1,84 @@
/// [225.用队列实现栈](https://leetcode.cn/problems/implement-stack-using-queues/)
///
/// 2022-08-03 16:39:36
///
/// 请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通栈的全部四种操作`push`、`top`、`pop` 和 `empty`)。
///
/// + 实现 `MyStack` 类:
/// * `void push(int x)` 将元素 x 压入栈顶。
/// * `int pop()` 移除并返回栈顶元素。
/// * `int top()` 返回栈顶元素。
/// * `boolean empty()` 如果栈是空的,返回 `true` ;否则,返回 `false` 。
/// + **注意:**
/// * 你只能使用队列的基本操作 —— 也就是 `push to back`、`peek/pop from front`、`size` 和 `is empty` 这些操作。
/// * 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque双端队列来模拟一个队列 , 只要是标准的队列操作即可。
/// + **示例:**
/// + **输入:**
/// - \["MyStack", "push", "push", "top", "pop", "empty"\]
/// - \[\[\], \[1\], \[2\], \[\], \[\], \[\]\]
/// + **输出:**
/// \[null, null, null, 2, 2, false\]
/// + **解释:**
/// ```java
/// MyStack myStack = new MyStack();
/// myStack.push(1);
/// myStack.push(2);
/// myStack.top(); // 返回 2
/// myStack.pop(); // 返回 2
/// myStack.empty(); // 返回 False
/// ```
/// + **提示:**
/// * `1 <= x <= 9`
/// * 最多调用`100` 次 `push`、`pop`、`top` 和 `empty`
/// * 每次调用 `pop` 和 `top` 都保证栈不为空
/// + **进阶:** 你能否仅用一个队列来实现栈。
/// + Related Topics
/// * 栈
/// * 设计
/// * 队列
/// * 👍 555
/// * 👎 0
#[derive(Default)]
struct MyStack {
q: std::collections::vec_deque::VecDeque<i32>,
}
impl MyStack {
fn new() -> Self {
Default::default()
}
fn push(&mut self, x: i32) {
self.q.push_front(x)
}
fn pop(&mut self) -> i32 {
match self.empty() {
true => -1,
_ => self.q.pop_front().unwrap()
}
}
fn top(&self) -> i32 {
match self.empty() {
true => -1,
_ => *self.q.front().unwrap()
}
}
fn empty(&self) -> bool {
self.q.is_empty()
}
}
#[cfg(test)]
mod test {
#[test]
fn test_q0225() {
let mut obj = crate::design::q0225::MyStack::new();
obj.push(1);
assert_eq!(obj.pop(), 1);
assert_eq!(obj.top(), -1);
assert_eq!(obj.empty(), true);
}
}

87
rust/src/design/q0232.rs Normal file
View File

@ -0,0 +1,87 @@
/// [232.用栈实现队列](https://leetcode.cn/problems/implement-queue-using-stacks/)
///
/// 2022-08-03 16:57:56
///
/// 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(`push`、`pop`、`peek`、`empty`
///
/// + 实现 `MyQueue` 类:
/// * `void push(int x)` 将元素 x 推到队列的末尾
/// * `int pop()` 从队列的开头移除并返回元素
/// * `int peek()` 返回队列开头的元素
/// * `boolean empty()` 如果队列为空,返回 `true` ;否则,返回 `false`
/// + **说明:**
/// * 你 **只能** 使用标准的栈操作 —— 也就是只有 `push to top`, `peek/pop from top`, `size`, 和 `is empty` 操作是合法的。
/// * 你所使用的语言也许不支持栈。你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可。
/// + **示例 1**
/// + **输入:**
/// + \["MyQueue", "push", "push", "peek", "pop", "empty"\]
/// + \[\[\], \[1\], \[2\], \[\], \[\], \[\]\]
/// + **输出:**
/// + \[null, null, null, 1, 1, false\]
/// + **解释:**
/// ```java
/// MyQueue myQueue = new MyQueue();
/// myQueue.push(1); // queue is: \[1\]
/// myQueue.push(2); // queue is: \[1, 2\] (leftmost is front of the queue)
/// myQueue.peek(); // return 1
/// myQueue.pop(); // return 1, queue is \[2\]
/// myQueue.empty(); // return false
/// ```
/// + **提示:**
/// * `1 <= x <= 9`
/// * 最多调用 `100` 次 `push`、`pop`、`peek` 和 `empty`
/// * 假设所有操作都是有效的 (例如,一个空的队列不会调用 `pop` 或者 `peek` 操作)
/// + **进阶:**
/// * 你能否实现每个操作均摊时间复杂度为 `O(1)` 的队列?换句话说,执行 `n` 个操作的总时间复杂度为 `O(n)` ,即使其中一个操作可能花费较长时间。
/// + Related Topics
/// * 栈
/// * 设计
/// * 队列
/// * 👍 720
/// * 👎 0
#[derive(Default)]
struct MyQueue {
s: std::collections::VecDeque<i32>,
}
impl MyQueue {
fn new() -> Self {
Default::default()
}
fn push(&mut self, x: i32) {
self.s.push_back(x)
}
fn pop(&mut self) -> i32 {
match self.empty() {
true => -1,
_ => self.s.pop_front().unwrap()
}
}
fn peek(&self) -> i32 {
match self.empty() {
true => -1,
_ => *self.s.front().unwrap()
}
}
fn empty(&self) -> bool {
self.s.is_empty()
}
}
#[cfg(test)]
mod test {
use crate::Solution;
#[test]
fn test_q0232() {
let mut obj = crate::design::q0232::MyQueue::new();
obj.push(1);
assert_eq!(obj.pop(), 1);
assert_eq!(obj.peek(), -1);
assert_eq!(obj.empty(), true);
}
}

View File

@ -6,9 +6,9 @@ use std::convert::TryInto;
use std::iter::Map;
use std::slice::Chunks;
/// 297.二叉树的序列化与反序列化
/// [297.二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/)
///
/// [原题链接](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/)
/// [剑指 Offer II 048. 序列化与反序列化二叉树](https://leetcode.cn/problems/h54YBf/)
///
/// 2021-07-16 11:29:22
///

71
rust/src/design/q0715.rs Normal file
View File

@ -0,0 +1,71 @@
use crate::Solution;
/// [715.Range 模块](https://leetcode-cn.com/problems/range-module/)
///
/// 2022-08-03 16:13:10
///
/// 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)` 中当前正在跟踪的每个实数。
/// + **示例 1**
/// + **输入**
/// - \["RangeModule", "addRange", "removeRange", "queryRange", "queryRange", "queryRange"\]
/// - \[\[\], \[10, 20\], \[14, 16\], \[10, 14\], \[13, 15\], \[16, 17\]\]
/// + **输出**
/// - \[null, null, null, true, false, true\]
/// + **解释**
/// ``` java
/// RangeModule rangeModule = new RangeModule();
/// rangeModule.addRange(10, 20);
/// rangeModule.removeRange(14, 16);
/// rangeModule.queryRange(10, 14); //返回 true (区间 \[10, 14) 中的每个数都正在被跟踪)
/// rangeModule.queryRange(13, 15); //返回 false未跟踪区间 \[13, 15) 中像 14, 14.03, 14.17 这样的数字)
/// rangeModule.queryRange(16, 17); //返回 true (尽管执行了删除操作,区间 \[16, 17) 中的数字 16 仍然会被跟踪)
/// ```
/// + **提示:**
/// * `1 <= left < right <= 109`
/// * 在单个测试用例中,对 `addRange` 、`queryRange` 和 `removeRange` 的调用总数不超过 `104` 次
/// + Related Topics
/// * 设计
/// * 线段树
/// * 有序集合
/// * 👍 193
/// * 👎 0
struct RangeModule {
}
impl RangeModule {
fn new() -> Self {
unimplemented!()
}
fn add_range(&self, left: i32, right: i32) {
unimplemented!()
}
fn query_range(&self, left: i32, right: i32) -> bool {
unimplemented!()
}
fn remove_range(&self, left: i32, right: i32) {
unimplemented!()
}
}
#[cfg(test)]
mod test {
use crate::Solution;
#[test]
fn test_q0715() {
unimplemented!()
}
}