当前位置:网站首页>Force deduction solution summarizes the lucky numbers in 1380 matrix
Force deduction solution summarizes the lucky numbers in 1380 matrix
2022-07-02 15:26:00 【Lost summer】
Original link : Power button
describe :
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
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Find out the smallest one on the line , List the largest . This car will have two arrays , Repeated in these two arrays , It's a lucky number
Code :
public class Solution1380 {
public List<Integer> luckyNumbers(int[][] matrix) {
Integer[] xInts = new Integer[matrix.length];
Integer[] yInts = new Integer[matrix[0].length];
for (int y = 0; y < matrix.length; y++) {
for (int x = 0; x < matrix[0].length; x++) {
int value = matrix[y][x];
if (xInts[y] == null) {
xInts[y] = value;
} else {
//x Axis
xInts[y] = Math.min(xInts[y], value);
}
if (yInts[x] == null) {
yInts[x] = value;
} else {
//y Axis
yInts[x] = Math.max(yInts[x], value);
}
}
}
List<Integer> result = new ArrayList<>();
Set<Integer> cache = new HashSet<>(Arrays.asList(xInts));
for (Integer i : yInts) {
if (cache.contains(i)) {
result.add(i);
}
}
return result;
}
}边栏推荐
- 19_Redis_宕机后手动配置主机
- How to conduct TPC-C test on tidb
- XML配置文件
- 11_Redis_Hyperloglog_命令
- 06_ Stack and queue conversion
- How to write sensor data into computer database
- Leetcode - Search 2D matrix
- Topology architecture of the minimum deployment of tidb cluster
- MySQL -- Index Optimization -- order by
- Table responsive layout tips
猜你喜欢
随机推荐
数据分析思维分析方法和业务知识——业务指标
语义分割学习笔记(一)
Jenkins Pipeline 应用与实践
JVM architecture, classloader, parental delegation mechanism
03_ Linear table_ Linked list
How to solve the problem of database content output
Record an interview
如何对 TiDB 进行 TPC-C 测试
08_ 串
16_Redis_Redis持久化
TiDB 软件和硬件环境建议配置
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
yolo格式数据集处理(xml转txt)
03_線性錶_鏈錶
04.进入云原生后的企业级应用构建的一些思考
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
AtCoder Beginner Contest 254
How to test tidb with sysbench
Infra11199 database system
12_ Redis_ Bitmap_ command









