每天一道Rust-LeetCode(2019-06-07) 622. 设计循环队列

坚持每天一道题,刷题学习Rust.

原题

题目描述

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下操作:

MyCircularQueue(k): 构造器,设置队列长度为 k 。

Front: 从队首获取元素。如果队列为空,返回 -1 。

Rear: 获取队尾元素。如果队列为空,返回 -1 。

enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。

deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。

isEmpty(): 检查循环队列是否为空。

isFull(): 检查循环队列是否已满。

示例:

MyCircularQueue circularQueue = new MycircularQueue(3); // 设置长度为 3

circularQueue.enQueue(1);  // 返回 true

circularQueue.enQueue(2);  // 返回 true

circularQueue.enQueue(3);  // 返回 true

circularQueue.enQueue(4);  // 返回 false,队列已满

circularQueue.Rear();  // 返回 3

circularQueue.isFull();  // 返回 true

circularQueue.deQueue();  // 返回 true

circularQueue.enQueue(4);  // 返回 true

circularQueue.Rear();  // 返回 4

提示:

所有的值都在 0 至 1000 的范围内;

操作数将在 1 至 1000 的范围内;

请不要使用内置的队列库。

解题过程

思路:

这个题比较简单,就是一个链表, 有首有尾

不过但是使用循环队列,我们能使用这些空间去存储新的值。这句话不满足.

如果是较大的对象,应该考虑支持空间反复利用.如果是从这个角度,那么就不应该做成链表,

直接使用Vector最好

struct MyCircularQueue {
v: Vec<i32>,
head: i32, //-1表示没有任何数据
tail: i32, //-1表示没有任何数据,其他情况指向下一个可用下标
} /**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyCircularQueue {
/** Initialize your data structure here. Set the size of the queue to be k. */
fn new(k: i32) -> Self {
MyCircularQueue {
v: vec![0; k as usize],
head: -1,
tail: -1,
}
} /** Insert an element into the circular queue. Return true if the operation is successful. */
fn en_queue(&mut self, value: i32) -> bool {
if self.is_full() {
return false;
}
if self.is_empty() {
self.tail = 1;
self.head = 0;
self.v[0] = value;
} else {
self.v[self.tail as usize] = value;
self.tail += 1;
if self.tail >= self.v.len() as i32 {
self.tail = 0;
}
}
true
} /** Delete an element from the circular queue. Return true if the operation is successful. 从前往后删 */
fn de_queue(&mut self) -> bool {
if self.is_empty() {
return false;
}
self.head += 1;
if self.head >= self.v.len() as i32 {
self.head = 0;
}
if self.tail == self.head {
self.tail = -1;
self.head = -1; //没有数据了,都记为-1
}
true
} /** Get the front item from the queue. */
fn front(&self) -> i32 {
if self.is_empty() {
return -1;
}
return self.v[self.head as usize];
} /** Get the last item from the queue. */
fn rear(&self) -> i32 {
if self.is_empty() {
return -1;
}
let mut l = self.tail - 1;
if l < 0 {
l = (self.v.len() - 1) as i32;
}
self.v[l as usize]
} /** Checks whether the circular queue is empty or not. */
fn is_empty(&self) -> bool {
return self.head == -1 && self.tail == -1;
} /** Checks whether the circular queue is full or not. */
fn is_full(&self) -> bool {
return self.head == self.tail && self.tail != -1;
}
} /**
* Your MyCircularQueue object will be instantiated and called as such:
* let obj = MyCircularQueue::new(k);
* let ret_1: bool = obj.en_queue(value);
* let ret_2: bool = obj.de_queue();
* let ret_3: i32 = obj.front();
* let ret_4: i32 = obj.rear();
* let ret_5: bool = obj.is_empty();
* let ret_6: bool = obj.is_full();
*/ #[cfg(test)]
mod test {
use super::*;
#[test]
fn test_design() {
let mut obj = MyCircularQueue::new(3);
assert_eq!(true, obj.en_queue(1));
assert_eq!(true, obj.en_queue(2));
assert_eq!(true, obj.en_queue(3));
assert_eq!(false, obj.en_queue(4));
assert_eq!(3, obj.rear());
assert_eq!(true, obj.is_full());
assert_eq!(true, obj.de_queue());
assert_eq!(true, obj.en_queue(4));
assert_eq!(4, obj.rear()); // ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","deQueue","deQueue","isEmpty","isEmpty","Rear","Rear","deQueue"]
// [[8],[3],[9],[5],[0],[],[],[],[],[],[],[]]
let mut obj = MyCircularQueue::new(8);
assert_eq!(true, obj.en_queue(3));
assert_eq!(true, obj.en_queue(9));
assert_eq!(true, obj.en_queue(5));
assert_eq!(true, obj.en_queue(0));
assert_eq!(true, obj.de_queue());
assert_eq!(true, obj.de_queue());
assert_eq!(false, obj.is_empty());
assert_eq!(false, obj.is_empty());
assert_eq!(0, obj.rear());
assert_eq!(0, obj.rear());
assert_eq!(true, obj.de_queue());
}
}

一点感悟

原题给的en_queue,de_queue都是&self借用,这个导致无法修改,如果想修改内容只能用RefCell之类的技术.

这个带来不必要的困扰,因此修改了接口.

应该是题目给的接口设计有问题,或者他就是想让用RefCell这类技术.

其他

欢迎关注我的github,本项目文章所有代码都可以找到.

每天一道Rust-LeetCode(2019-06-07)的更多相关文章

  1. 强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning)

    强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning) 学习笔记: Reinforcement Learning: An Introductio ...

  2. 新手C#构造函数、继承、组合的学习2018.08.06/07

    构造函数,是一种特殊的方法.主要用来在创建对象时初始化对象,即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数,可根据其参数个数的不同或参数类型的不同 ...

  3. MySQL实战 | 06/07 简单说说MySQL中的锁

    原文链接:MySQL实战 | 06/07 简单说说MySQL中的锁 本文思维导图:https://mubu.com/doc/AOa-5t-IsG 锁是计算机协调多个进程或纯线程并发访问某一资源的机制. ...

  4. BlackArch Linux 2019.06.01 宣布发布

    导读 BlackArch Linux是一个基于Arch Linux的发行版,专为渗透测试人员和安全研究人员设计,并包含大量渗透测试和安全实用程序,已宣布发布2019.06.01版本. BlackArc ...

  5. 开机时自动启动的AutoHotkey脚本 2019年07月08日19时06分

    ;;; 开机时自动启动的AutoHotkey脚本;; 此脚本修改时间 2019年06月18日20时48分;; 计时器创建代码段 ------------------------------------ ...

  6. 【2019年07月22日】A股最便宜的股票

    查看更多A股最便宜的股票:androidinvest.com/CNValueTop/ 便宜指数 = PE + PB + 股息 + ROE,四因子等权,数值越大代表越低估. 本策略只是根据最新的数据来选 ...

  7. 【2019年07月08日】A股最便宜的股票

    查看更多A股最便宜的股票:androidinvest.com/CNValueTop/ 便宜指数 = PE + PB + 股息 + ROE,四因子等权,数值越大代表越低估. 本策略只是根据最新的数据来选 ...

  8. gitlab4.0->5.0->6.0->7.14->8.0->8.2升级

    参考官方文档: https://gitlab.com/gitlab-org/gitlab-ce/tree/master/doc/update 本地服务器为4.0.1版本  1)4.0.1->4. ...

  9. 2019.06.17课件:[洛谷P1310]表达式的值 题解

    P1310 表达式的值 题目描述 给你一个带括号的布尔表达式,其中+表示或操作|,*表示与操作&,先算*再算+.但是待操作的数字(布尔值)不输入. 求能使最终整个式子的值为0的方案数. 题外话 ...

  10. 6383. 【NOIP2019模拟2019.10.07】果实摘取

    题目 题目大意 给你一个由整点组成的矩形,坐标绝对值范围小于等于\(n\),你在\((0,0)\),一开始面向\((1,0)\),每次转到后面第\(k\)个你能看到的点,然后将这条线上的点全部标记删除 ...

随机推荐

  1. hdu6470 矩阵快速幂+构造矩阵

    http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意 \(f[n]=2f[n-2]+f[n-1]+n^3,n \leq 10^{18}\),求f[n] 题 ...

  2. (四十一)golang--goroutine

    首先得了解: 进程 线程 并发 并行 Go协程和Go主线程: 主线程:相当于进程:直接作用于cpu上,是重量级的,是物理态的: 协程:相当于轻量级的线程:由主协程开启,是逻辑态的: Go协程的特点: ...

  3. CodeForce 192D Demonstration

    In the capital city of Berland, Bertown, demonstrations are against the recent election of the King ...

  4. 我在生产项目里是如何使用Redis发布订阅的?(一)使用场景

    转载请注明出处! 导语 Redis是我们很常用的一款nosql数据库产品,我们通常会用Redis来配合关系型数据库一起使用,弥补关系型数据库的不足. 其中,Redis的发布订阅功能也是它的一大亮点.虽 ...

  5. 数据持久化之Data Volume

    废话不多说直接操作 1.启动一个MySQL测试容器 [root@localhost labs]# docker pull mysql #下载MySQL镜像 [root@localhost labs]# ...

  6. Kubernetes 动态PV使用

    Kubernetes 动态PV使用 Kubernetes支持动态供给的存储插件:https://kubernetes.io/docs/concepts/storage/storage-classes/ ...

  7. c# 移除类中所有事件的绑定

    单例中为防止多处注册事件引起异步触发时发生报错,网上找了一圈没找到想要的方法. [异常类型]:ArgumentException[异常信息]:该委托必须有一个目标(且仅有一个目标). 结合网上资料整合 ...

  8. Subversion——密码保存位置

    Subversion——密码保存位置 摘要:本文主要说明了Subversion在电脑上保存密码的位置. 起因 在向本地电脑上的文件夹里下载程序代码的时候,发现输入了地址之后就能直接下载了,并没有提示输 ...

  9. Windows中将nginx添加到服务(转)

    下载安装nginx http://nginx.org/en/download.html 下载后解压到C盘 C:\nginx-1.14.0 添加服务 需要借助"Windows Service ...

  10. git合并单个节点

    有两个分支 # git branch -a * branchA branchB A分支合并B分支单个节点 # git log commit 6b4f9e1e1a1e1ed3e7ca3a1f15ce1f ...