当前位置:网站首页>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); */
边栏推荐
- 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
- 研究了11种实时聊天软件,我发现都具备这些功能…
- Redis 实现限流的三种方式
- Go语言高级
- Task: denial of service DOS
- Methods of finding various limits
- 宝,运维100+服务器很头疼怎么办?用行云管家!
- English语法_形容词/副词3级 -注意事项
- [go ~ 0 to 1] day 5 July 1 type alias, custom type, interface, package and initialization function
- Dom4J解析XML、Xpath检索XML
猜你喜欢
SuperVariMag 超导磁体系统 — SVM 系列
Learn MySQL from scratch - database and data table operations
PMP是被取消了吗??
求各种极限的方法
Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
MATLAB中subplot函数的使用
一次SQL优化,数据库查询速度提升 60 倍
Cdga | if you are engaged in the communication industry, you should get a data management certificate
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
Lumiprobe cell imaging study PKH26 cell membrane labeling kit
随机推荐
水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Introduction to relevant processes and functions of wechat official account development
DTD建模
Chaos engineering platform chaosblade box new heavy release
Go语言高级
【org.slf4j.Logger中info()方法】
Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
PMP是被取消了吗??
Learning notes [Gumbel softmax]
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
Transform + ASM data
Dlib+Opencv库实现疲劳检测
XML语法、约束
Solidity - contract structure - error - ^0.8.4 NEW
241. Different Ways to Add Parentheses
使用环信提供的uni-app Demo,快速实现一对一单聊
Methods of finding various limits
SuperVariMag 超导磁体系统 — SVM 系列