每天一道Rust-LeetCode(2019-06-07)
每天一道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)的更多相关文章
- 强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning)
强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning) 学习笔记: Reinforcement Learning: An Introductio ...
- 新手C#构造函数、继承、组合的学习2018.08.06/07
构造函数,是一种特殊的方法.主要用来在创建对象时初始化对象,即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数,可根据其参数个数的不同或参数类型的不同 ...
- MySQL实战 | 06/07 简单说说MySQL中的锁
原文链接:MySQL实战 | 06/07 简单说说MySQL中的锁 本文思维导图:https://mubu.com/doc/AOa-5t-IsG 锁是计算机协调多个进程或纯线程并发访问某一资源的机制. ...
- BlackArch Linux 2019.06.01 宣布发布
导读 BlackArch Linux是一个基于Arch Linux的发行版,专为渗透测试人员和安全研究人员设计,并包含大量渗透测试和安全实用程序,已宣布发布2019.06.01版本. BlackArc ...
- 开机时自动启动的AutoHotkey脚本 2019年07月08日19时06分
;;; 开机时自动启动的AutoHotkey脚本;; 此脚本修改时间 2019年06月18日20时48分;; 计时器创建代码段 ------------------------------------ ...
- 【2019年07月22日】A股最便宜的股票
查看更多A股最便宜的股票:androidinvest.com/CNValueTop/ 便宜指数 = PE + PB + 股息 + ROE,四因子等权,数值越大代表越低估. 本策略只是根据最新的数据来选 ...
- 【2019年07月08日】A股最便宜的股票
查看更多A股最便宜的股票:androidinvest.com/CNValueTop/ 便宜指数 = PE + PB + 股息 + ROE,四因子等权,数值越大代表越低估. 本策略只是根据最新的数据来选 ...
- 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. ...
- 2019.06.17课件:[洛谷P1310]表达式的值 题解
P1310 表达式的值 题目描述 给你一个带括号的布尔表达式,其中+表示或操作|,*表示与操作&,先算*再算+.但是待操作的数字(布尔值)不输入. 求能使最终整个式子的值为0的方案数. 题外话 ...
- 6383. 【NOIP2019模拟2019.10.07】果实摘取
题目 题目大意 给你一个由整点组成的矩形,坐标绝对值范围小于等于\(n\),你在\((0,0)\),一开始面向\((1,0)\),每次转到后面第\(k\)个你能看到的点,然后将这条线上的点全部标记删除 ...
随机推荐
- 小程序-tabBar简易版
<!-- 结构 --> <view class="wrapper"> <block wx:for="{{desc}}"> & ...
- 基于Django的Rest Framework框架的频率组件
0|1一.频率组件的作用 在我们平常浏览网站的时候会发现,一个功能你点击很多次后,系统会让你休息会在点击,这其实就是频率控制,主要作用是限制你在一定时间内提交请求的次数,减少服务器的压力. modle ...
- COMP222 - 2019
COMP222 - 2019 - Second CA AssignmentIndividual courseworkTrain Deep Learning AgentsAssessment Infor ...
- hue框架介绍和安装部署
大家好,我是来自内蒙古的小哥,我现在在北京学习大数据,我想把学到的东西分享给大家,想和大家一起学习 hue框架介绍和安装部署 hue全称:HUE=Hadoop User Experience 他是cl ...
- 一小段 Dapper 的简单示例
关于 Dapper 的介绍,请参考:Dapper - 一款轻量级对象关系映射(ORM)组件,DotNet 下 using System; using System.Collections.Generi ...
- 如何通过 Freemark 优雅地生成那些花里胡哨的复杂样式 Excel 文件?
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- C# 三元表达式
一.背景 因编程的基础差,因此最近开始巩固学习C#基础,后期把自己学习的东西,总结相应文章中,有不足处请大家多多指教. 二.语法 表达式1?表达式2:表达式3 描述: 表达式1一般为一个关系表达式. ...
- js函数定义及一些说明
1.javascript定义函数的三种方法一.function语句//这个方法比较常用function fn(){ alert("这是使用function语句进行函数定义");}f ...
- NumPy 文件数据读写
写数据 NumPy 数组可以使用 np.save 方法保存到本地磁盘中,默认扩展名是 .npy,并且是未压缩的二进制格式. import numpy as np a = np.array([[0, 1 ...
- Python - 字符串 - 第七天
Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hel ...