当前位置:网站首页>力扣刷题日记/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;
}
}
边栏推荐
- Is it safe to download the mobile version of Anxin securities and open an account online
- Make a grenade with 3DMAX
- 使用3DMAX制作一枚手雷
- 庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
- The block:usdd has strong growth momentum
- S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
- 【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
- 同事悄悄告诉我,飞书通知还能这样玩
- [test development] software testing - Basics
- gatling 之性能测试
猜你喜欢

【Hot100】32. 最长有效括号

爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用

超标量处理器设计 姚永斌 第6章 指令解码 摘录

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

力扣刷题日记/day7/6.30

华为云ModelArts的使用教程(附详细图解)

7 RSA Cryptosystem

Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
![[test development] software testing - Basics](/img/43/514016f270574fe711e0e15b581022.png)
[test development] software testing - Basics

Mysql5.7 installation tutorial graphic explanation
随机推荐
[proteus simulation] printf debugging output example based on VSM serial port
[211] go handles the detailed documents of Excel library
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
创业两年,一家小VC的自我反思
如何提高开发质量
表情包坑惨职场人
LD_ LIBRARY_ Path environment variable setting
Imitation of numpy 2
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
爬虫初级学习
[test development] software testing - Basics
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
Introduction of time related knowledge in kernel
Redis主从复制
Five thousand words to clarify team self-organization construction | Liga wonderful talk
RecastNavigation 之 Recast
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
[209] go language learning ideas