当前位置:网站首页>力扣刷题日记/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 science or metaphysics to rename a listed company?
- 估值900亿,超级芯片IPO来了
- 项目通用环境使用说明
- 五千字讲清楚团队自组织建设 | Liga 妙谈
- ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
- Achieve animation effect through event binding
- 【每日一题】556. 下一个更大元素 III
- Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
- With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
猜你喜欢
删除二叉搜索树中的节点附图详解
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)
同事悄悄告诉我,飞书通知还能这样玩
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
输入的查询SQL语句,是如何执行的?
MVC mode and three-tier architecture
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
表情包坑惨职场人
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
随机推荐
【Hot100】32. 最长有效括号
Mysql5.7 installation tutorial graphic explanation
Thawte通配符SSL证书提供的类型有哪些
mysql5.7安装教程图文详解
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
【209】go语言的学习思想
超标量处理器设计 姚永斌 第6章 指令解码 摘录
为啥有些线上演唱会总是怪怪的?
Make a grenade with 3DMAX
[209] go language learning ideas
曾经的“彩电大王”,退市前卖猪肉
SIGMOD’22 HiEngine论文解读
蓝桥:合根植物
ISO27001 certification process and 2022 subsidy policy summary
[HCIA continuous update] network management and operation and maintenance
Flask lightweight web framework