1342. Number of Steps to Reduce a Number to Zero
Linear Solution
class Solution {
public:
int numberOfSteps(int num) {
int steps = 0;
while (num != 0) {
if (num % 2 == 0) {
num /= 2;
} else {
num -= 1;
}
steps++;
}
return steps;
}
};
Complexity
- Time Complexity: O(logN), where N is the size of
num
. - Space Complexity: O(1).
Constant Solution
TODO Implement using this