2020-ARTS-打卡第六天
Algorithm 题目描述 设计你的循环队列实现,支持入队,出队,获取队首元素,获取队尾元素 题目解答 采用数组可以实现,由于要达到循环效果,主要要考虑队头与队尾的情况 public class MyCircurlarQueue { private int[] queue; private int headIndex; private int count; public MyCircurlarQueue(int capacity) { this.queue = new int[capacity]; } private boolean inQueue(int value) { if (isFull()) { return false; } queue[(headIndex + count) % queue.length] = value; count += 1; return true; } private boolean deQueue() { if (isEmpty()) { return false; } headIndex = (headIndex + 1) % queue.length; count -= 1; return true; } private int First() { if (isEmpty()) { return -1; } return queue[(headIndex + 1) % queue.length]; } private int Rear() { if (isEmpty()) { return -1; } return queue[(headIndex + count - 1) % queue.length]; } private boolean isEmpty() { return count == 0; } private boolean isFull() { return count == queue.length; } } Review code-smells-multi-responsibility-methods本文是Idea作者代码味道系列的第五篇。讲述作者对一个大方法进行重构的过程,主要处理的代码坏味道为多职责方法,在实际开发中随着功能的增加方法会一点点膨胀,这时候就要注意了 ...