每天一道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\)个你能看到的点,然后将这条线上的点全部标记删除 ...
随机推荐
- Paper | FFDNet: Toward a Fast and Flexible Solution for CNN based Image Denoising
目录 故事背景 核心思想 FFDNet 网络设置 噪声水平图 对子图像的去噪 保证噪声水平图的有效性 如何盲处理 为啥不用短连接 裁剪像素范围 实验 关于噪声水平图的敏感性 盲处理 发表在2018 T ...
- lograotate 配置常用配置
目录 lograotate 配置说明 nginx php-fpm tomcat 配置 各种参数说明 lograotate 配置说明 Linux系统默认安装logrotate工具,它默认的配置文件在: ...
- Java连载8-基本数据类型2
一.基本数据类型 1.字符串“abc”不属于基本数据类型,属于引用数据类型 2. 基本数据类型 占用空间大小(单位:字节) byte 1 short ...
- 可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?
本文由原作者松若章原创发布,作者主页:zhihu.com/people/hrsonion/posts,感谢原作者的无私分享. 1.引言 一道经典的面试题是:从 URL 在浏览器被被输入到页面展现的过程 ...
- 微信小程序跳转页面时参数过长导致参数丢失
问题描述: 微信小程序:跳转页面时传参,参数过长导致参数丢失 跳转到文章详情页时,使用的文章链接e.currentTarget.dataset.id过长导致参数丢失 handleClickArticl ...
- 简明了解apply()和call()
apply()和call()都是ES6语法的,并且都是函数的方法. function foo() { alert(this.name) } var obj = { name: '小明' } foo() ...
- Vue.js 源码分析(十六) 指令篇 v-on指令详解
可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码,例如: <!DOCTYPE html> <html lang="en"& ...
- [ThinkPHP]报错:Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in E:\wamp\www\jdlh\application\index\controller\Index.php on line
错误提示说命名空间声明语句必须是第一句,可我看就是第一句没毛病呀,这是为啥呢,后面发现<?php 前面有个空格,删掉就正常了 去掉空格之后页面能正常显示
- Knative 基本功能深入剖析:Knative Serving 自动扩缩容 Autoscaler
Knative Serving 默认情况下,提供了开箱即用的快速.基于请求的自动扩缩容功能 - Knative Pod Autoscaler(KPA).下面带你体验如何在 Knative 中玩转 Au ...
- JSTL+EL表达式+JSP自定义框架案例
不会框架不要紧,我带你自定义框架 前言:这标题说的有点大了,当一回标题党,之前在学JSP的时候提到了JSTL和EL表达式,由于一直钟情于Servlet,迟迟没有更新别的,这回算是跳出来了.这回放个大招 ...