这道题是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(坏了的计算器)的更多相关文章

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

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

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

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

  3. 123th LeetCode Weekly Contest Broken Calculator

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

  4. 【leetcode】991. Broken Calculator

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

  5. 991. Broken Calculator

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

  6. LC 991. Broken Calculator

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

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

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

  8. [LeetCode] Basic Calculator 基本计算器

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

  9. [LeetCode] Basic Calculator III 基本计算器之三

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

随机推荐

  1. JMeter--PerfMon Metrics Collector监控内存及CPU

    1.需要准备的软件及插件 ServerAgent-2.2.1.zip JMeterPlugins-Standard-1.3.1.zip 2.jmeter上JMeterPlugins-Standard- ...

  2. 简单的鼠标经过特效-mouse事件

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

  3. vs安装包

    http://blog.csdn.net/cometnet/article/details/19551125

  4. hihocoder1776 序列

    思路: 考虑从左至右依次向每个位置放置数字,对于第i个位置,以i为结尾的i个前缀和模P是不能相等的(因为不存在和为P的倍数的子串),所以第i个位置只能放置P - i个不同的数字.则答案就是(P - 1 ...

  5. php后端程序开发学习网站梳理

    博主只列举自己经常用到的: 看教学视频:慕课网,网易云课堂,百度传课 看博文和知识点:csdn 看源码:github 刷题:codewars 学习框架:thinkphp官网 下载相关资源:csdn 看 ...

  6. Android 6.0 运行时权限处理完全解析 (摘抄)

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/50709663: 本文出自:[张鸿洋的博客] 一.概述 随着Android 6. ...

  7. C#操作Txt(追加模式)

    /// <summary> /// 输出指定信息到文本文件 /// </summary> /// <param name="msg">输出信息& ...

  8. codevs 1097 校门外的树 2005年NOIP全国联赛普及组 (线段树)

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可 ...

  9. fgetc, fgets, getc, getchar, gets, ungetc - 输入字符和字符串

    总览 (SYNOPSIS) #include <stdio.h> int fgetc(FILE *stream); char *fgets(char *s, int size, FILE ...

  10. currentStyle和getComputedStyle来获取外部样式

    currentStyle和getComputedStyle来获取外部样式 通过document.getElementById(id).style.XXX就可以获取到XXX的值,但意外的是,这样做只能取 ...