LC 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.
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的更多相关文章
- 【leetcode】991. Broken Calculator
题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...
- 991. Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- 【LeetCode】991. Broken Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [Swift]LeetCode991. 坏了的计算器 | Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- 123th LeetCode Weekly Contest Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- A Broken Calculator 最详细的解题报告
题目来源:A Broken Calculator 题目如下(链接有可能无法访问): A Broken Calculator Time limit : 2sec / Stack limit : 256M ...
- 【LeetCode】Broken Calculator(坏了的计算器)
这道题是LeetCode里的第991道题. 题目描述: 在显示着数字的坏计算器上,我们可以执行以下两种操作: 双倍(Double):将显示屏上的数字乘 2: 递减(Decrement):将显示屏上的数 ...
- [LC] 224. Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
随机推荐
- spice在桌面虚拟化中的应用系列之一(spice简介,性能优化等)
1.spice介绍 1.1 spice简介 spice是由Qumranet开发的开源网络协议,2008年红帽收购了Qumranet获得了这个协议.SPICE是红帽在虚拟化领域除了KVM的又一“新兴技术 ...
- Elasticsearch 最佳运维实践 - 总结(一)
对于Elasticsearch的学习,需要清楚的明白它的每个核心概念,由浅入深的了解,才能更好的掌握这门技术.下面先简单罗列下Elasticsearch的核心概念: 一.Elasticsearch数据 ...
- window kvm 虚拟机的创建
1:开始安装win7 (1) 上传iso文件到/data/iso文件夹中 说明:如果不存在的话, 输入 mkdir -p /data/iso (2) 开始安装 virt-install --name= ...
- 备份一下我的88bugs的application文档
# 服务端口 server.port=8083 #spring.mvc.favicon.enabled=false server.servlet.context-path=/bug/ dateform ...
- 虚拟dom应用
vdom如何应用,核心api是什么 1.介绍snabbdom(开源社区用的多,vue2用的是他) 首先回顾下之前的vdom格式 真实的dom <body> <ul id=" ...
- pandas里面过滤列出现ValueError: cannot index with vector containing NA / NaN values错误的解决方法(转)
###df_18的字段fuek是否包含 / df_18[df_18['fuel'].str.contains('/')] 报错: ValueError Traceback (most recent c ...
- jquery仿排列顺序,变换颜色更换class
ps:箭头是字体图标,需要的可以去阿里字体下载,也可自己替换图片 点击之后的 代码部分 <ul class="Ep_sxxz"> <span class=&quo ...
- [bzoj 2693] jzptab & [bzoj 2154] Crash的数字表格 (莫比乌斯反演)
题目描述 TTT组数据,给出NNN,MMM,求∑x=1N∑y=1Mlim(x,y)\sum_{x=1}^N\sum_{y=1}^M lim(x,y)\newlinex=1∑Ny=1∑Mlim(x, ...
- BZOJ3678 wangxz与OJ (平衡树 无旋treap)
题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...
- AirTest与模拟器连接(二)
如果我们手边没有可用的Android真机,又想进行Android应用自动化测试,这时候就要使用AirtestIDE的Android模拟器自动化测试功能了. AirtestIDE所支持的模拟器包括 An ...