当前位置:网站首页>力扣刷题日记/day4/6.26
力扣刷题日记/day4/6.26
2022-07-04 16:33:00 【bobo洁厕灵】
新手村
日升日落,总有黎明
VScode运行java项目
新建一个窗口
CTRL+SHIFT+P,在弹出的窗口中输入create java,选择No build tools
选择一个文件夹存放java工程代码
设置工程名称
即可运行java代码
while循环
do while循环
do{
代码块
}while(布尔表达式);do while循环至少执行一次,因为先执行do语句,再执行判断语句while,所以至少执行一次代码块内容。
while循环
while(表达式){
代码块;
//跳出while循环的方法
if(){
break
}
}while(1)、while(2)、while(3)都表示条件为真,也就是说非0的数都表示真。
while循环若是表达式不满足,则一次都不执行。
自增符i++和++i
i++是先运算,再加一
++i是先加一,再运算
Java中i=i++和i++

以上代码中,最后输出的i的值始终为0,
因为i首先赋值给i为0,执行一次后,i的值为0,再进行i=i++,赋值运算一直在赋i=0。
修改代码

以上代码就可以实现对i的值的改变,while语句执行了n次,i的值就为n
例题

解题思路:首先定义运算次数i,创建一个while循环,判断num是否为偶数,是的话num=num/2,否的话num=num-1。i++,记录执行次数,最后return i
class Solution {
public int numberOfSteps(int num) {
int i=0;
while(num != 0){
if(num % 2 == 0){
num=num/2;
}
else{
num=num-1;
}
i++;
}
return i;
}
}
边栏推荐
- 12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
- fopen、fread、fwrite、fseek 的文件处理示例
- 【209】go语言的学习思想
- Recast of recastnavigation
- [proteus simulation] printf debugging output example based on VSM serial port
- The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
- 如何进行MDM的产品测试
- 明星开店,退,退,退
- android使用SQLiteOpenHelper闪退
- General environmental instructions for the project
猜你喜欢

Grain Mall (I)

Recast of recastnavigation

【Hot100】32. Longest valid bracket

90后开始攒钱植发,又一个IPO来了

Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation

ISO27001认证办理流程及2022年补贴政策汇总

为啥有些线上演唱会总是怪怪的?

一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)

MySQL common add, delete, modify and query operations (crud)

华为云ModelArts的使用教程(附详细图解)
随机推荐
RecastNavigation 之 Recast
Self reflection of a small VC after two years of entrepreneurship
Russia arena data releases PostgreSQL based products
Initial experience of domestic database tidb: simple and easy to use, quick to start
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Detectron2 installation method
内核中时间相关的知识介绍
regular expression
About the pit of firewall opening 8848 when Nacos is started
超标量处理器设计 姚永斌 第6章 指令解码 摘录
Thawte通配符SSL证书提供的类型有哪些
Introduction of time related knowledge in kernel
如何提高开发质量
要上市的威马,依然给不了百度信心
[cloud native] what is the "grid" of service grid?
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
俄罗斯 Arenadata 发布基于PostgreSQL的产品
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
【Hot100】31. 下一个排列
机器学习概念漂移检测方法(Aporia)