当前位置:网站首页>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 !
边栏推荐
- RTP 接发ps流工具改进(二)
- How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
- @BindsInstance在Dagger2中怎么使用
- 95页智慧教育解决方案2022
- 简述中台的常识
- 公司里只有一个测试是什么体验?听听他们怎么说吧
- Wechat applet basic learning (wxss)
- Dishes launcher small green program and directory management (efficiency tool)
- The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
- Leetcode DP three step problem
猜你喜欢

JSON data transfer parameters

PR FAQ, what about PR preview video card?

35 pages dangerous chemicals safety management platform solution 2022 Edition

Bean load control

Implementation of VGA protocol based on FPGA

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

Highly available cluster (HAC)

2022 latest and complete interview questions for software testing

How much do you know about synchronized?

Convolution和Batch normalization的融合
随机推荐
RuntimeError: no valid convolution algorithms available in CuDNN
Matlab 信号处理【问答笔记-1】
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
Golang common settings - modify background
Program analysis and Optimization - 9 appendix XLA buffer assignment
Bean加载控制
开发知识点
Solution to boost library link error
Agnosticism and practice makes perfect
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Load balancing cluster (LBC)
直击产业落地!飞桨重磅推出业界首个模型选型工具
Container runtime analysis
JDBC practice cases
[Verilog tutorial]
[OJ] intersection of two arrays (set, hash mapping...)
公司里只有一个测试是什么体验?听听他们怎么说吧
Practical series - free commercial video material library
2022年最新最全软件测试面试题大全
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office