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.

Note:

  1. 1 <= X <= 10^9
  2. 1 <= Y <= 10^9

CF原题,我们反过来看,Y是偶数就/2,奇数就加1,到了比X小就再加

class Solution {
public:
int brokenCalc(int X, int Y) {
int cnt=;
if(X>=Y){
return X-Y;
}
while(X!=Y){
if(Y<X)Y++;
else if(Y%){
Y++;
}else{
Y/=;
}
//cout<<Y<<endl;
cnt++;
}
return cnt;
}
};

123th LeetCode Weekly Contest Broken Calculator的更多相关文章

  1. 123th LeetCode Weekly Contest Add to Array-Form of Integer

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...

  2. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

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

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

  7. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  8. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  9. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

随机推荐

  1. Java 读取jar内的文件的超简便方法

    坑爹的java课程设计,偏要用jar来运行 读取.存储jar内文件的支持也好低 存储方法: 进入jar文件其实没有说的那么困难,jar文件本质是一个zip格式的压缩文件,只是把文件后缀名改了,要用Ja ...

  2. Windows 配置 nginx php 多版本切换

    1. 下载 nginx   nginx.org 2. 下载 php  windows.php.net   选择 nts 版本,解压后,将php.ini.development 重命名为  php.in ...

  3. Android Logging

    Here is HauteLook’s logger class from our Android Code Library. It displays class name, method name ...

  4. HALCON机器视觉软件

    HALCON是德国MVtec公司开发的一套完善的标准的机器视觉算法包,拥有应用广泛的机器视觉集成开发环境.它节约了产品成本,缩短了软件开发周期——HALCON灵活的架构便于机器视觉,医学图像和图像分析 ...

  5. Java如何实现按指定行读取文件

    最近在开发实战中,遇到了一个这样的技术情景: 把log4j生成的日志文件定时刷进MySQL数据库,比如三个小时刷一次,那么每次刷数据的时候,如何控制文件读取是从上一次文件读取结束的地方开始继续读取的? ...

  6. C#的Winform中OpenFileDialog对话框Filter属性设置包含特定字符,使用正则表达式

    OpenFileDialog对话框的Filter属性说明: 首先观察Filter属性的组成部分:“Word文件|*.doc ”,前面的“Word文件”成为标签,是一个可读的字符串,可以自定定义,“|* ...

  7. .net core i上 K8S(三)Yaml文件运行.netcore程序

    上一章我们通过kubectl run简单运行了一个.netcore网站,但实际的开发中,我们都是通过yaml来实现的. 1.编写yaml文件 关于yaml文件的格式在此就不多描述了,不熟悉的可以去网上 ...

  8. docker 进入容器

  9. Windows 2012 R2版本下部署IIS网站

    Windows 2012 R2是一个比较稳定的服务器版本,本文分享一篇在Windows 2012 R2版本下搭建IIS项目的操作流程. 1. 安装IIS Web服务器 打开远程桌面->控制面板- ...

  10. 通过api获取句柄控制其他窗体

    很多时候,编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的 ...