Skip to content

Commit 1ef1aff

Browse files
committed
✨feat: Add 1380
1 parent 53355cf commit 1ef1aff

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
| [1189. “气球” 的最大数量](https://leetcode-cn.com/problems/maximum-number-of-balloons/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-balloons/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9px4/) | 简单 | 🤩🤩🤩🤩 |
8787
| [1332. 删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-palindromic-subsequences/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-0zwn/) | 中等 | 🤩🤩🤩🤩 |
8888
| [1342. 将数字变成 0 的操作次数](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-85fb/) | 简单 | 🤩🤩🤩🤩 |
89+
| [1380. 矩阵中的幸运数](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9xwg/) | 简单 | 🤩🤩🤩 |
8990
| [1436. 旅行终点站](https://leetcode-cn.com/problems/destination-city/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/destination-city/solution/gong-shui-san-xie-jian-dan-fang-jia-mo-n-y47c/) | 简单 | 🤩🤩🤩🤩🤩 |
9091
| [1446. 连续字符](https://leetcode-cn.com/problems/consecutive-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/consecutive-characters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xtv6/) | 简单 | 🤩🤩🤩🤩🤩 |
9192
| [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1380. 矩阵中的幸运数](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9xwg/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个 $m * n$ 的矩阵,矩阵中的数字 **各不相同** 。请你按 **任意** 顺序返回矩阵中的所有幸运数。
10+
11+
幸运数是指矩阵中满足同时下列两个条件的元素:
12+
13+
* 在同一行的所有元素中最小
14+
* 在同一列的所有元素中最大
15+
16+
示例 1:
17+
```
18+
输入:matrix = [[3,7,8],[9,11,13],[15,16,17]]
19+
20+
输出:[15]
21+
22+
解释:15 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。
23+
```
24+
示例 2:
25+
```
26+
输入:matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
27+
28+
输出:[12]
29+
30+
解释:12 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。
31+
```
32+
示例 3:
33+
```
34+
输入:matrix = [[7,8],[1,2]]
35+
36+
输出:[7]
37+
```
38+
39+
提示:
40+
* $m == mat.length$
41+
* $n == mat[i].length$
42+
* $1 <= n, m <= 50$
43+
* $1 <= matrix[i][j] <= 10^5$
44+
* 矩阵中的所有元素都是不同的
45+
46+
---
47+
48+
### 模拟
49+
50+
根据题意进行模拟即可。
51+
52+
具体的,创建两个数组 `row``col` 用于进行预处理,$row[x]$ 含义为第 $x$ 行的最小值,$col[y]$ 为第 $y$ 列的最大值。
53+
54+
然后扫描棋盘取得符合条件的幸运值即可。
55+
56+
代码:
57+
```Java
58+
class Solution {
59+
int N = 55;
60+
int[] row = new int[N], col = new int[N];
61+
public List<Integer> luckyNumbers (int[][] mat) {
62+
int n = mat.length, m = mat[0].length;
63+
for (int i = 0; i < n; i++) {
64+
row[i] = 100001;
65+
for (int j = 0; j < m; j++) {
66+
row[i] = Math.min(row[i], mat[i][j]);
67+
col[j] = Math.max(col[j], mat[i][j]);
68+
}
69+
}
70+
List<Integer> ans = new ArrayList<>();
71+
for (int i = 0; i < n; i++) {
72+
for (int j = 0; j < m; j++) {
73+
int t = mat[i][j];
74+
if (t == row[i] && t == col[j]) ans.add(t);
75+
}
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
* 时间复杂度:$O(n * m)$
82+
* 空间复杂度:$O(n * m)$
83+
84+
---
85+
86+
### 最后
87+
88+
这是我们「刷穿 LeetCode」系列文章的第 `No.1380` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
89+
90+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
91+
92+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
93+
94+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
95+

0 commit comments

Comments
 (0)