题目描述:

Given a positive integer n and you can do operations as follow:

  1. If n is even, replace n with n/2.
  2. If n is odd, you can replace n with either n + 1 or n - 1.

What is the minimum number of replacements needed for n to become 1?

Example 1:

Input:
8 Output:
3 Explanation:
8 -> 4 -> 2 -> 1

Example 2:

Input:
7 Output:
4 Explanation:
7 -> 8 -> 4 -> 2 -> 1
or
7 -> 6 -> 3 -> 2 -> 1 分析,这个题第一眼看上去可以用递归,而且用递归确实可以实现,这就是第一种解法,但是递归的效率不高,这是大家的共识,在我翻看他人的博客和结题经验时,发现了另外一种非递归的方式,
只是这种非递归的方法比较难想,就是自己按照思路实现之后仍然一头雾水,毕竟这个算法不是自己想出来的,感觉其中糅合了贪心算法和动态规划,这是算法题中比较难的两种思想,这也理所当
然地成了我的拦路虎…… 解法一:递归方式
int integerReplacement(int n)
{
if(n == INT_MAX)
return ;
if(n <= )
return ;
if((n & ) == )
return + integerReplacement(n / );
else
return + min(integerReplacement(n - ), integerReplacement(n + ));
}

注意第一种case,可以防止溢出,第一次提交的时候就是这个case出错。

解法二:

int integerReplacement(int n)
{
if (n == INT32_MAX)
return ; int counter = ;
while (n != )
{
if ((n & ) == )
{
++ counter;
n = (n >> );
} else
{
if (n == )
n = ;
else
{
if (containBinaryZeros(n - ) > containBinaryZeros(n + ))
-- n;
else
++ n;
}
++counter;
} }
return counter; } int containBinaryZeros(int n)
{
int counter = ;
while ((n & ) == )
{
++counter;
n = n >> ;
}
return counter;
}

注意其中的特殊case(如3),以及运算的顺序(如== 和 &的运算顺序),第一次提交的时候也是因为没有把&运算给单独括起来导致的bug,所以一定要清晰运算的优先级,

尤其是逻辑运算和算术运算的优先级,这很重要,我已经连续在这个坑栽了两次(泪目)!如果不确定运算的优先级,一定要多用括号!

												

leetcode 397的更多相关文章

  1. LeetCode 397. Integer Replacement

    397. Integer Replacement QuestionEditorial Solution My Submissions   Total Accepted: 5878 Total Subm ...

  2. Java实现 LeetCode 397 整数替换

    397. 整数替换 给定一个正整数 n,你可以做如下操作: 如果 n 是偶数,则用 n / 2替换 n. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n. n 变为 1 所需的最小替换次数 ...

  3. Leetcode 397.整数替换

    整数替换 给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是 ...

  4. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. LeetCode.接雨水

    题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. [工作中的设计模式]责任链模式chain

    一.模式解析 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知 ...

  2. HTML: vertical algin Big/small div in same row (bootstrap)

    Reference: http://stackoverflow.com/questions/20547819/vertical-align-with-bootstrap-3?answertab=vot ...

  3. pip安装模块

    需要到pip目录下执行 pip install 模块名 pip 也需要配置环境变量,电脑是windows

  4. 我觉得好用的VS扩展(不定期更新)

    首先向这些扩展的创作者致敬 这里都是2013版的  有些在给出的连接里有该扩展支持的其他版本连接 当然你也可以通过 VS->工具->扩展和更新->在线->搜索扩展名 来找到它们 ...

  5. css3 缓动公式

    var easingMap = { "linear": [0.250, 0.250, 0.750, 0.750], "ease": [0.250, 0.100, ...

  6. 单元测试 逃不开的Done 与约定

    关注单元测试有一段时间了,也做了些尝试然后就停了下来,寻找框架.方法.各种尝试 看得多,尝试的少, 关于框架分为两类,1是自动化测试工具类,1是js单元测试框架 关于自动化测试工具我尝试了http:/ ...

  7. linux系统ftp命令

    先来一段简单的ftp 下载脚本 ftp -i -n<<EOF open 14.2.33.211 user etl etl cd /etlfile/ftpfile lcd /etlfile/ ...

  8. [转]SQL Server 存储过程 一些常用用法(事物、异常捕捉、循环)

      最新更新请访问: http://denghejun.github.io Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中 ...

  9. webdriverAPI-Java

    1.1   下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGuide:http://seleniu ...

  10. Oracle 树操作(select…start with…connect by…prior)

    摘自:http://www.cnblogs.com/linjiqin/archive/2013/06/24/3152674.html oracle树查询的最重要的就是select…start with ...