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. scrapy框架介绍

    一,介绍 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有很强通用性 ...

  2. Java线程(1)

    多线程快速入门 线程与进程区别 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行.也可以把它理解为代码运行的上下文.所以 ...

  3. Luogu P5020 货币系统

    Luogu P5020 货币系统 先把$a$数组排一下序. 从最小的数开始选,显然最小这个数必须选,然后利用完全背包的思想,从$a_i$到最大值筛选一遍,将可以组成的打上标记. 在判断后面的数字时,如 ...

  4. 常考JS题笔记

    ### 1. 原始类型有哪几种?null 是对象吗? 答: Null,undefined,Number,String,Blooean,symbol1)[理解和使用ES6中的Symbol][https: ...

  5. flask中使用ajax 处理前端请求,每隔一段时间请求一次

    需求: flask中使用ajax 处理前端请求,每隔一段时间请求一次,并展示在页面 使用 setInterval(function(){},1000)方法 结果展示: html:(test.html) ...

  6. 【Java】debug初级使用(Eclipse)

    1.设置.取消断点 双击要设置断点的代码行数字的前面 2.切换成Debug界面 就会发现画面变成了下图这样 以下是调试的界面解说(图源百度) 3.切换为原界面

  7. Mybatis-Generator逆向工程,简单策略

    1.下载generator包 https://github.com/mybatis/generator/releases mybatis-generator-core-1.3.6.zip 官网下载即可 ...

  8. python - pycharm 配置虚拟环境出现的中文命名问题

    说一个困扰我很久的问题,当使用 pycharm 配置新的虚拟环境想要与之前的环境隔离的时候,正常的点击 New Project 创建项目时,不勾选 Inherit global site-packag ...

  9. spring boot 实现多个 interceptor 并指定顺序

    首先我们创建Interceptor,实现HandlerInterceptor覆写方法:一.下面我创建了三个拦截器:MyInterceptor,UserInterceptor,StudentInterc ...

  10. 011_Python3 集合

    集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典.   创 ...