当前位置:网站首页>【leetcode】1380. Lucky number in matrix
【leetcode】1380. Lucky number in matrix
2022-07-02 21:52:00 【Chinese fir sauce_】
subject :
1380. The lucky number in the matrix
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
violence :
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
List<Integer> list = new ArrayList<>();
for(int i = 0; i < m;i++){
int resMin = Integer.MAX_VALUE;
int idx = 0;
// Traverse each element of the current line to find the minimum value
for(int j = 0; j < n;j++){
resMin = Math.min(resMin,matrix[i][j]);
// Assign the coordinates of the minimum value of the current row to idx
if(resMin == matrix[i][j])idx = j;
}
int resMax = Integer.MIN_VALUE;
// Traverse the current page i The smallest element of the row idx Column by column traversal
for(int j = 0; j < m;j++){
// Roll variables to record
resMax = Math.max(resMax,matrix[j][idx]);
}
// If this value is the minimum value of the current row again idx The maximum value of the column Then he is the lucky number Add it to the result
if(resMax == resMin) list.add(resMin);
}
return list;
}
}
边栏推荐
- sql service 截取字符串
- 《Just because》阅读感受
- How to center the positioned text horizontally and vertically
- About test cases
- [C language] [sword finger offer article] - replace spaces
- : last child does not take effect
- D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
- System (hierarchical) clustering method and SPSS implementation
- 分享一下如何制作专业的手绘电子地图
- Construction and maintenance of business website [2]
猜你喜欢

MySQL learning record (4)

关于测试用例

From "bronze" to "King", there are three secrets of enterprise digitalization

Cardinality sorting (detailed illustration)

LandingSite eBand B1冒烟测试用例

CVPR论文解读 | 弱监督的高保真服饰模特生成

How is LinkedList added?

Baidu sued a company called "Ciba screen"

MySQL learning record (7)

Image segmentation using pixellib
随机推荐
[C language] [sword finger offer article] - replace spaces
sql service 截取字符串
Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
Construction and maintenance of business websites [6]
pyqt圖片解碼 編碼後加載圖片
Evolution of messaging and streaming systems under the native tide of open source cloud
Unity3D学习笔记4——创建Mesh高级接口
Construction and maintenance of business website [2]
Research Report on micro vacuum pump industry - market status analysis and development prospect prediction
[sword finger offer] 56 - I. the number of numbers in the array
LandingSite eBand B1冒烟测试用例
攻防世界pwn题:Recho
Plastic granule Industry Research Report - market status analysis and development prospect forecast
beginning
System (hierarchical) clustering method and SPSS implementation
China plastic bottle and container market trend report, technological innovation and market forecast
From personal heroes to versatile developers, the era of programmer 3.0 is coming
MySQL learning record (1)
*C language final course design * -- address book management system (complete project + source code + detailed notes)
在beforeDestroy中销毁localStorage中的值无效