File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int climbStairs (int n ) {
3+ int [] memo = new int [n +1 ];
4+ return recur (n , memo );
5+ }
6+
7+ public static int recur (int n , int [] memo ) {
8+ if (n < 0 ) return 0 ;
9+ if (n == 0 ) return 1 ;
10+
11+ if (memo [n ] > 0 ) return memo [n ];
12+
13+ memo [n ] = recur (n - 1 , memo ) + recur (n - 2 , memo );
14+ return memo [n ];
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] productExceptSelf (int [] nums ) {
3+ int [] res = new int [nums .length ];
4+
5+ res [0 ] = 1 ;
6+ for (int i = 1 ; i < nums .length ; i ++) {
7+ res [i ] = res [i - 1 ] * nums [i - 1 ];
8+ }
9+
10+ int acc = 1 ;
11+ for (int i = nums .length - 2 ; i >= 0 ; i --) {
12+ acc *= nums [i + 1 ];
13+ res [i ] *= acc ;
14+ }
15+
16+ return res ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isAnagram (String s , String t ) {
3+ if (s .length () != t .length ()) {
4+ return false ;
5+ }
6+
7+ HashMap <Character , Integer > map = new HashMap <>();
8+ char [] originString = s .toCharArray ();
9+ for (int i = 0 ; i < originString .length ; i ++) {
10+ Integer count = map .getOrDefault (originString [i ], 0 );
11+ map .put (originString [i ], count + 1 );
12+ }
13+
14+ char [] targetString = t .toCharArray ();
15+ for (int i = 0 ; i < targetString .length ; i ++) {
16+ Integer count = map .get (targetString [i ]);
17+ if (count == null || count == 0 ) {
18+ return false ;
19+ }
20+ map .put (targetString [i ], count - 1 );
21+ }
22+ return true ;
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments