当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
中文姓名提取(玩具代码——准头太小,权当玩闹)
Unity SKFramework框架(二十)、VFX Lab 特效库
Jerry's watch delete alarm clock [chapter]
Explanation: here is your UFO, Goldbach conjecture
Web基础
2、 Frame mode MPLS operation
Finally, someone explained the supervised learning clearly
Unity SKFramework框架(十二)、Score 计分模块
Essential for operation and maintenance - Elk log analysis system
随机推荐
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
JS reverse row query data decryption
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
Unity skframework framework (XIII), question module
Post order traversal sequence of 24 binary search tree of sword finger offer
Jerry's weather direction coding table [chapter]
Fundamentals of machine learning (II) -- division of training set and test set
中文姓名提取(玩具代码——准头太小,权当玩闹)
伙伴云表格强势升级!Pro版,更非凡!
Partner cloud form strong upgrade! Pro version, more extraordinary!
Answer: can the audio be set to on by default during easydss video on demand?
2022零代码/低代码开发白皮书【伙伴云出品】附下载
nohup命令
Error function ERF
Unity skframework Framework (XVI), package manager Development Kit Manager
Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
[unity] using GB2312, the solution to abnormal program after packaging
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
Unity SKFramework框架(十五)、Singleton 单例
Everyone wants to eat a broken buffet. It's almost cold