当前位置:网站首页>1380. Lucky numbers in the matrix
1380. Lucky numbers in the matrix
2022-07-02 23:48:00 【A big pigeon】
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
Explain :
1. Find the smallest element in each row rowmin And the largest element per column colmax
Then traverse matrix, If both rowmin and colmax, Is a lucky number .
class Solution:
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
rows,cols = len(matrix), len(matrix[0])
rowmin = [matrix[i][0] for i in range(rows)]
colmax = [matrix[0][j] for j in range(cols)]
for i in range(rows):
for j in range(cols):
x = matrix[i][j]
if x < rowmin[i] :
rowmin[i] = x
if x > colmax[j]:
colmax[j] = x
#print(rowmin, colmax)
return [matrix[i][j] for i in range(rows) for j in range(cols) if matrix[i][j]==rowmin[i] and matrix[i][j] == colmax[j]]
2. Yes 1 Improvement , Finally, there is no need to traverse matrix, because matrix The elements are different , Only required rowmin and colmax The intersection is just .
class Solution:
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
rows,cols = len(matrix), len(matrix[0])
rowmin = [matrix[i][0] for i in range(rows)]
colmax = [matrix[0][j] for j in range(cols)]
for i in range(rows):
for j in range(cols):
x = matrix[i][j]
if x < rowmin[i] :
rowmin[i] = x
if x > colmax[j]:
colmax[j] = x
#print(rowmin, colmax)
return [x for x in rowmin if x in colmax ]
3. The simple writing of the comment area .
rowmin = [min(i) for i in matrix]
colmax = [max(i) for i in zip(*matrix)]
return [i for i in rowmin if i in colmax]
zip
(*iterables, strict=False) Iterate in parallel over multiple iterators , One data item is returned from each iterator to form a tuple .
“ You might as well know... In another way zip() : It turns rows into columns , Turn columns into rows . This is similar to Matrix transposition ”
* Here is the list unpacking operation .
Use zip and * Disassembly list can be realized .
>>> x = [1, 2, 3] >>> y = [4, 5, 6] >>> list(zip(x, y)) [(1, 4), (2, 5), (3, 6)] >>> x2, y2 = zip(*zip(x, y)) >>> x == list(x2) and y == list(y2) True
4. Other process control tools — Python 3.10.2 file
Unpack :
use *
Operators unpack arguments from lists or tuples
list(range(3, 6)) # normal call with separate arguments args = [3, 6] list(range(*args)) # call with arguments unpacked from a list
*args Will list [3, 6] Unpack as a separate parameter 3 and 6
# If args = [[1,2],[3,4]] Such nested lists ,*args Will unpack [1,2] and [3,4] That is to solve one layer .
** Dictionary unpacking , Solve the dictionary into keyword parameters .
def parrot(voltage, state='a stiff', action='voom'): print("-- This parrot wouldn't", action, end=' ') print("if you put", voltage, "volts through it.", end=' ') print("E's", state, "!") d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"} parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
边栏推荐
- Dishes launcher small green program and directory management (efficiency tool)
- Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
- All things work together, and I will review oceanbase's practice in government and enterprise industry
- Print out mode of go
- Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
- Detailed explanation of 'viewpager' in compose | developer said · dtalk
- Brief introduction to common sense of Zhongtai
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
- 接口自动化覆盖率统计——Jacoco使用
- [OJ] intersection of two arrays (set, hash mapping...)
猜你喜欢
Bean load control
跨境电商如何通过打好数据底座,实现低成本稳步增长
PR FAQ, what about PR preview video card?
Writing of head and bottom components of non routing components
采用VNC Viewer方式远程连接树莓派
接口差异测试——Diffy工具
JDBC practice cases
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
公司里只有一个测试是什么体验?听听他们怎么说吧
随机推荐
Container runtime analysis
leetcode 650. 2 keys keyboard with only two keys (medium)
Bean加载控制
Request and response
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Linux 下安装 redis
Practical series - free commercial video material library
JSON data transfer parameters
RuntimeError: no valid convolution algorithms available in CuDNN
35 pages dangerous chemicals safety management platform solution 2022 Edition
List of major chip Enterprises
简述中台的常识
开发知识点
How to apply for company email when registering in company email format?
可知论与熟能生巧
MFC 获取当前时间
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
leetcode 650. 2 keys keyboard with only two keys (medium)
VIM interval deletion note
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University