File tree 3 files changed +37
-0
lines changed
014.Longest Common Prefix
3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ const twoSum = function ( nums , target ) {
2
+ const map = { } ;
3
+
4
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
5
+ if ( map [ nums [ i ] ] !== undefined ) {
6
+ return [ map [ nums [ i ] ] , i ]
7
+ } else {
8
+ map [ target - nums [ i ] ] = i
9
+ }
10
+ }
11
+ } ;
Original file line number Diff line number Diff line change
1
+ const reverse1 = function ( x ) {
2
+ let s = String ( x ) ;
3
+ let isNegative = false ;
4
+ if ( s [ 0 ] === '-' ) {
5
+ isNegative = true ;
6
+ }
7
+ s = parseInt ( s . split ( '' ) . reverse ( ) . join ( '' ) ) ;
8
+ return isNegative ? ( s > Math . pow ( 2 , 31 ) ? 0 : - s ) : ( s > Math . pow ( 2 , 31 ) - 1 ? 0 : s ) ;
9
+ }
10
+
11
+ const reverse = function ( x ) {
12
+ let result = parseInt ( x . toString ( ) . split ( '' ) . reverse ( ) . join ( '' ) ) ;
13
+ if ( result > Math . pow ( 2 , 31 ) - 1 || - result < Math . pow ( - 2 , 31 ) ) return 0 ;
14
+ return x > 0 ? result : - result ;
15
+ }
Original file line number Diff line number Diff line change
1
+ const longestCommonPrefix = function ( strs ) {
2
+ if ( strs . length === 0 ) return '' ;
3
+ for ( let j = 0 ; j < strs [ 0 ] . length ; j ++ ) {
4
+ for ( let i = 0 ; i < strs . length ; i ++ ) {
5
+ if ( strs [ 0 ] [ j ] !== strs [ i ] [ j ] ) {
6
+ return strs [ 0 ] . substring ( 0 , j ) ;
7
+ }
8
+ }
9
+ }
10
+ return strs [ 0 ] ;
11
+ }
You can’t perform that action at this time.
0 commit comments