给你一个 m * n 的矩阵,矩阵中的数字 各不相同 。请你按 任意 顺序返回矩阵中的所有幸运数。
幸运数是指矩阵中满足同时下列两个条件的元素:
- 在同一行的所有元素中最小
- 在同一列的所有元素中最大
大佬的解法:
class Solution:
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
mins = [min(rows) for rows in matrix]
maxes = [max(columns) for columns in zip(*matrix)]
lucky = []
for i, row in enumerate(matrix):
for j, value in enumerate(row):
if value == mins[i] and value == maxes[j]:
lucky.append(value)
return lucky
tips: zip(*matrix)来获取矩阵的列
我的解法:
import numpy as np
class Solution:
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
np.array(matrix)
ma = []
minindex = np.argmin(matrix, axis = 1)
maxindex = np.argmax(matrix, axis = 0)
for index,num in enumerate(minindex):
if index == maxindex[num]:
ma.append(matrix[index][num])
return ma