File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ function twoSum ( nums : number [ ] , target : number ) : number [ ] {
2
+ const map = new Map ( ) ;
3
+
4
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
5
+ if ( map . has ( nums [ i ] ) ) {
6
+ return [ i , map . get ( nums [ i ] ) ] ;
7
+ } else {
8
+ map . set ( target - nums [ i ] , i ) ;
9
+ }
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ function containsDuplicate ( nums : number [ ] ) : boolean {
2
+ const set = new Set ( ) ;
3
+
4
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
5
+ if ( set . has ( nums [ i ] ) ) return true ;
6
+ else set . add ( nums [ i ] ) ;
7
+ }
8
+
9
+ return false ;
10
+ }
Original file line number Diff line number Diff line change
1
+ function isAnagram ( s : string , t : string ) : boolean {
2
+ if ( s . length !== t . length ) return false ;
3
+
4
+ const store = new Array ( 26 ) . fill ( 0 ) ;
5
+
6
+ for ( let i = 0 ; i < s . length ; i ++ ) {
7
+ store [ s . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) ] ++ ;
8
+ store [ t . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) ] -- ;
9
+ }
10
+
11
+ for ( let i = 0 ; i < store . length ; i ++ ) {
12
+ if ( store [ i ] !== 0 ) return false ;
13
+ }
14
+
15
+ return true ;
16
+ }
You can’t perform that action at this time.
0 commit comments