学习笔记
leetcode
双端队列
双端队列使用了循环队列。判断满不满的条件是多开一个空间,即判断rear与front的关系。
我们rear指针指向元素的下一个空位置,front指向元素
class MyCircularDeque {
int[] num;
int front;
int rear;
int size;
public MyCircularDeque(int k) {
num = new int[k+1];
size = k+1;
}
public boolean insertFront(int value) {
if(isFull()){
return false;
}
front = (front-1+size)%size;
num[front] = value;
return true;
}
public boolean insertLast(int value) {
if(isFull()){
return false;
}
num[rear] = value;
rear = (rear+1)%size;
return true;
}
public boolean deleteFront() {
if(isEmpty()){
return false;
}
front = (front+1)%size;
return true;
}
public boolean deleteLast() {
if(isEmpty()){
return false;
}
rear = (rear-1+size)%size;
return true;
}
public int getFront() {
if(isEmpty()){
return -1;
}
return num[front];
}
public int getRear() {
if(isEmpty()){
return -1;
}
return num[(rear-1+size)%size];
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return (rear+1)%size==front;
}
}
获取字符串字节数
String str = "test";
str.getBytes().length;