附leetcode链接:https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
1342. Number of steps to Reduce a Number to Zero
Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
publice int numberOfSteps(int num) {
int steps = 0;
while(num > 0) {
if(num%2 == 0)
num = num/2;
else
num--;
steps++;
}
return steps;
}
小结:对一个int类型的数字进行算数处理,用到while循环、if判断。
网友评论