4层循环暴力超时
class Solution:
def kthLargestValue(self, matrix: List[List[int]], k: int) -> int:
nums=[]
for a in range(len(matrix)):
for b in range(len(matrix[0])):
num=0
for i in range(a+1):
for j in range(b+1):
num^=matrix[i][j]
nums.append(num)
nums.sort()
return nums[-k]
使用前缀异或矩阵,再矩阵转列表
class Solution:
def kthLargestValue(self, matrix: List[List[int]], k: int) -> int:
#前缀矩阵
nums=matrix
for a in range(len(matrix)):
for b in range(len(matrix[0])):
if a==0 and b==0:
nums[a][b]=matrix[a][b]
elif a==0:
nums[a][b]=matrix[a][b]^nums[a][b-1]
elif b==0:
nums[a][b]=matrix[a][b]^nums[a-1][b]
else:
nums[a][b]=matrix[a][b]^nums[a][b-1]^nums[a-1][b]^nums[a-1][b-1]
ret=[]
#前缀矩阵转列表再排序
for i in range(len(matrix)):
for j in range(len(matrix[0])):
ret.append(matrix[i][j])
ret.sort()
return ret[-k]