[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
透過蝦皮分潤計畫,輕鬆賺取零用金!本文分享5-6月實測心得,包含數據流程、實際收入、平臺優點及注意事項,並推薦高分潤商品,教你如何運用空閒時間創造被動收入。
Thumbnail
透過蝦皮分潤計畫,輕鬆賺取零用金!本文分享5-6月實測心得,包含數據流程、實際收入、平臺優點及注意事項,並推薦高分潤商品,教你如何運用空閒時間創造被動收入。
Thumbnail
單身的人有些會養寵物,而我養植物。畢竟寵物離世會傷心,植物沒養好再接再厲就好了~(笑)
Thumbnail
單身的人有些會養寵物,而我養植物。畢竟寵物離世會傷心,植物沒養好再接再厲就好了~(笑)
Thumbnail
不知你有沒有過這種經驗?衛生紙只剩最後一包、洗衣精倒不出來,或電池突然沒電。這次一次補貨,從電池、衛生紙到洗衣精,還順便分享使用心得。更棒的是,搭配蝦皮分潤計畫,愛用品不僅自己用得安心,分享給朋友還能賺回饋。立即使用推薦碼 X5Q344E,輕鬆上手,隨時隨地賺取分潤!
Thumbnail
不知你有沒有過這種經驗?衛生紙只剩最後一包、洗衣精倒不出來,或電池突然沒電。這次一次補貨,從電池、衛生紙到洗衣精,還順便分享使用心得。更棒的是,搭配蝦皮分潤計畫,愛用品不僅自己用得安心,分享給朋友還能賺回饋。立即使用推薦碼 X5Q344E,輕鬆上手,隨時隨地賺取分潤!
Thumbnail
身為一個典型的社畜,上班時間被會議、進度、KPI 塞得滿滿,下班後只想要找一個能夠安靜喘口氣的小角落。對我來說,畫畫就是那個屬於自己的小樹洞。無論是胡亂塗鴉,還是慢慢描繪喜歡的插畫人物,那個專注在筆觸和色彩的過程,就像在幫心靈按摩一樣,讓緊繃的神經慢慢鬆開。
Thumbnail
身為一個典型的社畜,上班時間被會議、進度、KPI 塞得滿滿,下班後只想要找一個能夠安靜喘口氣的小角落。對我來說,畫畫就是那個屬於自己的小樹洞。無論是胡亂塗鴉,還是慢慢描繪喜歡的插畫人物,那個專注在筆觸和色彩的過程,就像在幫心靈按摩一樣,讓緊繃的神經慢慢鬆開。
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 解題
Thumbnail
千萬不要傻傻乘開啦,這題有好幾種優雅的做法~
Thumbnail
千萬不要傻傻乘開啦,這題有好幾種優雅的做法~
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News