641. Design Circular Deque

1 题目理解

要求设计一个双端循环队列。这个队列能支持以下操作:
MyCircularDeque(k): Constructor, set the size of the deque to be k.
insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
getRear(): Gets the last item from Deque. If the deque is empty, return -1.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.

2 思路

再次来做的时候完全没有想法,参考了答案后回过头看自己的队列文章发现思路就在文章的循环队列里面讲明白了。
重点理解:
1 front表示第一个数字有效的位置, tail表示最有一个有效数字位置的下一位,也可以理解为要插入一个元素时候的位置。这两个变量的含义不能变。
2 队列为空的条件:front =tail
3 队列满的条件:(tail+1)%capacity=0,是要空出一个位置的。

class MyCircularDeque {private int capacity ;private int[] values;private int front;private int rear;/** Initialize your data structure here. Set the size of the deque to be k. */public MyCircularDeque(int k) {this.values = new int[k+1];this.capacity = k+1;}/** Adds an item at the front of Deque. Return true if the operation is successful. */public boolean insertFront(int value) {if(isFull()) return false;front = front == 0? capacity-1 :(front-1);values[front] = value;return true;}/** Adds an item at the rear of Deque. Return true if the operation is successful. */public boolean insertLast(int value) {if(isFull()) return false;values[rear] = value;rear =  (rear+1)%capacity;return true;}/** Deletes an item from the front of Deque. Return true if the operation is successful. */public boolean deleteFront() {if(isEmpty()) return false;front = (front+1)%capacity;return true;}/** Deletes an item from the rear of Deque. Return true if the operation is successful. */public boolean deleteLast() {if(isEmpty()) return false;rear = rear==0? capacity-1: rear-1;return true;}/** Get the front item from the deque. */public int getFront() {return !isEmpty()?values[front]:-1;}/** Get the last item from the deque. */public int getRear() {return !isEmpty()?values[rear>0?rear-1:capacity-1]:-1;}/** Checks whether the circular deque is empty or not. */public boolean isEmpty() {return front == rear;}/** Checks whether the circular deque is full or not. */public boolean isFull() {return (rear+1)%capacity==front;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/424286.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

QQ技术攻略-原来隐藏着这么多秘密(上)

一、将您的QQ的在线状态发布在互联网上将您的QQ的在线状态发布在互联网上,不用加好友也可以聊天.将您的QQ/TM的在线状态发布在互联网上;点击 QQ在线,不用加好友也可以聊天;寻找商机,广交朋友,"互动状态…

第八十八期:4000万程序员最爱开源项目和编程语言排名出炉!

今天,全球最大开发者社区GitHub重磅发布2019年度报告,透露了一个数据:GitHub目前在全球已有超过4000万开发者用户,其中80%来自美国之外的地区。 作者:小芹、亮亮 全球最大开发者社区GitHub今天重磅发布2019年度报告&…

Java2实用教程(第二版)程序代码——第十四章 Component类的常用方法

1//例子12import java.applet.*;import java.awt.*;3import java.awt.event.*;4import javax.swing.JTextArea;5publicclassExample14_1 extends Applet implements ItemListener6{ List list ; 7 JTextArea text; 8 public void init() 9 { listnew List(6,false…

239. Sliding Window Maximum

文章目录1理解题目2 思路2.1暴力求解2.2双端队列1理解题目 输入:整数数组nums,滑动窗口大小k 输出:整数数组 规则:在一个窗口内只能看到k个数,找一个最大的数,添加到返回数组中。每次滑动向右滑动一步。 …

第八十九期:还在手动盖楼领喵币?双十一这群开发者竟然如此「作弊」

开发者构建了一个脚本以自动逛双十一会场,让使用者轻松完成各种领币任务,同时还能解放双手。 作者:Synced 每年的 11 月份,总觉得有些硝烟弥漫。好在淘宝双十一领喵币,也已经有了自动化脚本。 感觉还未从去年双十一…

Serverless简介

说起当前最火的技术,除了最新的区块链,AI,还有一个不得不提的概念是Serverless。Serverless作为一种新型的互联网架构直接或间接推动了云计算的发展,从AWS Lambda到阿里云函数计算,Serverless一路高歌,同时…

使用matlab工具研究神经网络的简单过程(网络和数据下载)

本人在神经网络研究中是个新手的新手,使用matlab gui工具能够让我们这些小菜也可以研究这些复杂的问题。 在matlab中输入“nntool”,这样就可以出来gui了哈哈。然后按照提示输入:输入数据,目标数据,网络的设置。自然也…

第九十期:哪种人是软件设计中的稀缺型人才?

好的系统架构离不开好的接口设计,因此,真正懂接口设计的人往往是软件设计队伍中的稀缺型人才。 作者:从码农到工匠 好的系统架构离不开好的接口设计,因此,真正懂接口设计的人往往是软件设计队伍中的稀缺型人才。 为什…

151. Reverse Words in a String

1 题目理解 输入:一个字符串s 规则:一个单词是一串非空字符组成的。单词之间用空格分隔。 输出:将字符串按照单词反转字符串。多余的空格只保留一个。 Example 1: Input: s “the sky is blue” Output: “blue is sky the” Example 2: …

C语言输入字符和字符串

C语言有多个函数可以从键盘获得用户输入,它们分别是: scanf():和 printf() 类似,scanf() 可以输入多种类型的数据。getchar()、getche()、getch():这三个函数都用于输入单个字符。gets():获取一行数据&…

收集一些正则表达式

匹配中文字符的正则表达式: [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内):[^\x00-\xff] 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.lenfunction(){return this.re…

第九十一期:架构设计常用到的10种设计模式,你都知道吗?

企业规模的软件系统该如何设计呢?在开始写代码之前,我们需要选择一个合适的架构,这个架构将决定软件实施过程中的功能属性和质量属性。因此,了解软件设计中的不同架构模式对我们的软件设计会有较大的帮助。 作者:abel_…

8. String to Integer (atoi)

1题目理解 输入:一个字符串s,可能包含空格、正负号、数字,还有其他字符。 输出:将字符串转为int 规则:字符串s一开始可能有很多空格,可以忽略这些空格,直到遇到第一个非空字符。从这个字符开始…

程序编码应保持良好的规范(C#)

呵呵,这个简直是超级老生常谈了。但我还是希望能让更多的程序员能了解一些细节习惯对于程序阅读性的影响。而这个很大程度决定了程序的可移植性。1。变量赋值之间注意保留空格。有些程序员往往不注意。不好的: Body.txtVersion.Textib.Version.ToString(…

第九十二期:多少程序员注意到了「中台」的背面?

中台这个词,最近两年特别火,它的爆发源于2015年张勇在阿里发出的内部信中提到的“大中台,小前台”战略。随后吸引了很多人开始“追逐”它。也有很多人开始借着这概念来挣钱。 作者:跨界架构师 这篇文章比较长,有5200…

框架学习 Spring之依赖注入DI

依赖注入的方式有四种: 1、Setter注入(属性注入) 2、构造器注入 3、P命名空间注入 4、集合类型值注入 1、Setter注入(属性注入) Employee 员工实体类 package com.spring.pojo;public class Employee {private Integer…

图:两点之间的最短距离

文章出处:极客时间《数据结构和算法之美》-作者:王争。该系列文章是本人的学习笔记。 1 问题阐述 像 Google 地图、百度地图、高德地图这样的地图软件,如果想从家开车到公司,你只需要输入起始、结束地址,地图就会给你…

spring mvc学习(10):eclipse的环境前maven配置

一.maven的安装 1解压maven压缩包到某一路径下 2配置MAVEN_HOME 3配置path到MAVEN_HOME/bin下 二eclipse集成MAVEN 方法1:直接使用自带插件 1在用户目录/.m2文件夹下面创建setting.xml文件,配置maven仓库位置 2在eclipse中直接配置maven的文件路径 方法2&…

for in / for of 要会用

for in是ES5标准,遍历index ---索引 or key --- 键. 1 for (var index in arr){} // index 0 1 2 3...arr.length-1 1 for (var key in obj){} // key obj里边enumerable的属性(可枚举的属性) for of是ES6标准,遍历value---每…

04级函授计算机等级考试练习.rar

04级函授计算机等级考试练习.rar 以下是计算机基础的练习资料:函授练习.rar posted on 2005-07-16 21:11 麦子 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/maixf/archive/2005/07/16/194254.html