当前位置:网站首页>Winter vacation daily question - lucky numbers in the matrix
Winter vacation daily question - lucky numbers in the matrix
2022-07-02 13:32:00 【A cat that doesn't eat mice】
- Topic link :https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/
- Title Description
To give you one m * n Matrix , The number in the matrix Each are not identical . Please press arbitrarily Return all the lucky numbers in the matrix in order .
Lucky number refers to the elements in the matrix that meet the following two conditions at the same time :
The smallest of all elements in the same row
The largest of all elements in the same column
- Example
Input :matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output :[15]
explain :15 Is the only lucky number , Because it is the smallest value in its row , It is also the maximum value in the column .
Ideas
Preprocessing with two arrays , Store the minimum value of each row and the maximum value of each column respectively . Then find the same value in the two arrays .
- Code
public List<Integer> luckyNumbers (int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[] rows = new int[m];
int[] cols = new int[n];
Arrays.fill(rows,Integer.MAX_VALUE);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
rows[i] = Math.min(rows[i],matrix[i][j]);
cols[j] = Math.max(cols[j],matrix[i][j]);
}
}
List<Integer> list = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (cols[j]==rows[i])
list.add(rows[i]);
}
}
return list;
}
边栏推荐
- Solution: Compression Technology (original version and sequel version)
- ADB basic commands
- Unity skframework framework (XXI), texture filter map resource filtering tool
- De4000h storage installation configuration
- 2、 Frame mode MPLS operation
- 文件的下载与图片的预览
- Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
- PR usage skills, how to use PR to watermark?
- Jerry's watch delete alarm clock [chapter]
- Explanation of 34 common terms on the Internet
猜你喜欢
What are eNB, EPC and PGW?
【云原生数据库】遇到慢SQL该怎么办(上)?
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
Ali on three sides, it's really difficult to successfully get the offer rated P7
TVOC, VOC, VOCs gas detection + Solution
Error function ERF
Unity SKFramework框架(十三)、Question 问题模块
net share
记忆函数的性能优化
【OpenGL】笔记二十九、高级光照(镜面高光)
随机推荐
Jerry's watch time synchronization [chapter]
D how to check null
Record idea shortcut keys
机器学习基础(二)——训练集和测试集的划分
EasyDSS点播服务分享时间出错如何修改?
Why is the default of switch followed by break?
Unity skframework framework (XV), singleton singleton
Mysql常用命令详细大全
Finally, someone explained the supervised learning clearly
Unity SKFramework框架(十五)、Singleton 单例
Unity skframework framework (XIV), extension extension function
Error function ERF
Security RememberMe原理分析
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
D如何检查null
ADB basic commands
上海交大教授:何援军——包围盒(包容体/包围盒子)
Jerry's weather code table [chapter]
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...