当前位置:网站首页>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 !
边栏推荐
- [error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
- Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
- Matlab 信号处理【问答笔记-1】
- A single element in an ordered array -- Valentine's Day mental problems
- What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
- Load balancing cluster (LBC)
- Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
- [Verilog tutorial]
- PHP get real IP
- @BindsInstance在Dagger2中怎么使用
猜你喜欢
内网渗透 | 手把手教你如何进行内网渗透
Writing of head and bottom components of non routing components
Request and response
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
How does win11 turn on visual control? Win11 method of turning on visual control
Bean load control
Golang common settings - modify background
Print out mode of go
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
采用VNC Viewer方式远程连接树莓派
随机推荐
基于FPGA的VGA协议实现
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
内网渗透 | 手把手教你如何进行内网渗透
How does win11 turn on visual control? Win11 method of turning on visual control
[array] binary search
[proteus simulation] 51 MCU +lcd12864 push box game
Convolution和Batch normalization的融合
基于Pyqt5工具栏按钮可实现界面切换-1
开发知识点
2022 latest and complete interview questions for software testing
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
Why can't the start method be called repeatedly? But the run method can?
YOLOX加强特征提取网络Panet分析
返回二叉树两个节点间的最大距离
Returns the size of the largest binary search subtree in a binary tree
流媒体技术优化
顶级 DevOps 工具链大盘点
RuntimeError: no valid convolution algorithms available in CuDNN
RuntimeError: no valid convolution algorithms available in CuDNN
Which common ports should the server open