Skip to content

Commit d8ea9fe

Browse files
authored
Merge pull request larissalages#200 from shruthi019/master
Added cpp solution for 238
2 parents 7aaa0c1 + 44b236b commit d8ea9fe

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

leetcode/cpp/Array/238.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
class Solution {
22
public:
33
vector<int> productExceptSelf(vector<int>& nums) {
4-
int n= nums.size(), i, prodSoFar= 1;
5-
vector<int> output(n,nums[n-1]);
4+
5+
int n = nums.size(), i, prodSoFar = 1;
6+
vector<int> output(n, nums[n - 1]);
7+
68
//preprocessing the array
7-
for (i=n-2;i>=0;i--){
8-
output[i]= output[i+1]*nums[i];
9+
for (i = n - 2; i >= 0; i--){
10+
output[i] = output[i + 1] * nums[i];
911
}
1012

11-
for (i=0;i<n-1;i++){
12-
output[i]= prodSoFar*output[i+1];
13-
prodSoFar*= nums[i];
13+
for (i = 0; i < n - 1; i++){
14+
output[i] = prodSoFar * output[i + 1];
15+
prodSoFar *= nums[i];
1416
}
15-
output[n-1]= prodSoFar;
17+
output[n - 1] = prodSoFar;
1618

1719
return output;
1820
}
@@ -21,6 +23,6 @@ class Solution {
2123

2224

2325
//==============================================================
24-
//Time complexity of the above algorith: O(n), n is the length of the input array
26+
//Time complexity of the above algorithm: O(n), n is the length of the input array
2527

26-
//Space complexity: O(1), constant space
28+
//Space complexity: O(1), constant space

0 commit comments

Comments
 (0)