当前位置:网站首页>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;
}
}边栏推荐
- 12_Redis_Bitmap_命令
- TiDB 软件和硬件环境建议配置
- Printf function and scanf function in C language
- JVM architecture, classloader, parental delegation mechanism
- HUSTPC2022
- How to avoid 7 common problems in mobile and network availability testing
- Solution of Queen n problem
- 10_Redis_geospatial_命令
- .NET Core 日志系统
- Btrace- (bytecode) dynamic tracking tool
猜你喜欢

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!

16_ Redis_ Redis persistence

N皇后问题的解决

How to avoid 7 common problems in mobile and network availability testing

.NET Core 日志系统

17_ Redis_ Redis publish subscription

Semantic segmentation learning notes (1)

05_ queue

Data analysis thinking analysis methods and business knowledge - business indicators

19_Redis_宕机后手动配置主机
随机推荐
[noi simulation] Elis (greedy, simulation)
Tidb environment and system configuration check
21_ Redis_ Analysis of redis cache penetration and avalanche
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Application of CDN in game field
你不知道的Set集合
Learn the method code example of converting timestamp to uppercase date using PHP
学习使用php实现公历农历转换的方法代码
How to find a sense of career direction
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
[solution] educational codeforces round 82
06_ Stack and queue conversion
Principles, language, compilation, interpretation
List集合&UML图
Markdown tutorial
Solution of Queen n problem
How does the computer set up speakers to play microphone sound
HUSTPC2022
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香