Skip to content

Commit 87f20e4

Browse files
committed
Add solution #1017
1 parent 55ac67f commit 87f20e4

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,091 LeetCode solutions in JavaScript
1+
# 1,092 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -825,6 +825,7 @@
825825
1014|[Best Sightseeing Pair](./solutions/1014-best-sightseeing-pair.js)|Medium|
826826
1015|[Smallest Integer Divisible by K](./solutions/1015-smallest-integer-divisible-by-k.js)|Medium|
827827
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|
828829
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
829830
1023|[Camelcase Matching](./solutions/1023-camelcase-matching.js)|Medium|
830831
1028|[Recover a Tree From Preorder Traversal](./solutions/1028-recover-a-tree-from-preorder-traversal.js)|Hard|

solutions/1017-convert-to-base-2.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
};

0 commit comments

Comments
 (0)