leetcode 1342. Number of Steps to Reduce a Number to Zero(数归为0需要几步)

LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero Table of Contents 中文版: 英文版: My answer: 解题报告: 中文版: 给你一个非负整数num,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。 示例 1: 输入:num = 14 输出:6 解释: 步骤 1) 14 是偶数,除以 2 得到 7 。 步骤 2) 7 是奇数,减 1 得到 6 。 步骤 ... 阅读详情

Given an integer num, return the number of steps to reduce it to zero.

In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.

Example 1:

Input: num = 14
Output: 6
Explanation:
Step 1) 14 is even; divide by 2 and obtain 7.
Step 2) 7 is odd; subtract 1 and obtain 6.
Step 3) 6 is even; divide by 2 and obtain 3.
Step 4) 3 is odd; subtract 1 and obtain 2.
Step 5) 2 is even; divide by 2 and obtain 1.
Step 6) 1 is odd; subtract 1 and obtain 0.

给一个数字num, 问需要多少步能把它变为0,
一步是这样的:
如果是偶数,就除以2,
奇数就-1.

思路:
就按题目说的来,偶数/=2, 奇数-1, 直到num为0

public int numberOfSteps(int num) {
    int cnt = 0;
    
    while(num > 0) {
        if(num % 2 == 0) {
            num /= 2;
        } else {
            num -= 1;
        }
        cnt ++;
    }
    
    return cnt;
}
LeetCode --- 1342. Number of Steps to Reduce a Number to Zero 解题报告 【代码】LeetCode --- 1342. Number of Steps to Reduce a Number to Zero 解题报告。 阅读详情

相关推荐

torch.jit.script 与 torch.jit.trace

它们是 PyTorch 的即时编译(Just-in-Time Compilation)模块的一部分,用于提高模型的执行效率并支持模型的部署。方法只跟踪了给定输入张量的执行路径,因此在使用转换后的模块对象进行推理时,输入张量的维度和数据类型必须与跟踪时使用的相同。跟踪模型可以看作是一个具有相同功能的脚本模型,但它还保留了原始模型的动态特性,可以使用更多高级特性,如动态图和控制流。它接受一个模型和一个示例输入,并记录模型在给定输入上的执行过程,然后返回一个跟踪模型。的模型文件,并将其转换为评估模式。

leoatliang的博客 1万+

Leetcode 1342. Number of Steps to Reduce a Number to Zero (Java)

Given an integernum, returnthe number of steps to reduce it to zero. In one step, if the current number is even, you have to divide it by2, otherwise, you have to subtract1from it. Example 1: Input: num = 14 Output: 6 Explanation: Step 1) 14 is...

weixin_58080442的博客 174

微带线,带状线和接地共面波导的区别

微带线,带状线和接地共面波导的区别

sky_ying的博客 3万+

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

[Easy] LeetCode 1342. Number of Steps to Reduce a Number to Zero 链接: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/ 题目描述: Given a non-negative integer num, return the numbe...

MrJustin的博客 304

用户存续期价值评估CLV(二) BG/NBD Model python模拟

BG/NBD Model …继续 想象每一个客户有两个硬币,一个“Buy Coin"控制客户购买的概率;一个"Die Coin"控制客户流失、不再购买的概率。 前面假设了客户的交易次数服从泊松过程: 在一年中的每个月,客户都以抛硬币的方式来决定是否购买。我们所观察到的购买次数取决与泊松分布的参数 ????。 %matplotlib inline import numpy as np impor...

weixin_43171270的博客 3419

概率论基础-严士健 第二版 习题与补充1.6答案

概率论基础-严士健 第二版 习题与补充1.6答案 1.不独立。 设有一均匀正八面体,其第1,2,3,4面染红色,第1,2,3,5面染白色,第1,6,7,8面染黑色,现在以A,B,C分别表示投一次正八面体出现红、白、黑的事件,则 P(A)=P(B)=P(C)=12.P(A) = P(B) = P(C) = \frac{1}{2}.P(A)=P(B)=P(C)=21​. P(ABC)=18=P(A)P...

weixin_42341993的博客 2515

[LeetCode-1342]Number of Steps to Reduce a Number to Zero

该题是easy,比较简单,我用的是常规的方法,看到了一行实现的大神。。。 文章目录题目相关Solution 题目相关 【题目解读】 给定一个数(0~10610^6106),判断该数经过减1 或 除2操作到达0所需要的步数。 【原题描述】原题链接 Given a non-negative integer num, return the number of steps to reduce it t...

Donald Su 749

科研智能体平台搭建与实战:大模型如何驱动科研自动化

大模型技术的快速发展,正推动AI智能体从单纯对话工具向主动执行任务的科研伙伴演进。智能体的核心原理在于整合模型调用、工具使用、知识库检索与工作流编排,使其能够自主拆解目标、执行操作并迭代优化。在科研场景中,这一技术价值尤为突出:文献初筛、实验数据清洗、代码调试、结果汇总等高频重复劳动均可实现自动化,大幅释放研究者精力。从基础组件配置到多智能体协同,从平台选型到提示词调优,实际落地需要关注模型匹配、工具稳定性、记忆管理与人机校验机制。本文基于真实工程经验,系统梳理科研智能体平台的搭建路径、应用案例与高频问题排

weixin_32597009的博客 282

1342. Number of Steps to Reduce a Number to Zero*

1342. Number of Steps to Reduce a Number to Zero* https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/ 题目描述 Given a non-negative integer num, return the number of steps to reduce ...

珍妮的选择的博客 473

iphone查看/private/var/mobile/Containers/Data/Application/文件

xcode查看iPhone的private/var/文件夹xcode连接iPhone真机选择app下载Containers文件夹到mac电脑中查看手机中下载的文件 使用xcode真机调试的时候,文件目录都是这种/private/var/mobile/Containers/Data/Application/FAEFDF4D-5CE4-474F-9C53-C19789DB59F6/tmp/ xcode连接iPhone真机 不需要越狱,使用xcode上连接iPhone,然后选择运行的app,选择window-&g

小手琴师的博客 101万+

RAG——RAG 索引(文档解析技术)

本文介绍了RAG系统中文档解析的关键技术,重点分析了如何通过LangChain的Document Loaders处理多种格式文档。文章指出高质量文档解析对RAG系统效果的决定性作用,并详细讲解了PDF、TXT、Word等常见格式的解析方法。作者提供了代码示例和Gitee开源项目链接,特别强调了PDF解析的深入方案,同时介绍了相关依赖库的安装配置。文章还展示了如何定义通用文档加载方法,为开发者提供了实用的技术参考。

小志的博客 751

【Leetcode】1342. Number of Steps to Reduce a Number to Zero

【代码】【Leetcode】1342. Number of Steps to Reduce a Number to Zero。

数学、算法爱好者的博客 454

LLC 谐振转换器中为什么需要 ( L_m )(励磁电感)

在每个模态中,励磁电感 ( L_m )、谐振电感 ( L_r ) 和谐振电容 ( C_r ) 分别承担着储存和释放能量、维持谐振电流和电压波形的任务。Q1和Q2的交替导通确保了这些元件能够在不同的模态下实现能量的有效传输和谐振状态的维持。励磁电感 ( L_m ) 在这一过程中起着不可或缺的作用,确保了电路的高效运行和稳定性。

weixin_43199439的博客 3652

Leetcode 1342.将数字变成 0 的操作次数(Number of Steps to Reduce a Number to Zero)

Leetcode 1342.将数字变成 0 的操作次数 1 题目描述(Leetcode题目链接)   给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。 输入:num = 14 输出:6 解释: 步骤 1) 14 是偶数,除以 2 得到 7 。 步骤 2) 7 是奇数,减 1 得到 6 。 步骤 3) 6 是偶数,除以 2...

qq_39378221的博客 297

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

【LeetCode】No.1342. Number of Steps to Reduce a Number to Zero -- Java Version

【LeetCode】No.1342. Number of Steps to Reduce a Number to Zero -- Java Version

汤姆lazy的优快云博客 814

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode C++ 1342. Number of Steps to Reduce a Number to Zero【Bit Manipulation】简单

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. Example 1: Input: num = 14 Output: 6 Explanation: Step 1) 14 is even;

memcpy0的博客 360

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode 1342. Number of Steps to Reduce a Number to Zero

【代码】Leetcode 1342. Number of Steps to Reduce a Number to Zero。

小白菜又菜 309

LeetCode:1342. Number of Steps to Reduce a Number to Zero 将数字变成 0 的操作次数(C语言)

题目描述: 给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。 示例 1: 输入:num = 14 输出:6 解释: 步骤 1) 14 是偶数,除以 2 得到 7 。 步骤 2) 7 是奇数,减 1 得到 6 。 步骤 3) 6 是偶数,除以 2 得到 3 。 步骤 4) 3 是奇数,减 1 得到 2 。 步骤 5) 2 ...

C/C++攻城狮 477

leetcode 1342. Number of Steps to Reduce a Number to Zero(python)

描述 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. Example 1: Input: num = 14 Output: 6 Explanation: Step 1) 14 is eve

王大呀呀的博客 336

Leetcode 1342. Number of Steps to Reduce a Number to Zero (Python)

Given an integernum, returnthe number of steps to reduce it to zero. In one step, if the current number is even, you have to divide it by2, otherwise, you have to subtract1from it. Example 1: Input: num = 14 Output: 6 Explanation: Step 1) 14 is...

weixin_58080442的博客 196

leetcode 1342. Number of Steps to Reduce a Number to Zero 将数字变成 0 的操作次数-耗时100

Problem:耗时100%,按照题意操作就可以了。

邹九的个人博客 51

LeetCode(每周一题)——1342. 将数字变成 0 的操作次数

题目地址为:https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/ 给你一个非负整数num,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。 理解题意之后,一个简单的while循环就能搞定。 public int numberOfStep...

xugc2015的博客 241

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 352

LeetCode Hot 100 链表专题 三· 3 题(Java)

收录题目:2. 两数相加、19. 删除链表的倒数第 N 个结点、24. 两两交换链表中的节点。 统一结构:① 题目分析与解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结。 贯穿三题的两条主线:虚拟头结点 dummy(统一处理头结点被修改的场景)与 指针修改顺序(避免断链、丢引用)。

2401_88612756的博客 481

LeetCode 每日一题 2026/11/2-2026/11/8

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步

AlphaTao的博客 174
上一篇: orb slam3之BA部分源码解读
下一篇: orb slam3根据匹配特征点重建相机pose部分源码解读
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值