File tree Expand file tree Collapse file tree 7 files changed +99
-65
lines changed Expand file tree Collapse file tree 7 files changed +99
-65
lines changed Original file line number Diff line number Diff line change 11/**
2+ * https://leetcode.com/problems/single-number/
3+ * Time O(N) | Space O(1)
24 * @param {number[] } nums
35 * @return {number }
46 */
5- var singleNumber = function ( nums ) {
6- let xor_final = 0 ;
7+ var singleNumber = function ( nums , xor = 0 ) {
78 for ( num of nums ) {
8- xor_final = xor_final ^ num ;
9+ xor ^= num ;
910 }
10- return xor_final ;
11+
12+ return xor ;
1113} ;
Original file line number Diff line number Diff line change 1- var reverseBits = function ( n ) {
2- let result = 0b0 ;
3- let curr = n ;
4-
1+ /**
2+ * https://leetcode.com/problems/reverse-bits/
3+ * Time O(1) | Space O(1)
4+ * @param {number } n - a positive integer
5+ * @return {number } - a positive integer
6+ */
7+ var reverseBits = function ( n , bit = 0 ) {
58 for ( let i = 0 ; i < 32 ; i ++ ) {
6- const lastBit = curr & 0b1 ;
7- result = result << 1 ;
8- result = result | lastBit ;
9- curr = curr >> 1 ;
9+ bit <<= 1 ; // Double * 2
10+ bit |= ( n & 1 ) ; // Flip
11+ n >>= 1 ; // Reduce * 0.5
1012 }
1113
12- return result >>> 0 ;
14+ return bit >>> 0 ;
1315} ;
Original file line number Diff line number Diff line change 11/**
2+ * https://leetcode.com/problems/number-of-1-bits/
3+ * Time O(1) | Space (1)
24 * @param {number } n - a positive integer
35 * @return {number }
46 */
5- var hammingWeight = function ( n ) {
6- let output = 0 ;
7- while ( n != 0 ) {
8- n &= n - 1 ;
9- output ++ ;
7+ var hammingWeight = function ( n ) {
8+ let [ bits , mask ] = [ 0 , 1 ]
9+
10+ for ( let i = 0 ; i < 32 ; i ++ ) {
11+ const hasBit = ( ( n & mask ) !== 0 )
12+ if ( hasBit ) bits ++
13+
14+ mask <<= 1
1015 }
11- return output ;
16+
17+ return bits
1218} ;
19+
20+ /**
21+ * https://leetcode.com/problems/number-of-1-bits/
22+ * Time O(1) | Space (1)
23+ * @param {number } n - a positive integer
24+ * @return {number }
25+ */
26+ var hammingWeight = function ( n , sum = 0 ) {
27+ while ( n !== 0 ) {
28+ n &= ( n - 1 )
29+ sum ++
30+ }
31+
32+ return sum
33+ }
Original file line number Diff line number Diff line change 1- var missingNumberWithSums = function ( nums ) {
2- let res = nums . length ;
3-
1+ /**
2+ * https://leetcode.com/problems/missing-number/
3+ * Time O(N) | Space O(1)
4+ * @param {number[] } nums
5+ * @return {number }
6+ */
7+ var missingNumber = function ( nums , missingNumber = nums . length ) {
48 for ( let i = 0 ; i < nums . length ; i ++ ) {
5- res += i - nums [ i ] ;
6- }
7-
8- return res ;
9- } ;
10-
11- var missingNumberWithBit = function ( nums ) {
12- let res = nums . length ;
9+ const xor = ( i ^ nums [ i ] ) ;
1310
14- for ( let i = 0 ; i < nums . length ; i ++ ) {
15- res = res ^ i ^ nums [ i ] ;
11+ missingNumber ^= xor ;
1612 }
1713
18- return res ;
14+ return missingNumber ;
1915} ;
Original file line number Diff line number Diff line change 11/**
2+ * https://leetcode.com/problems/counting-bits/
3+ * Time O(N) | Space (1)
24 * @param {number } n
35 * @return {number[] }
46 */
5- var countBits = function ( n ) {
6- let output = [ 0 ] ;
7- for ( let i = 1 ; i < n + 1 ; i ++ ) {
8- output . push ( output [ i >> 1 ] + ( i & 1 ) ) ;
7+ var countBits = function ( n , dp = [ 0 ] ) {
8+ for ( let i = 1 ; i < ( n + 1 ) ; i ++ ) {
9+ const [ mid , bit ] = [ ( i >> 1 ) , ( i & 1 ) ]
10+ const bits = ( dp [ mid ] + bit )
11+
12+ dp . push ( bits ) ;
913 }
10- return output ;
14+
15+ return dp ;
1116} ;
Original file line number Diff line number Diff line change 1- var getSum = function ( a , b ) {
2- let tb = b ;
3- let res = a ;
1+ /**
2+ * https://leetcode.com/problems/sum-of-two-integers/
3+ * Time O(1) | Space O(1)
4+ * @param {number } a
5+ * @param {number } b
6+ * @return {number }
7+ */
8+ var getSum = function ( a , b ) {
9+ while ( b !== 0 ) {
10+ const [ xor , carry ] = [ ( a ^ b ) , ( ( a & b ) << 1 ) ] ;
411
5- while ( tb ) {
6- let temp = ( res & tb ) << 1 ;
7- res = res ^ tb ;
8- tb = temp ;
12+ a = xor ;
13+ b = carry ;
914 }
10-
11- return res ;
12- } ;
15+
16+ return a
17+ } ;
Original file line number Diff line number Diff line change 11/**
2+ * https://leetcode.com/problems/reverse-integer/
3+ * Time O(log(x)) | Space O(1)
24 * @param {number } x
35 * @return {number }
46 */
5- const reverse = function ( x ) {
6- const max = 2 ** 31 - 1 ;
7- const min = - ( 2 ** 31 ) ;
8-
9- let result = 0 ;
7+ var reverse = function ( x , result = 0 ) {
108 while ( x !== 0 ) {
11- const digit = x % 10 ;
12- x = Math . trunc ( x / 10 ) ;
9+ const digit = ( x % 10 )
1310
14- if ( result > max / 10 || ( result === max / 10 && digit >= max % 10 ) ) {
15- return 0 ;
16- } else if (
17- result < min / 10 ||
18- ( result === max / 10 && digit <= min % 10 )
19- ) {
20- return 0 ;
21- } else {
22- result = result * 10 + digit ;
23- }
11+ if ( isOutOfBounds ( digit , result ) ) return 0 ;
12+
13+ x = Math . trunc ( x / 10 ) ;
14+ result = ( result * 10 ) + digit ;
2415 }
2516
2617 return result ;
2718} ;
19+
20+ const isOutOfBounds = ( digit , result ) => {
21+ const [ max , min ] = [ ( ( 2 ** 31 ) - 1 ) , ( - ( 2 ** 31 ) ) ] ;
22+ const [ maxProduct , maxRemainder ] = [ ( max / 10 ) , ( max % 10 ) ] ;
23+ const [ minProduct , minRemainder ] = [ ( min / 10 ) , ( min % 10 ) ] ;
24+ const isTarget = result === maxProduct ;
25+
26+ const isMaxOut = ( ( maxProduct < result ) || ( isTarget && ( maxRemainder <= digit ) ) ) ;
27+ const isMinOut = ( ( result < minProduct ) || ( isTarget && ( digit <= minRemainder ) ) ) ;
28+
29+ return isMaxOut || isMinOut ;
30+ }
You can’t perform that action at this time.
0 commit comments