Skip to content

Commit ef151e9

Browse files
committed
✨ [1342]
1 parent 9c018a5 commit ef151e9

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

1342/my_solution.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number} num
3+
* @return {number}
4+
*/
5+
const numberOfSteps = (num) => {
6+
let count = 0;
7+
8+
while (num > 0) {
9+
count++;
10+
if (num % 2 == 0) {
11+
num /= 2;
12+
} else {
13+
num--;
14+
}
15+
}
16+
return count;
17+
};
18+
19+
let x = null;
20+
x = numberOfSteps(14);// 6
21+
x = numberOfSteps(8);// 6
22+
23+
console.log("Result")
24+
console.log(x)

1342/solution.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} num
3+
* @return {number}
4+
*/
5+
const numberOfSteps = (num) => {
6+
let count = 0;
7+
8+
while (num > 0) {
9+
count++;
10+
if (num % 2 == 0) {
11+
num /= 2;
12+
} else {
13+
num--;
14+
}
15+
}
16+
return count;
17+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
- [997. Find the Town Judge](./997/)
7373
- [1161. Maximum Level Sum of a Binary Tree](./1161/)
7474
- [1187. Make Array Strictly Increasing](./1187/)
75+
- [1342. Number of Steps to Reduce a Number to Zero](./1342/)
7576
- [1365. How Many Numbers Are Smaller Than the Current Number](./1365/)
7677
- [1480. Running Sum of 1d Array](./1480/)
7778
- [1512. Number of Good Pairs](./1512/)
@@ -131,7 +132,7 @@ Batch create:
131132
NOTE: JS IS HERE
132133
-->
133134
```ssh
134-
chapter=832 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
135+
chapter=1342 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
135136
```
136137
> then you can use `x` for quick debug.
137138

0 commit comments

Comments
 (0)