没有想出好的搜索方案。
这是一道数学题,根据题目可以写成如下公式:
2^a * x - (2^b + 2^c + ....) = y
所以,从y往x去算,更直接。
/*
* @lc app=leetcode id=991 lang=cpp
*
* [991] Broken Calculator
*/
// @lc code=start
class Solution {
public:
int brokenCalc(int startValue, int target) {
int ans = 0;
while(startValue < target) {
if(target&1) target++;
else target /= 2;
ans ++;
}
return ans + startValue - target;
}
};
// @lc code=end