当前位置:网站首页>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;
}
边栏推荐
- MAC (MacOS Monterey 12.2 M1) personal use PHP development
- Everyone wants to eat a broken buffet. It's almost cold
- Mysql常用命令详细大全
- How to get the operating system running PHP- How to get the OS on which PHP is running?
- 解答:EasyDSS视频点播时音频是否可以设置为默认开启?
- OpenFOAM:lduMatrix&lduAddressing
- ADB basic commands
- Unity skframework framework (XIX), POI points of interest / information points
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
猜你喜欢
日本赌国运:Web3.0 ,反正也不是第一次失败了!
2022零代码/低代码开发白皮书【伙伴云出品】附下载
题解:《压缩技术》(原版、续集版)
Fundamentals of face recognition (facenet)
上海交大教授:何援军——包围盒(包容体/包围盒子)
net share
OpenFOAM:lduMatrix&lduAddressing
Student course selection information management system based on ssm+jsp framework [source code + database]
Unity skframework Framework (XVI), package manager Development Kit Manager
Bridge of undirected graph
随机推荐
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
OpenAPI generator: simplify the restful API development process
Jerry's weather code table [chapter]
Error function ERF
2、 Frame mode MPLS operation
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
Explanation of 34 common terms on the Internet
Quantum three body problem: Landau fall
2022 Heilongjiang provincial examination on the writing skills of Application Essays
Ruby: how to copy variables without pointing to the same object- Ruby: how can I copy a variable without pointing to the same object?
日本赌国运:Web3.0 ,反正也不是第一次失败了!
三谈exception——错误处理
TVOC, VOC, VOCs gas detection + Solution
Jerry's watch gets the default ringtone selection list [article]
科技的成就(二十七)
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
研究表明“气味相投”更易成为朋友
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
Jerry's watch ringtone audition [article]