File tree Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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
+
6
8
// 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];
9
11
}
10
12
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];
14
16
}
15
- output[n- 1 ] = prodSoFar;
17
+ output[n - 1 ] = prodSoFar;
16
18
17
19
return output;
18
20
}
@@ -21,6 +23,6 @@ class Solution {
21
23
22
24
23
25
// ==============================================================
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
25
27
26
- // Space complexity: O(1), constant space
28
+ // Space complexity: O(1), constant space
You can’t perform that action at this time.
0 commit comments