【leetcode】991. Broken Calculator
题目如下:
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 <= X <= 10^91 <= Y <= 10^9
解题思路:要增加X只能做乘法操作,要减小X只能做减法。如果X>Y的话,那么只需要一直对X做减法操作直到X=Y为止,operation的次数是X-Y;对于X<Y的情况,我们可以由Y的值反推X,即如果Y是偶数,那么令Y=Y/2,如果Y是奇数,令Y=Y-1,直至Y=X为止。
代码如下:
class Solution(object):
def brokenCalc(self, X, Y):
"""
:type X: int
:type Y: int
:rtype: int
"""
if X >= Y:
return X - Y
res = 0
while Y != X:
res += 1
if Y > X and Y % 2 == 0:
Y = Y / 2
else:
Y = Y + 1
return res
【leetcode】991. Broken Calculator的更多相关文章
- 【LeetCode】991. Broken Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】224. Basic Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...
- 【LeetCode】227. Basic Calculator
Problem: Implement a basic calculator to evaluate a simple expression string. The expression string ...
- 【LeetCode】227. Basic Calculator II
Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...
- 【LeetCode】224. Basic Calculator
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- ubuntu软件源变更阿里源和arm板子变更国内源
使用 lsb_release -c 命令,看看当前版本号,替换一下“bionic”字符 1.更改 /etc/apt/sources.list,更改之前先备份一下 deb http://mirrors. ...
- python每日练习10题
161.求1000以内的所有的素数以及闰年的数之和 第一步:求1000以内的素数,素数:只能被1和本身整除的数叫素数 import math def is_prime(num): if num ==1 ...
- php explode()函数 语法
php explode()函数 语法 作用:把字符串打散为数组 语法:explode(separator,string,limit)大理石机械构件 参数: 参数 描述 separator 必需.规定在 ...
- visual studio code的使用
1.添加代码片段 参考:https://blog.csdn.net/qq_36370731/article/details/83014439 2.在vscode上运行Git 先打开vscode内置的G ...
- Codeforces 803E--Roma and Poker (DP)
原题链接:http://codeforces.com/problemset/problem/803/E 题意:给一个n长度的字符串,其中'?'可以替换成'D'.'W'.'L'中的任意一种,'D'等价于 ...
- 超全Altium Designer16 总结--Altium Designer
原址:http://blog.csdn.net/qq_29350001/article/details/52199356 以前是使用DXP2004来画图的,后来转行.想来已经有一年半的时间没有画过了. ...
- 【BZOJ1801】【DTOJ2004】 [Ahoi2009]chess 中国象棋 【DP】
题解: 首先知道一个性质,每一行每一列都最多有两个炮 那么很显然是DP 设F[i][j][k]表示前i行,有j列有一个炮,有k列有两个炮,那么转移式子为 这一行什么都不做:f[i][j][k]=f[i ...
- C#编写ActiveX控件
用C#编写ActiveX控件 http://www.cnblogs.com/homer/archive/2005/01/04/86473.html http://www.cnblogs.com/hom ...
- Divideing Jewels
Divideing Jewels 时间限制: 1 Sec 内存限制: 128 MB提交: 63 解决: 17[提交][状态] 题目描述 Mary and Rose own a collection ...
- 2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告
实验三 敏捷开发与XP实践-1 1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这 ...