Broken Calculator Solutions in C++
Number 991
Difficulty Medium
Acceptance 45.6%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/broken-calculator/// Author : Hao Chen// Date : 2019-05-01class Solution {public:int brokenCalc(int X, int Y) {if (X >= Y) return X-Y ;if ( Y%2 ==0 ) return brokenCalc(X, Y/2) + 1;return brokenCalc(X, Y+1) + 1;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/broken-calculator//// Author : liuyubobobo/// Time : 2019-02-09#include <iostream>#include <cassert>using namespace std;/// Greedy/// Time Complexity: O(logY)/// Space Complexity: O(1)class Solution {public:int brokenCalc(int X, int Y) {if(X >= Y) return X - Y;int res = 0;while(X != Y){if(Y % 2) res ++, Y ++;else res ++, Y /= 2;if(X > Y)return res + X - Y;}return res;}};int main() {cout << Solution().brokenCalc(2, 3) << endl;// 2cout << Solution().brokenCalc(5, 8) << endl;// 2cout << Solution().brokenCalc(3, 10) << endl;// 3cout << Solution().brokenCalc(1024, 1) << endl;// 1023cout << Solution().brokenCalc(1, 1000000000) << endl;// 39return 0;}