每天一道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. Python源码:字典

    一.创建增加修改 1.实现代码 #创建 stu_info = { "xiedi":28, "liuhailin":27,"daiqiao": ...

  2. Note | Ubuntu

    目录 0. 教程 1. 安装 2. 系统 0. 教程 <Linux就该这么学>:https://www.cnblogs.com/RyanXing/p/9462850.html 1. 安装 ...

  3. Sharding-JDBC:查询量大如何优化?

    主人公小王入职了一家刚起步的创业公司,公司正在研发一款App.为了快速开发出能够投入市场进行宣传的版本,小王可是天天加班到很晚,忙了一段时间后终于把第一个版本赶出来了. 初期功能不多,表也不多,用的M ...

  4. 第04组 Alpha冲刺(4/6)

    队名:new game 组长博客:戳 作业博客:戳 组员情况 鲍子涵(队长) 燃尽图 过去两天完成了哪些任务 才两天,也就是实现一些功能而已 复习 接下来的计划 实现更多的功能 为这周的比赛准备 还剩 ...

  5. spring-framework-core-ioc Container 笔记版本

    Spring框架对于java开发人员来说是无比重要的.接触java也有3年了,接触Spring两年了.在工作中天天使用它,平时也会通过视频和书籍尝试更加的了解Spring.对于初学者来说,Spring ...

  6. Shell基本运算符之布尔运算符、逻辑运算符

    Shell基本运算符 =============================摘自与菜鸟教程=============================== 1.布尔运算符 ! 非运算,表达式为tru ...

  7. 『CSP2019初赛后的总结』

    初赛已经过去了,分数大概也已经知道了,接下来的一个月停课应该就是全部准备复赛. 联赛前几次讲课的内容是组合计数,计数\(dp\),字符串,概率期望,数论,数据结构,多数知识点难度都是大于联赛难度的,不 ...

  8. 什么是code-Behind技术?

    code-Behind技术就是代码隐藏(代码后置),在ASP.NET中通过ASPX页面指向CS文件的方法实现显示逻辑和处理逻辑的分离,这样有助于web应用程序的创建. 比如分工,美工和编程的可以个干各 ...

  9. 关于javascript,多种函数封装!!

    1.获取最终的属性 function getStyleAttr(obj, attr){ if(window.getComputedStyle){ return window.getComputedSt ...

  10. opencv::GMM(高斯混合模型)

    GMM方法概述:基于高斯混合模型期望最大化. 高斯混合模型 (GMM) 高斯分布与概率密度分布 - PDF 初始化 初始化EM模型: Ptr<EM> em_model = EM::crea ...