当前位置:网站首页>剑指offer专项突击版第14天
剑指offer专项突击版第14天
2022-07-29 17:14:00 【hys__handsome】
简单的队列,我给它加上循环队列,就当成复习
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;
}
};
本来想用前缀和,但是发现 t t t 的范围太大,由于题目保证每次 p i n g ping ping 调用的 t t t 都单调递增,也就是说每个 t t t 只有一个请求,所以只需计算队列长度即可。
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. 往完全二叉树添加节点
通过层序遍历找出所有没有满孩子的结点并按顺序加入 c o n d i d a t e condidate condidate 队列。
在插入结点时取 c o n d i d a t e condidate condidate 队列第一个,即可直接 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;
}
};
边栏推荐
- Tech Talk 活动回顾|基于 Amazon KVS 打造智能视觉产品
- 【高并发】我用多线程优化了亿级流量电商业务下的海量数据校对系统,性能直接提升了200%!!(全程干货,建议收藏)
- How should small and medium-sized financial enterprises carry out disaster recovery construction?
- 生物JC TRIM37防止凝集物组织的异位纺锤体极点的形成,以确保有丝分裂的保真度
- 【南瓜书ML】(task5)支持向量机的数学推导(更新ing)
- Texture 】 【 terrain 】 【 virtual virtual terrain texture technology is introduced
- 58安全-图像质量评价技术实践
- js骏马奔腾点击裁剪js特效
- TensorFlow Serving 高性能的机器学习模型服务系统
- 最大化平均值
猜你喜欢
随机推荐
MUD DAO火爆入世,DAO主轮募集蓄势待发
Tutorial/detailed_workflow. Ipynb quantitative financial Qlib library
【微信小程序】组件使用及属性参考
RocketQA: across batches negative sampling (cross - batch negatives), the denoising of strong negative cases of sampling (denoised hard negative from) and data to enhance (data augment
SR-TE的功能架构概述
ByteArrayOutputStream class source code analysis
【高并发】我用多线程优化了亿级流量电商业务下的海量数据校对系统,性能直接提升了200%!!(全程干货,建议收藏)
【WeChat Mini Program】Zero Basic Learning | Mini Program Grammar
go defer panic recover入门
The two armies clash
清道夫受体-A靶向脂肪酸修饰白蛋白纳米粒/银耳多糖修饰白蛋白微球的制备
4G无线模块 电力通信模块
知识图谱构建全流程
【高并发】我用多线程进一步优化了亿级流量电商业务下的海量数据校对系统,性能再次提升了200%!!(全程干货,建议收藏)
Loadrunner与Jmeter区别与相同
js骏马奔腾点击裁剪js特效
[Network] LAN technology MSTP
query词权重, 搜索词权重计算
大数阶乘计算
Win10 check sha256









