1. LeetCode991题目链接链接
https://leetcode-cn.com/problems/broken-calculator/submissions/
2.解题思路
这个题目相对简单,出了可以将X乘2或者减1之外,还可以将Y除2加1,我们可以用逆向思维来让Y无限接近于X,最后计算次数既可以了。
public int brokenCalc(int X, int Y) {
int count = 0;
while (Y > X) {
count++;
if (Y % 2 == 1) {
Y++;
} else {
Y /= 2;
}
}
return count + X - Y;
}
3.提交结果

网友评论