Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 36c71da

Browse files
committed
Add solution #1404
1 parent 9d40d44 commit 36c71da

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,283 LeetCode solutions in JavaScript
1+
# 1,284 LeetCode solutions in JavaScript
22

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

@@ -1072,6 +1072,7 @@
10721072
1401|[Circle and Rectangle Overlapping](./solutions/1401-circle-and-rectangle-overlapping.js)|Medium|
10731073
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
10741074
1403|[Minimum Subsequence in Non-Increasing Order](./solutions/1403-minimum-subsequence-in-non-increasing-order.js)|Easy|
1075+
1404|[Number of Steps to Reduce a Number in Binary Representation to One](./solutions/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js)|Medium|
10751076
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
10761077
1410|[HTML Entity Parser](./solutions/1410-html-entity-parser.js)|Medium|
10771078
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1404. Number of Steps to Reduce a Number in Binary Representation to One
3+
* https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/
4+
* Difficulty: Medium
5+
*
6+
* Given the binary representation of an integer as a string s, return the number of steps to reduce
7+
* it to 1 under the following rules:
8+
* - If the current number is even, you have to divide it by 2.
9+
* - If the current number is odd, you have to add 1 to it.
10+
*
11+
* It is guaranteed that you can always reach one for all test cases.
12+
*/
13+
14+
/**
15+
* @param {string} s
16+
* @return {number}
17+
*/
18+
var numSteps = function(s) {
19+
let num = BigInt(`0b${s}`);
20+
let steps = 0;
21+
22+
while (num !== 1n) {
23+
if (num % 2n === 0n) {
24+
num /= 2n;
25+
} else {
26+
num += 1n;
27+
}
28+
steps++;
29+
}
30+
31+
return steps;
32+
};

0 commit comments

Comments
 (0)