File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 %}
You can’t perform that action at this time.
0 commit comments