Le migliori 150 intervisteFacile

Prodotto di array tranne sé

Guida dettagliata e implementazione Python per il problema "Prodotto dell'array tranne sé".

Dichiarazione del problema

Facile

Dato un array di numeri interi nums, restituisce un array answer tale che answer[i] sia uguale al prodotto di tutti gli elementi di nums tranne nums[i].

È garantito che il prodotto di qualsiasi prefisso o suffisso di nums rientri in un numero intero a 32 bit.

È necessario scrivere un algoritmo che venga eseguito in tempo O(n) e senza utilizzare l'operazione di divisione.

Scrivi una funzione productExceptSelf(nums: List[int]) -> List[int].

Vincoli
  • 2 <= len(nums) <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix of nums fits in a 32-bit integer

Esempi

Example 1
Input
nums = [1, 2, 3, 4]
Output
[24, 12, 8, 6]
Explanation

answer[0] = 2*3*4 = 24, answer[1] = 1*3*4 = 12, answer[2] = 1*2*4 = 8, answer[3] = 1*2*3 = 6.

Example 2
Input
nums = [-1, 1, 0, -3, 3]
Output
[0, 0, 9, 0, 0]
Explanation

Any product that includes the 0 at index 2 becomes 0. answer[2] = (-1)*1*(-3)*3 = 9.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per array e hashing come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.