当前位置:网站首页>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); */
边栏推荐
- XML语法、约束
- 下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
- 241. Different Ways to Add Parentheses
- Learn MySQL from scratch - database and data table operations
- Shell高级进阶
- web开发常用的开源框架的开源协议整理
- uni-app商品分类
- Redis 实现限流的三种方式
- CMU AI PhD 第一年总结
- English grammar_ Adjective / adverb Level 3 - precautions
猜你喜欢

Case sharing: basic networking configuration of QinQ

June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries

Supervarimag superconducting magnet system SVM series

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

Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase

Solidity - contract structure - error - ^0.8.4 NEW

MySQL common graphics management tools | dark horse programmers

XML语法、约束

MATLAB中subplot函数的使用

商业智能BI开发和报表开发有什么本质区别?
随机推荐
web开发常用的开源框架的开源协议整理
Lake Shore 连续流动低温恒温器传输线
M91快速霍尔测量仪—在更短的时间内进行更好的测量
EasyGBS网络不稳定情况下重复请求视频拉流问题的优化
Contos 7 搭建sftp之创建用户、用户组以及删除用户
Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
[English grammar] Unit1 articles, nouns, pronouns and numerals
Is PMP cancelled??
研究了11种实时聊天软件,我发现都具备这些功能…
SuperVariMag 超导磁体系统 — SVM 系列
宝,运维100+服务器很头疼怎么办?用行云管家!
Learn MySQL from scratch - database and data table operations
Supervarimag superconducting magnet system SVM series
Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
Implement a Prometheus exporter
transform + asm资料
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!