LeetCode - Algorithms - 1342. Number of Steps to Reduce a Number to Zero

Problem

1342. Number of Steps to Reduce a Number to Zero

Java

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int numberOfSteps (int num) {
int i = 0;
while (num > 0) {
if ((num & 1) == 1) {
num--;
} else {
num = num >> 1;
}
i++;
}
return i;
}
}

Submission Detail

  • 204 / 204 test cases passed.
  • Runtime: 1 ms, faster than 19.49% of Java online submissions for Number of Steps to Reduce a Number to Zero.
  • Memory Usage: 38 MB, less than 11.94% of Java online submissions for Number of Steps to Reduce a Number to Zero.

2

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int numberOfSteps (int num) {
int i = 0;
for (; num > 0; i++) {
if ((num & 1) == 1) {
num--;
} else {
num = num >> 1;
}
}
return i;
}
}

Submission Detail

  • 204 / 204 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Number of Steps to Reduce a Number to Zero.
  • Memory Usage: 38.2 MB, less than 6.92% of Java online submissions for Number of Steps to Reduce a Number to Zero.