My LeetCode grinding. Trying to do a problem a day.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132
  1. class Solution:
  2. def oddCells(self, n, m, indices):
  3. # Initialise the matrix
  4. matrix = []
  5. for x in range(n):
  6. new_row = []
  7. for y in range(m):
  8. new_row.append(0)
  9. matrix.append(new_row)
  10. # Now apply steps
  11. for i in indices:
  12. ri = i[0]
  13. ci = i[1]
  14. for n in range(len(matrix[ri])):
  15. matrix[ri][n] += 1
  16. for n in range(len(matrix)):
  17. matrix[n][ci] += 1
  18. # now count odd values (this could be done while changes are made, but this is easier to reason)
  19. count = 0
  20. for x in range(len(matrix)):
  21. for y in range(len(matrix[x])):
  22. if matrix[x][y] % 2 != 0:
  23. count += 1
  24. return count
  25. s = Solution()
  26. print("Expected: 6")
  27. print("Got:", s.oddCells(2, 3, [[0, 1], [1, 1]]))