Skip to content

Commit 9048421

Browse files
add 1342
1 parent ad91780 commit 9048421

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1343|[Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium||
12+
|1342|[Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1342.java) | |Easy|Bit Manipulation|
1213
|1341|[The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1341.java) | |Easy||
1314
|1338|[Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1338.java) | |Medium||
1415
|1337|[Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1337.java) | |Easy|String|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1342. Number of Steps to Reduce a Number to Zero
5+
*
6+
* Given a non-negative integer num, return the number of steps to reduce it to zero.
7+
* If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
8+
*
9+
* Example 1:
10+
* Input: num = 14
11+
* Output: 6
12+
* Explanation:
13+
* Step 1) 14 is even; divide by 2 and obtain 7.
14+
* Step 2) 7 is odd; subtract 1 and obtain 6.
15+
* Step 3) 6 is even; divide by 2 and obtain 3.
16+
* Step 4) 3 is odd; subtract 1 and obtain 2.
17+
* Step 5) 2 is even; divide by 2 and obtain 1.
18+
* Step 6) 1 is odd; subtract 1 and obtain 0.
19+
*
20+
* Example 2:
21+
* Input: num = 8
22+
* Output: 4
23+
* Explanation:
24+
* Step 1) 8 is even; divide by 2 and obtain 4.
25+
* Step 2) 4 is even; divide by 2 and obtain 2.
26+
* Step 3) 2 is even; divide by 2 and obtain 1.
27+
* Step 4) 1 is odd; subtract 1 and obtain 0.
28+
*
29+
* Example 3:
30+
* Input: num = 123
31+
* Output: 12
32+
*
33+
* Constraints:
34+
* 0 <= num <= 10^6
35+
* */
36+
public class _1342 {
37+
public static class Solution1 {
38+
public int numberOfSteps (int num) {
39+
int steps = 0;
40+
while (num != 0) {
41+
if (num % 2 == 0) {
42+
num /= 2;
43+
} else {
44+
num--;
45+
}
46+
steps++;
47+
}
48+
return steps;
49+
}
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1342;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1342Test {
10+
private static _1342.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1342.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(12, solution1.numberOfSteps(123));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(6, solution1.numberOfSteps(14));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(4, solution1.numberOfSteps(8));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)