1 class Solution: 2 def kWeakestRows(self, mat: 'List[List[int]]', k: int) -> 'List[int]': 3 m = len(mat) 4 n = len(mat[0]) 5 counter = [0] * m 6 for i in range(m): 7 for j in range(n): 8 if mat[i][j] == 1: 9 counter[i] += 1 10 else: 11 break 12 #print(counter) 13 at = [] 14 for i in range(len(counter)): 15 at.append((i,counter[i])) 16 at = sorted(at,key=lambda x:(x[1],x[0])) 17 result = [] 18 for x in range(k): 19 result.append(at[x][0]) 20 return result
算法思路:排序。