当前位置:网站首页>[leectode 2022.2.15] lucky numbers in the matrix
[leectode 2022.2.15] lucky numbers in the matrix
2022-07-03 02:44:00 【Procedural ape does not lose hair 2】
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 1:
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 .
Example 2:
Input :matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output :[12]
explain :12 Is the only lucky number , Because it is the smallest value in its row , It is also the maximum value in the column .
Example 3:
Input :matrix = [[7,8],[1,2]]
Output :[7]
Tips :
m == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 10^5
All elements in the matrix are different
java Code :
class Solution {
// Pretreated every 行 Array of minimum values \textit{minRow}minRow And an array of maximum values per column \textit{maxCol}maxCol,
// among \textit{minRow}[i]minRow[i] It means the first one ii 行 The minimum value of ,\
//textit{maxCol}[j]maxCol[j] It means the first one jj The maximum value of the column .
// ergodic matrix \textit{matrix}matrix, If \textit{matrix}[i][j]matrix[i][j] At the same time satisfy \textit{matrix}[i][j]=\textit{minRow}[i]matrix[i][j]=minRow[i] and \textit{matrix}[i][j] = \textit{maxCol}[j]matrix[i][j]=maxCol[j], that \textit{matrix}[i][j]matrix[i][j] Is the lucky number in the matrix , Add the returned results .
public List<Integer> luckyNumbers (int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[] minRow = new int[m];
Arrays.fill(minRow, Integer.MAX_VALUE);
int[] maxCol = new int[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
minRow[i] = Math.min(minRow[i], matrix[i][j]);
maxCol[j] = Math.max(maxCol[j], matrix[i][j]);
}
}
List<Integer> ret = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == minRow[i] && matrix[i][j] == maxCol[j]) {
ret.add(matrix[i][j]);
}
}
}
return ret;
}
}
边栏推荐
- What does it mean when lambda is not entered?
- HW-初始准备
- JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
- [Hcia]No.15 Vlan间通信
- Interview stereotyped version
- 迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
- 【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
- SQL Server Query spécifie la structure de la table
- [translation] flux is safe. Gain more confidence through fuzzy processing
- Process the dataset and use labelencoder to convert all IDs to start from 0
猜你喜欢
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
![[shutter] banner carousel component (shutter_wiper plug-in | swiper component)](/img/a6/5c97ef3f34702b83ebf0511501d757.gif)
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)

【翻译】后台项目加入了CNCF孵化器

What is the way out for children from poor families?

定了,就选它

What does "where 1=1" mean

超好用的日志库 logzero

Kubernetes cluster log and efk architecture log scheme
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)

SQL statement
随机推荐
Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
Gbase 8C system table PG_ cast
Joking about Domain Driven Design (III) -- Dilemma
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
HTB-Devel
搭建私有云盘 cloudreve
C语言中左值和右值的区别
【翻译】后台项目加入了CNCF孵化器
简单理解svg
【翻译】Flux安全。通过模糊处理获得更多信心
Gbase 8C system table PG_ collation
Hcip137-147 title + analysis
How to change the panet layer in yolov5 to bifpn
Informatics Olympiad one general question bank 1006 a+b questions
为什么会选择框架?选择什么样的框架
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Why choose a frame? What frame to choose
cvpr2022去雨去雾
Gbase 8C system table PG_ attribute
Gbase 8C system table PG_ amop