File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,091 LeetCode solutions in JavaScript
1
+ # 1,092 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
825
825
1014|[ Best Sightseeing Pair] ( ./solutions/1014-best-sightseeing-pair.js ) |Medium|
826
826
1015|[ Smallest Integer Divisible by K] ( ./solutions/1015-smallest-integer-divisible-by-k.js ) |Medium|
827
827
1016|[ Binary String With Substrings Representing 1 To N] ( ./solutions/1016-binary-string-with-substrings-representing-1-to-n.js ) |Medium|
828
+ 1017|[ Convert to Base -2] ( ./solutions/1017-convert-to-base-2.js ) |Medium|
828
829
1022|[ Sum of Root To Leaf Binary Numbers] ( ./solutions/1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
829
830
1023|[ Camelcase Matching] ( ./solutions/1023-camelcase-matching.js ) |Medium|
830
831
1028|[ Recover a Tree From Preorder Traversal] ( ./solutions/1028-recover-a-tree-from-preorder-traversal.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1017. Convert to Base -2
3
+ * https://leetcode.com/problems/convert-to-base-2/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer n, return a binary string representing its representation in base -2.
7
+ *
8
+ * Note that the returned string should not have leading zeros unless the string is "0".
9
+ */
10
+
11
+ /**
12
+ * @param {number } n
13
+ * @return {string }
14
+ */
15
+ var baseNeg2 = function ( number ) {
16
+ if ( number === 0 ) return '0' ;
17
+
18
+ let result = '' ;
19
+ let current = number ;
20
+
21
+ while ( current !== 0 ) {
22
+ const remainder = current & 1 ;
23
+ result = remainder + result ;
24
+ current = ( current - remainder ) / - 2 ;
25
+ }
26
+
27
+ return result ;
28
+ } ;
You can’t perform that action at this time.
0 commit comments