File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int climbStairs (int n ) {
3
+ if (n == 1 ) {
4
+ return 1 ;
5
+ }
6
+ int [] dp = new int [n + 1 ];
7
+
8
+ dp [1 ] = 1 ;
9
+ dp [2 ] = 2 ;
10
+
11
+ for (int i = 3 ; i <= n ; i ++) {
12
+ dp [i ] = dp [i - 1 ] + dp [i - 2 ];
13
+ }
14
+
15
+ return dp [n ];
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] productExceptSelf (int [] nums ) {
3
+ int [] answer = new int [nums .length ];
4
+ int [] front = new int [nums .length ];
5
+ int [] back = new int [nums .length ];
6
+
7
+ for (int i = 0 ; i < nums .length ; i ++) {
8
+ front [i ] = 1 ;
9
+ back [i ] = 1 ;
10
+ }
11
+
12
+ for (int i = 0 ; i < nums .length - 1 ; i ++) {
13
+ front [i + 1 ] = front [i ] * nums [i ];
14
+ }
15
+
16
+ for (int i = nums .length - 1 ; i > 0 ; i --) {
17
+ back [i - 1 ] = nums [i ] * back [i ];
18
+ }
19
+
20
+ for (int i = 0 ; i < nums .length ; i ++) {
21
+ answer [i ] = front [i ] * back [i ];
22
+ }
23
+
24
+ return answer ;
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments