【LeetCode】Broken Calculator(坏了的计算器)
这道题是LeetCode里的第991道题。
题目描述:
在显示着数字的坏计算器上,我们可以执行以下两种操作:
- 双倍(Double):将显示屏上的数字乘 2;
- 递减(Decrement):将显示屏上的数字减 1 。
最初,计算器显示数字
X。返回显示数字
Y所需的最小操作数。示例 1:
输入:X = 2, Y = 3
输出:2
解释:先进行双倍运算,然后再进行递减运算 {2 -> 4 -> 3}.示例 2:
输入:X = 5, Y = 8
输出:2
解释:先递减,再双倍 {5 -> 4 -> 8}.示例 3:
输入:X = 3, Y = 10
输出:3
解释:先双倍,然后递减,再双倍 {3 -> 6 -> 5 -> 10}.示例 4:
输入:X = 1024, Y = 1
输出:1023
解释:执行递减运算 1023 次
如果这道题你是硬算的话 (由 X 到 Y),那我得佩服你,因为直接从正面硬算的话需要考虑的条件很多原因在于有乘 2 这个操作,我们不知道什么时候乘 2 才合适。这里我使用的是逆向思维(由 Y 到 X),逻辑就简单多了,因为对于 Y 来说,它要除 2,就必须是偶数。否则递增。
通过除 2,使 Y 不断的逼近 X。对于偶数,除 2 操作,不管怎么样,肯定比递增 Y 好。
解题代码:
class Solution {
public int brokenCalc(int X, int Y) {
if(X>Y)return X-Y;
int res=0;
while(X<Y){
if(Y%2==1){
Y++;
}else{
Y/=2;
}
res++;
}
return res+X-Y;
}
}
提交结果:

个人总结:
这道题是一道贪心题,正面计算的算法肯定有。在这里我想说的是我自己的代码,最后的返回值我的并不是 " return res+X-Y; " 而是 " return res; ",while 循环条件我的也并不是 " X<Y " 而是 " X!=Y ",还有中间的 if 判断条件,我这样改算是优化了一下代码,缩短了计算的时间。
【LeetCode】Broken Calculator(坏了的计算器)的更多相关文章
- [Swift]LeetCode991. 坏了的计算器 | Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- 【LeetCode】991. Broken Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 123th LeetCode Weekly Contest Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- 【leetcode】991. Broken Calculator
题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...
- 991. Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- LC 991. Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- A Broken Calculator 最详细的解题报告
题目来源:A Broken Calculator 题目如下(链接有可能无法访问): A Broken Calculator Time limit : 2sec / Stack limit : 256M ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
随机推荐
- 现在的Unix时间戳(Unix timestamp)
如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScript Math.round(new Date().getTime()/1000)ge ...
- 最大流bfs
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- Java 多个if 和多个else if 的区别
int a=1; if(a==1){System.out.println("1");} if(a==2){System.out.println("2");} i ...
- PeopleSoft FSCM Production Support 案例分析之一重大紧急事故发生时的应对策略
案例背景: 今天一大早用户打电话来讲昨天上传的银行的forex payment return file好像没有被处理到,我一听就觉得纳闷,因为昨天晚上operator也没有给我打电话啊(如果有job ...
- mongodb-3.2.8 单机复制集安装
规划: replSet 复制集名称: rs1 MongoDB数据库安装安装路径为:/usr/local/mongodb/ 复制集成员IP与端口: 节点1: localhost:28010 (默认的 ...
- Hyperledger Fabric on SAP Cloud Platform
今天的文章来自Wen Aviva, 坐Jerry面对面的程序媛. Jerry在之前的公众号文章<在SAP UI中使用纯JavaScript显示产品主数据的3D模型视图>已经介绍过Aviva ...
- scrollviews page分页实现方式
代码 buttonX = 0; buttonW = 50; buttonH = 20; margin = (self.view.width - 5 * buttonW) / 6; CGFloat ym ...
- Dockerfile优化建议
1. 减少镜像层 一次RUN指令形成新的一层,尽量Shell命令都写在一行,减少镜像层. 2. 优化镜像大小:清理无用数据 一次RUN形成新的一层,如果没有在同一层删除,无论文件是否最后删除,都会带到 ...
- table 会有默认的外边框,内部会有分割线
.表格中边框的显示 只显示上边框 <table frame=above> 只显示下边框 <table frame=below> 只显示左.右边框 <table frame ...
- 2.add two number
在初始化的时候:ListNode* result;这样就会报runtime error