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. Oracle加密解密

    Oracle内部有专门的加密包,可以很方便的对内部数据进行加密(encrypt)和解密(decrypt). 介绍加密包之前,先简单说一下Oracle基本数据类型——RAW类型. RAW,用于保存位串的 ...

  2. Flutter——TabBar组件(顶部Tab切换组件)

    TabBar组件的常用属性: 属性 描述 tabs 显示的标签内容,一般使用 Tab 对象,也可以是其他的Widget  controller TabController 对象 isScrollabl ...

  3. java--动态代理设计模式,CGLIB实现的动态代理设计模式

    代理设计模式 代理设计模式的基本形式 代理设计模式的核心思路,一个接口两个子类,一个子类完成核心业务操作,另一个完成与核心业务有关的辅助性操作.例如,编写一个简单的设计模式. package com. ...

  4. CEIWEI CommTone串口调试精灵7.1 串口调试 串口工具

    CEIWEI CommTone串口调试精灵   是一款功能强大的串行端口通信调试软件,内嵌超过100种标准的CRC校验功能,并支校验结果高低位字节前导转换:支持批量协议调试,并支持文件.16进制.UN ...

  5. Thread.sleep()和Thread.currentThread().sleep()区别

    先看一下代码 public class Thread1 extends Thread{ @Override public void run() { try { System.out.println(& ...

  6. 19 使用Vue实例的render方法渲染组件

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. springboot整合shiro引用配置文件配置redis信息报空指针异常

    1.问题现象: 上面这些属性是从application.properties配置文件中获取的,按常理来说应该能顺利获取到,但均未赋上值. 2.解决办法:(不得不说百度,千篇一律,最后用谷歌找到的) 最 ...

  8. 记录 ubuntu 安装中文语言出现 software database is broken

    在终端下执行 sudo apt-get install  language-pack-zh-han* 然后去语言中心设置中文,并全局使用即可.

  9. MySQL 视图 触发器 事务 存储过程 函数 流程控制 索引与慢查询优化

    视图 1.什么是视图? 视图就是通过查询得到的一张虚拟表,然后保存下来,下次可直接使用 2.为什么要使用视图? 如果要频繁使用一张虚拟表,可以不用重复查询 3.如何使用视图? create view ...

  10. pycharm鼠标更改字体大小

    Pycharm快捷键设置(鼠标滚动控制字体大小) 一.pycharm字体放大的设置 File -> setting -> Keymap ->在搜寻框中输入:increase -> ...