Solution.java 542 B

123456789101112131415161718192021222324
  1. package leetcode.p1342;
  2. /**
  3. * @ProjectName: LeetCode
  4. * @FileName: Solution
  5. * @Author: 杨逸
  6. * @Data:2024/4/9 14:20
  7. * @Description: https://leetcode.cn/problems/number-of-steps-to-reduce-a-number-to-zero/
  8. * 1342. 将数字变成 0 的操作次数
  9. */
  10. public class Solution {
  11. public int numberOfSteps(int num) {
  12. int count = 0;
  13. while (num != 0){
  14. if (num%2==0){
  15. num /= 2;
  16. }else {
  17. num--;
  18. }
  19. count++;
  20. }
  21. return count;
  22. }
  23. }