150 najlepszych wywiadówŁatwe

Iloczyn tablicy z wyjątkiem siebie

Szczegółowy przewodnik i implementacja Python dla problemu „Produkt tablicy z wyjątkiem siebie”.

Oświadczenie o problemie

Łatwe

Biorąc pod uwagę tablicę liczb całkowitych nums, zwróć tablicę answer taką, że answer[i] jest równe iloczynowi wszystkich elementów nums z wyjątkiem nums[i].

Gwarantujemy, że iloczyn dowolnego przedrostka lub sufiksu nums będzie mieścić się w 32-bitowej liczbie całkowitej.

Musisz napisać algorytm działający w czasie O(n) i bez użycia operacji dzielenia.

Napisz funkcję productExceptSelf(nums: List[int]) -> List[int].

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla tablic i haszowania, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.