123th LeetCode Weekly Contest 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
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的更多相关文章
- 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. ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- 【LeetCode】991. Broken Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
随机推荐
- Excel分类汇总与数据有效性
分类汇总就是把一些数据按照一个标准进行分类,然后按照相应的汇总方式进行汇总. 使用分类汇总之前先排序,否则汇总会出现很多类. 看如上这个表,如果按照所属区域分类,然后按照金额的总和汇总,在汇总之前就要 ...
- OBD Problem Vehicles
This page contains a list of vehicles that are known to be non-compliant with OBD-II in one way or a ...
- 第05章 管理ElasticSearch
本章内容 如何选择正确的目录实现,使得ElasticSearch能够以高效的方式访问底层I/O系统. 如何配置发现模块来避免潜在的问题. 如何配置网关模块以适应我们的需求. 恢复模块能带来什么,以及如 ...
- 编写高质量代码改善C#程序的157个建议——建议138:事件和委托变量使用动词或形容词短语命名
建议138:事件和委托变量使用动词或形容词短语命名 事件和委托使用场景是调用某个方法,只不过这个方法由调用者赋值.这决定了对应的变量应该以动词或形容词短语命名. 关于事件和委托变量妥当的命名示例如下: ...
- socket API详解
send函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向 ...
- JPA和Hibernate的相关使用技巧
介绍 尽管有SQL标准,但每个关系数据库终将是唯一的,因此你需要调整数据访问层,以便充分利用在使用中的关系数据库. 在本文中,我们将介绍在使用带有JPA和Hibernate的MySQL时,为了提高性能 ...
- LightOJ 1098(均值不等式,整除分块玄学优化)
We all know that any integer number n is divisible by 1 and n. That is why these two numbers are not ...
- [Windows] IIS6 部署ISAPI
环境: OS:Windows Server 2003 Enterprise sp2 34位 IIS:6.0 ISAPI: delphi xe 编译的webbroker isapi dll 32位 ( ...
- duilib入门简明教程 -- XML基础类(7)
现在大家应该对XML描述界面不那么陌生了,那么我们做进一步介绍. 前面的教程我们写了很多代码,为的是让大家了解下基本流程,其实duilib已经对常用的操作做了很好的包装,正式使用时无需像前面的教程那样 ...
- django+echarts
思路: 统一返回数据类型为json,然后前端发起Ajax请求后台数据接口 views.py def count_blog(request): # 下面等价于:select distinct auth, ...