当前位置:网站首页>703. 数据流中的第 K 大元素
703. 数据流中的第 K 大元素
2022-07-01 18:45:00 【Mr Gao】
703. 数据流中的第 K 大元素
设计一个找到数据流中第 k 大元素的类(class)。注意是排序后的第 k 大元素,不是第 k 个不同的元素。
请实现 KthLargest 类:
KthLargest(int k, int[] nums) 使用整数 k 和整数流 nums 初始化对象。
int add(int val) 将 val 插入数据流 nums 后,返回当前数据流中第 k 大的元素。
示例:
输入:
[“KthLargest”, “add”, “add”, “add”, “add”, “add”]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
输出:
[null, 4, 5, 5, 8, 8]
解释:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
typedef struct {
struct KthLargest *next;
int val
} KthLargest;
KthLargest* kthLargestCreate(int k, int* nums, int numsSize) {
KthLargest *l=(KthLargest*)malloc(sizeof(KthLargest));
l->val=k;
l->next=NULL;
int i;
for(i=0;i<numsSize;i++){
KthLargest *p=(KthLargest*)malloc(sizeof(KthLargest));
p->val=nums[i];
KthLargest *pre=l,*s=l->next;
if(i==0){
p->next=l->next;
l->next=p;
}
else{
while(s){
if(s->val>=nums[i]){
break;}
pre=s;
s=s->next;
}
p->next=s;
pre->next=p;
}
}
return l;
}
int kthLargestAdd(KthLargest* obj, int val) {
KthLargest *pre=obj,*s=obj->next;
KthLargest *p=(KthLargest*)malloc(sizeof(KthLargest));
p->val=val;
while(s){
if(s->val>=val){
break;}
pre=s;
s=s->next;
}
p->next=s;
pre->next=p;
int i=1;
s=obj->next;
while(i<=obj->val-2){
s=s->next;
i++;
}
KthLargest *t=s->next;
s->next=t->next;
s=obj->next;
while(s){
printf("%d ",s->val);
s=s->next;
}
printf("|");
return t->val;
}
void kthLargestFree(KthLargest* obj) {
}
/** * Your KthLargest struct will be instantiated and called as such: * KthLargest* obj = kthLargestCreate(k, nums, numsSize); * int param_1 = kthLargestAdd(obj, val); * kthLargestFree(obj); */
边栏推荐
- 研究了11种实时聊天软件,我发现都具备这些功能…
- PMP是被取消了吗??
- Werewolf killing strategy: do you think I'm easy to cheat? Who do we believe!
- 【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
- ddr4测试-2
- pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
- Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
- 精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
- nacos启动失败问题解决与总结
- Superoptimag superconducting magnet system - SOM, Som2 series
猜你喜欢

Lumiprobe cell imaging study PKH26 cell membrane labeling kit

MySQL常用图形管理工具 | 黑马程序员

Lumiprobe free radical analysis h2dcfda instructions

Ubuntu14 install MySQL and configure root account local and remote access

白盒加密技术浅理解

PMP是被取消了吗??

The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site

精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会

CDGA|从事通信行业,那你应该考个数据管理证书

测试自学人必看:软件测试如何找测试项目?
随机推荐
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
测试自学人必看:软件测试如何找测试项目?
线程的并行、并发、生命周期
Nacos configuration file publishing failed, please check whether the parameters are correct solution
web开发常用的开源框架的开源协议整理
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
Helium transmission line of lake shore cryostat
【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
【pytorch记录】自动混合精度训练 torch.cuda.amp
241. Different Ways to Add Parentheses
Lumiprobe phosphide hexaethylene phosphide specification
Contos 7 set up SFTP to create users, user groups, and delete users
Go语言高级
Facebook聊单,SaleSmartly有妙招!
Methods of finding various limits
Implement a Prometheus exporter
SuperVariMag 超导磁体系统 — SVM 系列
Dlib+opencv library for fatigue detection
任务:拒绝服务DoS
ffmpeg 音频相关命令