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/ [题意] 告诉你任意两个 ...
随机推荐
- 4款最受欢迎的Mac原型工具
原型工具中Wireframe, Mockup和prototype之间的有什么不同? 无论你是一名刚入行的UX/UI设计师,还是入行多年的老手,在制作原型的过程中一定接触或听说过其中很重要的三个原型术语 ...
- 关于HBase的memstoreFlushSize。
memstoreFlushSize是什么呢? memstoreFlushSize为HRegion上设定的一个阈值,当MemStore的大小超过这个阈值时,将会发起flush请求. 它的计算首先是由Ta ...
- 4619 Warm up 2
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ][]; ...
- ajax中 XmlHttp的open( )方法
博客分类: Ajax XML open 创建一个新的http请求,并指定此请求的方法.URL以及验证信息 语法 oXMLHttpRequest.open(bstrMethod, bstrUrl, ...
- list 返回列表null替换
function formatt(rec, val, index) { if (rec === null || rec == "null") { return "0&qu ...
- [GO]结构体成员的使用:指针变量
package main import "fmt" func main() { type student struct { id int name string sex byte ...
- ThinkPhp数据缓存技术
1.缓存初始化 在 ThinkPHP 中,有一个专门处理缓存的类:Cache.class.php(在Thinkphp/Library/Think/cache.class.php,其他的各种缓存类也在这 ...
- 初涉Runtime (一)
Objective-C 是一门动态语言,有很多东西都是运行时才确定的. 比如这句代码首先声明testObject是一个NSString,然后创建了一个NSData对象,并且将这个对象的内存地址保存在t ...
- SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
一.spring cloud简介 鉴于<史上最简单的Spring Cloud教程>很受读者欢迎,再次我特意升级了一下版本,目前支持的版本为Spring Boot版本2.0.3.RELEAS ...
- ABP架构
ABP架构 一.什么是ABP架构? ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate 基于DDD的经典分层 ...