Skip to content

Commit d54fbb3

Browse files
committed
Solve in 28.8 and 29.8
1 parent 11d550f commit d54fbb3

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def productExceptSelf(self, nums):
3+
product_array = [1] * len(nums)
4+
product_array[0] = 1
5+
product_array[1] = nums[0]
6+
# Base case
7+
if len(nums) == 2:
8+
return [nums[1], nums[0]]
9+
10+
# After for loop, # product_array[i] store the product of element from index 0 to the index i - 1
11+
for i in range(2, len(nums)):
12+
product_array[i] = product_array[i - 1] * nums[i - 1]
13+
14+
current_const = 1
15+
# After second for loop we got the final output
16+
for i in range(len(nums) - 2, -1, -1):
17+
current_const *= nums[i + 1]
18+
product_array[i] *= current_const
19+
20+
return product_array

283. Move Zeroes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def moveZeroes(self, nums):
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
n = len(nums)
7+
total_zeros = 0
8+
for i in range(n):
9+
if nums[i] == 0:
10+
total_zeros += 1
11+
continue
12+
nums[i - total_zeros] = nums[i]
13+
14+
for i in range(n - total_zeros, n):
15+
nums[i] = 0

0 commit comments

Comments
 (0)