每天一道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\)个你能看到的点,然后将这条线上的点全部标记删除 ...
随机推荐
- SpringCloud微服务常见组件理解
概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓 ...
- 一、Spring注解之@ComponentScan
Spring注解之@ComponentScan [1]@ComponentScan注解是什么 @ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bea ...
- vue项目打包之后样式错乱问题,如何处理
最近公司做的这个项目,要大量修改element里面的css样式,所以项目打包之后 会出现样式和本地开发的时候样式有很多不一样,原因可能是css加载顺序有问题,样式被覆改了. 所以在mian.js里面这 ...
- ThinkPHP查询数据的时候toArray()报错解决办法
当查找不到数据时toArray()会报错,如图 解决办法:先查找数据,然后加个判断,如果有数据再转化为数组,如果没有数据就给个空值,不想代码继续往下执行就return false;
- Linux查找文件夹下包含某字符的所有文件
Linux grep 命令用于查找文件里符合条件的字符串.grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显示 ...
- 特征金字塔网络Feature Pyramid Networks
小目标检测很难,为什么难.想象一下,两幅图片,尺寸一样,都是拍的红绿灯,但是一副图是离得很近的拍的,一幅图是离得很远的拍的,红绿灯在图片里只占了很小的一个角落,即便是对人眼而言,后者图片中的红绿灯也更 ...
- linux 如何指定nologin用户执行命令
在linux中建立网站时,我们一般分配一个www之类的用户给网站应用程序. 如果我们使用root或者具有管理员权限的账号在网站目录下去创建文件时,会遇到各种权限问题. 这时我们可以切换到www用户,这 ...
- annyconnect掉线之后重新链接
sudo service vpnagentd restart /opt/cisco/anyconnect/bin/vpnui 重启服务+重新登录 deepin的优点之一是它的程序不会安装到各个角落里, ...
- java.lang.NoSuchMethodError的通用解决思路
NoSuchMethodError中文意思是没有找到方法,遇到这个错误并不是说依赖的jar包.方法不存在而找不到,这就类似于 ClassNotFoundException错误了,出现ClassNotF ...
- Qt播放音视频文件报错DirectShowPlayerService::doRender: Unresolved error code 0x80040266或DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x80004005 ()
使用QMediaPlayer和QVideoWidget QHBoxLayout *m_layout=newQHBoxLayout(this); QMediaPlayer *m_player = new ...