当前位置:网站首页>378. 有序矩阵中第 K 小的元素-常规法
378. 有序矩阵中第 K 小的元素-常规法
2022-06-21 20:53:00 【Mr Gao】
378. 有序矩阵中第 K 小的元素
给你一个 n x n 矩阵 matrix ,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素。
请注意,它是 排序后 的第 k 小元素,而不是第 k 个 不同 的元素。
你必须找到一个内存复杂度优于 O(n2) 的解决方案。
示例 1:
输入:matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
输出:13
解释:矩阵中的元素为 [1,5,9,10,11,12,13,13,15],第 8 小元素是 13
示例 2:
输入:matrix = [[-5]], k = 1
输出:-5
int cmp(const void *a, const void *b) {
return (*(int *)a - *(int *)b); }
int kthSmallest(int **matrix, int matrixSize, int *matrixColSize, int k) {
int *rec = (int *)malloc(matrixSize * matrixSize * sizeof(int));
int num = 0;
for (int i = 0; i < matrixSize; i++) {
for (int j = 0; j < matrixColSize[i]; j++) {
rec[num++] = matrix[i][j];
}
}
qsort(rec, num, sizeof(int), cmp);
return rec[k - 1];
}
边栏推荐
- Pycharm下可以正常运行,Pyinstaller打包软件报出Fatal error
- Nacos installation guide
- 运维规范:线上故障处理的流程模板
- FPGA设计中RAM和ROM初始化的方法
- Uwp tablet inkcanvas
- 将字符串按长度截取成数组
- Bitmap使用注意事项
- WPF routing
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- Use the for loop to calculate n! Value of
猜你喜欢

STM32 cannot download the program again after downloading it once, and the program cannot run.

Introduction to software architecture

Technology sharing | introduction to kubernetes pod

About LG (n!) Asymptotically compact supremum of

语音信号处理之多阶MFCC提取(matlab)

《MATLAB 神经网络43个案例分析》:第9章 离散Hopfield神经网络的联想记忆——数字识别

Go服务平台项目(一)数据库表的设计与Gendry库的使用

分布式数据库使用逻辑卷管理存储之扩容
![[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor](/img/74/a645742a8e135b32154859be956760.png)
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
![[deeply understand tcapulusdb technology] tmonitor module architecture](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[deeply understand tcapulusdb technology] tmonitor module architecture
随机推荐
[deeply understand tcapulusdb technology] table management of document acceptance
翻译软件Bob安装教程
js实现斐波那契数列
专业字体设计编辑Glyphs 3
Nacos installation guide
An example of CPU instruction rearrangement leading to errors
ES7 create index template
Second understanding microservice
[wustctf2020] plain and unpretentious -1
Pycharm使用指南
About LG (n!) Asymptotically compact supremum of
flutter系列之:flutter中的IndexedStack
Use the for loop to calculate n! Value of
语音信号处理之多阶MFCC提取(matlab)
语音断点检测(短时改进子带谱熵)
2022-06-21:golang选择题,以下golang代码输出什么?A:3;B:4;C:100;D:编译失败。 package main import (
The way of FPGA -- interface level standard between digital systems
Pychart User Guide
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
WPF 启动带参数