当前位置:网站首页>The 14th day of the special assault version of the sword offer
The 14th day of the special assault version of the sword offer
2022-07-29 18:07:00 【hys__handsome】
简单的队列,I add a circular queue to it,Consider it a rehearsal
class MovingAverage {
private:
int que[1010] = {
0};
int tail = 0,head = 0;
int sz = 0;
int sum = 0;
public:
/** Initialize your data structure here. */
MovingAverage(int size) {
sz = size;
}
double next(int val) {
que[tail] = val;
tail = (tail+1)%1010;
sum += val;
int len = (tail-head+1010)%1010;
if(len > sz) {
sum -= que[head];
head = (head+1)%1010;
len--;
}
return 1.0*sum/len;
}
};
Originally wanted to use prefix sum,但是发现 t t t 的范围太大,Because of the subject guarantee every time p i n g ping ping 调用的 t t t 都单调递增,也就是说每个 t t t 只有一个请求,So just calculate the queue length.
class RecentCounter {
private:
int que[10010] = {
0};
int head, tail;
public:
RecentCounter() {
head = tail = 0;
}
int ping(int t) {
que[tail++] = t;
while(que[head] < t-3000) head++;
return tail-head;
}
};
剑指 Offer II 043. 往完全二叉树添加节点
Find all nodes that do not have full children through layer-order traversal and join them in order c o n d i d a t e condidate condidate 队列.
Taken when inserting a node c o n d i d a t e condidate condidate first in queue,即可直接 O ( 1 ) O(1) O(1) 的时间插入.
class CBTInserter {
private:
queue<TreeNode*> condidate;
TreeNode* root;
public:
CBTInserter(TreeNode* root) {
this->root = root;
queue<TreeNode*> que;
que.push(root);
while(que.size()){
auto ptr = que.front(); que.pop();
if(ptr->left) {
que.push(ptr->left);
}
if(ptr->right) {
que.push(ptr->right);
}
if(!ptr->right || !ptr->left) {
condidate.push(ptr);
}
}
}
int insert(int v) {
auto child = new TreeNode(v);
auto node = condidate.front();
if(!node->left) {
node->left = child;
} else {
node->right = child;
condidate.pop();
}
condidate.push(child);
return node->val;
}
TreeNode* get_root() {
return root;
}
};
边栏推荐
- Interface content 01 document: postman learning route
- 大数阶乘计算
- (notes) Build the was configured to -- Settings repositories over project repositories but solutions
- [网络知识]路由OSPF
- 虚拟偶像的歌声原来是这样生成的!
- 【 Leetcode 】 200. The number of islands (medium)
- Knowledge Graph Construction of Mall Commodities
- 直播实录 | 37 手游如何用 StarRocks 实现用户画像分析
- js骏马奔腾点击裁剪js特效
- 浅谈智能家居应用及传输方式
猜你喜欢
随机推荐
不堆概念、换个角度聊多线程并发编程
Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。
Flutter dynamic | Fair server new version features
ASCII code sorting
InstallAnywhere 2022
IDEA远程调试
解析idea中的debug调试模式
针不戳!腾讯云架构师出品的《MySQL性能优化和高可用架构实践》
Recall i2i
百面量化金融
Lanzhou Mencius Lightweight Pre-training Model Technical Practice
leetcode141 -- 环形链表
Quantitative Finance
「记录」MMDetection入门篇
清道夫受体-A靶向脂肪酸修饰白蛋白纳米粒/银耳多糖修饰白蛋白微球的制备
Six basic experiments of STC8h1k28
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
flink cdc source支持redis 和es吗
零花钱









