Skip to content

Commit ecd5338

Browse files
author
Ysc
committed
5/28
1 parent 75289ac commit ecd5338

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Easy Leetcode 1380
2+
1517474043
3+
tags: Array
4+
5+
[lucky numvers in a matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/)
6+
7+
方法1:
8+
命令式解题。先找最大值,再找最小值。我的解法
9+
方法2:
10+
通过数学证明,这样的数字之恶能存在一个
11+
12+
13+
14+
/*
15+
方法1
16+
*/
17+
class Solution {
18+
public List<Integer> luckyNumbers (int[][] matrix) {
19+
int m = matrix.length;
20+
List<Integer> result = new LinkedList<>();
21+
for (int i = 0; i < m; i++) {
22+
int[] row = matrix[i];
23+
int index = findMin(row);
24+
boolean flag = true;
25+
for (int j = 0; j < m; j++) {
26+
if(matrix[j][index] > matrix[i][index]) {
27+
flag = false;
28+
break;
29+
}
30+
}
31+
if(flag) {
32+
result.add(matrix[i][index]);
33+
}
34+
35+
}
36+
return result;
37+
}
38+
39+
public static int findMin(int[] x) {
40+
int min = x[0];
41+
int index = 0;
42+
for(int i = 0; i < x.length; i++) {
43+
if(x[i] < min) {
44+
min = x[i];
45+
index = i;
46+
}
47+
}
48+
return index;
49+
}
50+
}
51+
52+
53+
/*
54+
方法2
55+
*/
56+
public List<Integer> luckyNumbers (int[][] matrix) {
57+
Set<Integer> minSet = new HashSet<>(), maxSet = new HashSet<>();
58+
for (int[] row : matrix) {
59+
int mi = row[0];
60+
for (int cell : row)
61+
mi = Math.min(mi, cell);
62+
minSet.add(mi);
63+
}
64+
for (int j = 0; j < matrix[0].length; ++j) {
65+
int mx = matrix[0][j];
66+
for (int i = 0; i < matrix.length; ++i)
67+
mx = Math.max(matrix[i][j], mx);
68+
if (minSet.contains(mx))
69+
maxSet.add(mx);
70+
}
71+
return new ArrayList<>(maxSet);
72+
}
73+

0 commit comments

Comments
 (0)