当前位置:网站首页>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); */
边栏推荐
- Lake Shore - crx-em-hf low temperature probe station
- Contos 7 set up SFTP to create users, user groups, and delete users
- 奔赴山海之前,毕业季一定要做的那些事情
- sql查询去重统计的方法总结
- indexof和includes的区别
- MySQL common graphics management tools | dark horse programmers
- 从零开始学 MySQL —数据库和数据表操作
- 商业智能BI开发和报表开发有什么本质区别?
- Download (export) PDF template file (such as approval form), and report error: invalid nested tag * * * found, expected closing tag***
- 学习笔记【gumbel softmax】
猜你喜欢

axure不显示元件库

What must be done in graduation season before going to Shanhai

The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables

任务:拒绝服务DoS

nacos配置文件发布失败,请检查参数是否正确的解决方案

Chaos engineering platform chaosblade box new heavy release

SuperVariMag 超导磁体系统 — SVM 系列

Summary of SQL query de duplication statistics methods

Task: denial of service DOS
![Reading the paper [learning to discretely compose reasoning module networks for video captioning]](/img/a2/acdaebeb67ec4bcb01c8ff4bbd1d1e.png)
Reading the paper [learning to discretely compose reasoning module networks for video captioning]
随机推荐
Lake Shore—OptiMag 超导磁体系统 — OM 系列
SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
正则表达式=Regex=regular expression
学习笔记【gumbel softmax】
The use of subplot function in MATLAB
XML语法、约束
CDGA|从事通信行业,那你应该考个数据管理证书
MySQL common graphics management tools | dark horse programmers
Dom4j parsing XML, XPath retrieving XML
Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
What must be done in graduation season before going to Shanhai
web开发常用的开源框架的开源协议整理
狼人杀攻略:你当我好骗吗,我们相信谁!
混沌工程平台 ChaosBlade-Box 新版重磅发布
【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
Facebook聊单,SaleSmartly有妙招!
Detailed explanation of JUnit unit test framework
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
Download (export) PDF template file (such as approval form), and report error: invalid nested tag * * * found, expected closing tag***
Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!