Skip to content

Commit d0a3728

Browse files
committed
add product of array
1 parent e11b290 commit d0a3728

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

product-of-array-except-self/README.md

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int[] output = new int[nums.length];
4+
5+
if(nums.length == 0) return output;
6+
7+
output[0] = nums[0];
8+
9+
for(int i = 1; i < nums.length; i++){
10+
output[i] = output[i - 1] * nums[i];
11+
}
12+
13+
output[nums.length - 1] = output[nums.length - 2] * 1;
14+
15+
int t = nums[nums.length - 1];
16+
for(int i = output.length - 2; i > 0 ; i--){
17+
output[i] = t * output[i - 1];
18+
t *= nums[i];
19+
}
20+
21+
output[0] = t;
22+
23+
return output;
24+
}
25+
}

product-of-array-except-self/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: solution
3+
title: Product of Array Except Self
4+
date: 2015-07-26 17:23:07+08:00
5+
leetcode_id: 238
6+
---
7+
{% include_relative README.md %}

0 commit comments

Comments
 (0)