[Leetcode] 238. Product of Array Except Self

更新於 發佈於 閱讀時間約 4 分鐘

題目 : 238. Product of Array Except Self Medium

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.


Example 1:

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

Example 2:

Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]


# 不能用除法 時間限制O(n)

# nums = [1, 2, 3, 4]

# answer = [24,12, 8, 6]

# 24 = 2 * 3 * 4 left = 1 reght = 2*3*4

# 12 = 1 * * 3 * 4 left = 1 reght = 3*4

# 8 = 1 * 2 * 4 left = 1*2 reght = 4

# 6 = 1 * 2 * 3 left = 1*2*3 reght = 1

# left = [1,1,2,6] ->

# right = [24,12,4,1] <-

# answer = left*right = [24,12,8,6]

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:


length = len(nums)

L, R, answer = [0]*length, [0]*length, [0]*length

L[0] = 1
for i in range(1, length):
L[i] = nums[i-1] * L[i-1]

R[length-1] = 1
for i in range(length-2, -1, -1):
R[i] = R[i+1] * nums[i+1]

for i in range(length):
answer[i] = R[i] * L[i]

return answer


  1. 只使用1個陣列存取

line 7 : 因為第1個元素的左邊沒有值可以相乘,所以直接assign 1給它

line 8 : 先將左邊的部分存到anwer陣列中

line 11 : buff是暫存右邊乘積的數值,倒數第1個元素的右邊沒有值,所以assign 1 給它

line 12 : 設1個for迴圈,從倒數第2個元素開始做計算,將原先存在answer的左邊乘積乘上buff(右邊乘積),即可得出最後的答案

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:

length = len(nums)

answer = [0]*length
answer[0] = 1
for i in range(1,length):
answer[i] = answer[i-1] * nums[i-1]

buff = 1
for i in range(length-2, -1, -1):
buff *= nums[i+1]
answer[i] *= buff

return answer






留言
avatar-img
留言分享你的想法!
avatar-img
Youna's Devlog
7會員
49內容數
這裡會放一些我寫過的 Leetcode 解題和學習新技術的筆記
Youna's Devlog的其他內容
2024/02/13
題目 : 121. Best Time to Buy and Sell Stock
Thumbnail
2024/02/13
題目 : 121. Best Time to Buy and Sell Stock
Thumbnail
2024/01/11
題目 : 100. Same Tree
Thumbnail
2024/01/11
題目 : 100. Same Tree
Thumbnail
2024/01/11
題目 : 83. Remove Duplicates from Sorted List
Thumbnail
2024/01/11
題目 : 83. Remove Duplicates from Sorted List
Thumbnail
看更多
你可能也想看
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
Thumbnail
題目敘述 題目會給定我們一個輸入陣列nums,要求我們掃描美個陣列元素nums[i],計算除了nums[i]以外的陣列元素連乘積。 題目的原文敘述 測試範例 Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] nums[0] 以
Thumbnail
題目敘述 題目會給定我們一個輸入陣列nums,要求我們掃描美個陣列元素nums[i],計算除了nums[i]以外的陣列元素連乘積。 題目的原文敘述 測試範例 Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] nums[0] 以
Thumbnail
正確答案居然有無限多組!找到任何一種就算通過,這是怎麼回事?
Thumbnail
正確答案居然有無限多組!找到任何一種就算通過,這是怎麼回事?
Thumbnail
題目敘述 題目會給定一個整數陣列nums,要求我們找出最大的兩個整數a, b,返回(a-1) * (b-1)的乘積。 詳細的題目可在這裡看到 測試範例 Example 1: Input: nums = [3,4,5,2] Output: 12
Thumbnail
題目敘述 題目會給定一個整數陣列nums,要求我們找出最大的兩個整數a, b,返回(a-1) * (b-1)的乘積。 詳細的題目可在這裡看到 測試範例 Example 1: Input: nums = [3,4,5,2] Output: 12
Thumbnail
128. Longest Consecutive Sequence 解題
Thumbnail
128. Longest Consecutive Sequence 解題
Thumbnail
238. Product of Array Except Self 解題
Thumbnail
238. Product of Array Except Self 解題
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News