[LeetCode] Product of Array Expect Self
1. ๋ฌธ์ : Link
2. ํ์ด
0์ ์ ๋ฌด์ ๊ฐ์์ ๋ฐ๋ผ ๋๋ ์ ๊ฐ์ ๋ฃ์ด์ค๋ค.
์ด ํฉ์์ ๋๋๊ธฐ๋ฅผ ํ๋ฉด O(n)์ ์ ์งํ ์ ์์!
3. ์ฝ๋
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
tmp = 1
check = 0
arr = []
for n in nums:
if n == 0:
check += 1
continue
else:
tmp *= n
if check >= 2:
for _ in range(len(nums)):
arr.append(0)
elif check == 1:
for n in nums:
if n == 0:
arr.append(tmp)
else:
arr.append(0)
else:
for n in nums:
arr.append(tmp // n)
return arr
'๐ป Coding Problems Solving > Array | String | Loop' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฌธ์์ด ๋ง๋ค๊ธฐ (์๋ฐ java) (0) | 2022.11.24 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ต๋๊ฐ๊ณผ ์ต์๊ฐ (์๋ฐ java) (0) | 2022.11.24 |
[LeetCode] Contains Duplicate (0) | 2022.07.09 |
[LeetCode] Best Time to Buy and Sell (0) | 2022.07.03 |
[LeetCode] Two Sum (0) | 2022.06.30 |
์ต๊ทผ๋๊ธ