[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






7會員
47內容數
這裡會放一些我寫過的 Leetcode 解題和學習新技術的筆記
留言0
查看全部
發表第一個留言支持創作者!
Youna's Devlog 的其他內容
[Leetcode] 217. Contains Duplicate
閱讀時間約 2 分鐘
[Leetcode] 1. Two Sum
閱讀時間約 1 分鐘
[Leetcode] 49. Group Anagrams
閱讀時間約 3 分鐘
[Leetcode] 125. Valid Palindrome
閱讀時間約 3 分鐘
[Leetcode] 20. Valid Parentheses
閱讀時間約 3 分鐘
你可能也想看
【LeetCode】896. Monotonic Array || 這一刻,意識到了自己的成長。題目: 給一個陣列,判斷內容是不是遞增或遞減
Thumbnail
avatar
2022-04-22
【LeetCode】19.Remove Nth Node From End of ListInput: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
Thumbnail
avatar
2021-10-09
【LeetCode】876. Middle of the Linked ListInput: head = [1,2,3,4,5] Output: [3,4,5] 單看列表只是要找中間值,不過給定的資料結構不是陣列,而是鏈結串列。
Thumbnail
avatar
2021-10-09
【LeetCode】189. Rotate Array之前跳過的題目,回來補完成。 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Thumbnail
avatar
2021-10-08
【LeetCode】557. Reverse Words in a String III今日題目: 把一行字內每個單字都反轉字元。 Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Thumbnail
avatar
2021-10-08
【LeetCode】344. Reverse String今日題目:字串反轉 Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
Thumbnail
avatar
2021-10-08
【LeetCode】283. Move Zeroes題目要求如下: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 把0都搬到後面去,非0的數字移到前面,且不更改原本數字的大小順序。
Thumbnail
avatar
2021-10-07
【LeetCode】977. Squares of a Sorted Array比今天的題目示例如下: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] 簡單來說,要的輸出結果是把陣列內每個數字取平方後,對陣列做排序。
Thumbnail
avatar
2021-10-06
【LeetCode】704. Binary SearchLeetcode上正好有14天的讀書計畫,今天三題都是二分搜索,順手三題都寫一寫,想法上很固定。
Thumbnail
avatar
2021-10-05
【LeetCode】20. Valid Parentheses 前幾天看別人直播刷題,心血來潮打開很久沒動的leetcode試試,挑了一題當初面試沒寫出來的題目。嗯...我那時才剛碰資料結構,知道要用stack寫卻沒實作過任何東西,現在有工作經驗後再來寫,看看會不會有不一樣的想法。
Thumbnail
avatar
2021-10-04