当前位置:网站首页>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 !
边栏推荐
- Difference between NVIDIA n card and amda card
- JDBC教程
- MFC文件操作
- 顶级 DevOps 工具链大盘点
- Use of cocospods
- How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
- (stinger) use pystinger Socks4 to go online and not go out of the network host
- 67 page overall planning and construction plan for a new smart city (download attached)
- Remote connection of raspberry pie by VNC viewer
- @BindsInstance在Dagger2中怎么使用
猜你喜欢

Mapper代理开发

Mapper agent development

内网渗透 | 手把手教你如何进行内网渗透

JDBC tutorial

基于FPGA的VGA协议实现

JDBC練習案例

Intranet penetration | teach you how to conduct intranet penetration hand in hand

PR FAQ, what about PR preview video card?

Optimization of streaming media technology

Fusion de la conversion et de la normalisation des lots
随机推荐
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
接口自动化覆盖率统计——Jacoco使用
2022 latest and complete interview questions for software testing
Difference between NVIDIA n card and amda card
【OJ】两个数组的交集(set、哈希映射 ...)
[Verilog tutorial]
顶级 DevOps 工具链大盘点
How to set automatic reply for mailbox and enterprise mailbox?
MFC gets the current time
Speech recognition Series 1: speech recognition overview
What can I do after buying a domain name?
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
Bean加载控制
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
Yolox enhanced feature extraction network panet analysis
MFC 获取当前时间
"A good programmer is worth five ordinary programmers!"
yolov5test. Py comment
Print out mode of go
Returns the size of the largest binary search subtree in a binary tree