150 principais entrevistasFácil

Definir zeros da matriz

Guia detalhado e implementação de Python para o problema 'Definir zeros de matriz'.

Declaração do problema

Fácil

Dada uma matriz matriz inteira m x n, se um elemento for 0, defina toda a sua linha e coluna como 0.

Você deve fazer isso no lugar.

Implemente uma função setZeroes(matrix: list) -> list que modifica a matriz no local e a retorna.

Restrições
  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -2^31 <= matrix[i][j] <= 2^31 - 1

Exemplos

Example 1
Input
[[1,1,1],[1,0,1],[1,1,1]]
Output
[[1,0,1],[0,0,0],[1,0,1]]
Explanation

The element at position (1,1) is 0. So the entire row 1 and column 1 are set to 0.

Example 2
Input
[[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output
[[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Explanation

Elements at (0,0) and (0,3) are 0. Row 0 becomes all zeros. Columns 0 and 3 become all zeros.

Need a Hint?
Considere usar estruturas de dados específicas de matemática e geometria, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.