当前位置:网站首页>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 !
边栏推荐
- @BindsInstance在Dagger2中怎么使用
- [array] binary search
- Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
- How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
- CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
- cocospods 的使用
- How to apply for company email when registering in company email format?
- Dishes launcher small green program and directory management (efficiency tool)
- Go project operation method
- 第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
猜你喜欢

Connexion à distance de la tarte aux framboises en mode visionneur VNC

JDBC tutorial

CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
![Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]](/img/d8/d22cbbaccb1594ee46aca098c41002.png)
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]

How to apply for company email when registering in company email format?

Dishes launcher small green program and directory management (efficiency tool)

35页危化品安全管理平台解决方案2022版

理想汽车×OceanBase:当造车新势力遇上数据库新势力

JSON数据传递参数

JDBC練習案例
随机推荐
ArrayList analysis 2: pits in ITR, listiterator, and sublist
A single element in an ordered array -- Valentine's Day mental problems
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
Interface switching based on pyqt5 toolbar button -1
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
Convolution和Batch normalization的融合
What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
基于Pyqt5工具栏按钮可实现界面切换-2
JDBC Exercise case
Use of cocospods
MySQL Foundation
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
How much do you know about synchronized?
MFC file operation
开发知识点
leetcode 650. 2 keys keyboard with only two keys (medium)
@How to use bindsinstance in dagger2
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
面试过了,起薪16k
C# MVC创建一个视图摆脱布局的影响