双端队列

队列空的标识:front == rear
队列满的标识:front + 1 == rear
为了避免队列满 和 队列空的判断冲突,双端队列数据存储 比单队列多一位
typedef struct {
int capity;
int front;
int rear;
int data[];
} MyCircularDeque;

/** Initialize your data structure here. Set the size of the deque to be k. */

MyCircularDeque* myCircularDequeCreate(int k) {
int capity = k + 1;
MyCircularDeque ans = (MyCircularDeque )malloc(sizeof(MyCircularDeque) + capity * sizeof(int));
if (ans == NULL) {
return NULL;
}
ans->front = 0;
ans->rear = 0;
ans->capity = capity;
return ans;
}
/
Checks whether the circular deque is empty or not. /
bool myCircularDequeIsEmpty(MyCircularDeque
obj) {
if (obj->front == obj->rear) {
return true;
}
return false;
}

/** Checks whether the circular deque is full or not. /
bool myCircularDequeIsFull(MyCircularDeque
obj) {
if ((obj->front + 1) % (obj->capity) == obj->rear) {
return true;
}
return false;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. /
bool myCircularDequeInsertFront(MyCircularDeque
obj, int value) {
if (myCircularDequeIsFull(obj)) {
return false;
}
obj->data[obj->front] = value;
obj->front = (obj->front + 1) % obj->capity;
return true;
}

/** Adds an item at the rear of Deque. Return true if the operation is successful. /
bool myCircularDequeInsertLast(MyCircularDeque
obj, int value) {
if (myCircularDequeIsFull(obj)) {
return false;
}
obj->rear = (obj->rear - 1 + obj->capity) % obj->capity;
obj->data[obj->rear] = value;
return true;
}

/** Deletes an item from the front of Deque. Return true if the operation is successful. /
bool myCircularDequeDeleteFront(MyCircularDeque
obj) {
if (myCircularDequeIsEmpty(obj)) {
return false;
}
obj->front = (obj->front - 1 + obj->capity) % obj->capity;
return true;
}

/** Deletes an item from the rear of Deque. Return true if the operation is successful. /
bool myCircularDequeDeleteLast(MyCircularDeque
obj) {
if (myCircularDequeIsEmpty(obj)) {
return false;
}
obj->rear = (obj->rear + 1) % obj->capity;
return true;
}

/** Get the front item from the deque. /
int myCircularDequeGetFront(MyCircularDeque
obj) {
if (myCircularDequeIsEmpty(obj)) {
return -1;
}
int pos = (obj->front - 1 + obj->capity) % obj->capity;
return obj->data[pos];
}

/** Get the last item from the deque. /
int myCircularDequeGetRear(MyCircularDeque
obj) {
if (myCircularDequeIsEmpty(obj)) {
return -1;
}
int pos = obj->rear;
return obj->data[pos];
}

void myCircularDequeFree(MyCircularDeque* obj) {
free(obj);
}

34、在排序数组中查找元素的第一个和最后一个位置
思路 : 二分法查找最左节点和最右节点

79、单词接龙
思路:DFS (回溯) 注意:需要通过状态函数记录当前位置是否访问过。
知识点:
数组指针:二维数组名 每个元素代表一个大小相等的数组,每个元素的大小 即为指向数组的大小。
指针数组:二级指针 每个元素是个指针,每个元素的大小 即为指针的大小,每个指针可以指向不同的大小空间。

395 至少有K个重复字符的最长子串
思路:
1、分演 : 出现次数小于K的字符一定不在最终的字符串中,将出现字符次数小于K的字符置为0,即将字符串进行切割,在子串中继续查找,注意如果字符串中所有字符出现次数都大于等于K则返回字符串长度。注意字符串切割后剩下一个串

5 最长回文子串
思路 : 动态规划
定义p[i][j]用于表示(i, j)之间的字符串 是不是回文子串
1) (i, i)一定是回文子串
2) if (s[i] == s[i+1]), 则(i, i+1)是回文子串
3)如果(i, j - 1)是回文子串,则s[i] == s[j]一定成立, (i + 1, j - 1)一定是回文子串 即只有当(i + 1, j - 1)为回文子串并且s[i] == s[j]的情况下, (i, j)才能是回文子串
4)循环 按照(i,j)之间的 步长来循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值