On a broken calculator that has a number showing on its display, we can perform two operations:

  • Double: Multiply the number on the display by 2, or;
  • Decrement: Subtract 1 from the number on the display.

Initially, the calculator is displaying the number X.

Return the minimum number of operations needed to display the number Y.

Example 1:

Input: X = 2, Y = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.

Example 2:

Input: X = 5, Y = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.

Example 3:

Input: X = 3, Y = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.

Example 4:

Input: X = 1024, Y = 1
Output: 1023
Explanation: Use decrement operations 1023 times.
 
 
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Broken Calculator.
Memory Usage: 4.7 MB, less than 100.00% of C++ online submissions for Broken Calculator.
class Solution {
public:
int brokenCalc(int X, int Y) {
if(X == Y) return ;
if(X < Y) {
if(Y& == ) {
int tmp = ((Y >> )+) << ;
return brokenCalc(X,tmp >> ) + tmp - Y + ;
}else {
return brokenCalc(X,Y>>)+;
}
}
return X - Y;
}
};

LC 991. Broken Calculator的更多相关文章

  1. 【leetcode】991. Broken Calculator

    题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...

  2. 991. Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  3. 【LeetCode】991. Broken Calculator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [Swift]LeetCode991. 坏了的计算器 | Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  5. 123th LeetCode Weekly Contest Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  6. A Broken Calculator 最详细的解题报告

    题目来源:A Broken Calculator 题目如下(链接有可能无法访问): A Broken Calculator Time limit : 2sec / Stack limit : 256M ...

  7. 【LeetCode】Broken Calculator(坏了的计算器)

    这道题是LeetCode里的第991道题. 题目描述: 在显示着数字的坏计算器上,我们可以执行以下两种操作: 双倍(Double):将显示屏上的数字乘 2: 递减(Decrement):将显示屏上的数 ...

  8. [LC] 224. Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  9. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

随机推荐

  1. Linux命令——tac、rev

    tac和rev命令列到一起,并不是功能相似,而是他们都是将输入内容反置. tac -s:使用指定字符串代替换行作为分隔标志 [root@localhost ~]# echo "1,2&quo ...

  2. LInux CentOS7 vsftpd 配置注释

    本文首发: https://www.somata.work/2019/LinuxCentOSvsftpdConfigComment.html vsftpd.conf anonymous_enable= ...

  3. 【DevOps】在CentOS中安装Rancher2,并配置kubernetes集群

    准备 一台CentOS主机,安装DockerCE,用于安装Rancher2 一台CentOS主机,安装DockerCE,用于安装kubernetes集群管理主机 多台CentOS主机,安装Docker ...

  4. Centos 6.x开机启动流程

    Centos 6.x开机启动流程 BIOS(COMS)检查 加载Bios,bios包含所有硬件信息(CPU,内存,硬盘,时钟,鼠标键盘等等) 读MBR 硬盘上第0磁道第一个扇区被称为MBR(maste ...

  5. Gym - 102012H Rikka with A Long Colour Palette N线段K色贪心染色

    给你数轴上的N条线段和K种颜色 K和N1e5 要你把这N条线段染色 使得有K种不同颜色的线段长度最长 首先很容易想到被至少K段线段覆盖的区间是一定有贡献的 接下来就是怎么染色的问题 我们把这N个区间的 ...

  6. 胡搞-强化版的light oj-1055-的思路-AI版的6重暴力For循环的BFS

    新题目大意: 三个棋子按照先后顺序,可以随意方向合法地走到空位置上(而不是像原题light oj-1055中的一样三个棋子每次走的方向都一致),当三个棋子全部走进目标地点,就结束:求需要指挥的最少次数 ...

  7. LINUX 新手 入门 教程

    新手入门教程 点评: linux 入门 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声

  8. C#格式化信息,格式化数字、格式化日期

    一.格式化方法: 1.ToString()实例方法 使用当前文化: varname.ToString("C4"); 使用特定文化: varname.ToString("C ...

  9. TOMCAT 请求HTTP原理

    一.Tomcat是什么?Tomcat是一个Web应用服务器,同时也是一个Servlet/JSP容器.Tomcat作为Servlet容器,负责处理客户端请求,把请求传送给Servlet,并将Servle ...

  10. Java:JVM的内存模型

    JVM内存模型 JVM内存模型可以分为两个部分,如下图所示,堆和方法区是所有线程共有的,而虚拟机栈,本地方法栈和程序计数器则是线程私有的.   1. 堆(Heap) 堆内存是所有线程共有的,可以分为两 ...