leetcode 397
题目描述:
Given a positive integer n and you can do operations as follow:
- If n is even, replace n with
n/2. - If n is odd, you can replace n with either
n + 1orn - 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的更多相关文章
- LeetCode 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- Java实现 LeetCode 397 整数替换
397. 整数替换 给定一个正整数 n,你可以做如下操作: 如果 n 是偶数,则用 n / 2替换 n. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n. n 变为 1 所需的最小替换次数 ...
- Leetcode 397.整数替换
整数替换 给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是 ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode.接雨水
题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- ios数据永久存储之----NSUserDefaults
我们在开发app时不可避免的会在本地存储一些数据,NSUserDefaults就是系统提供的一个用来数据存储的类,本片文章就来介绍一些NSserdefazults的用法. 详细内容:https://m ...
- 疑问:line-height对非文字行内块的影响
line-height:对子元素是非文字的行内块,表现出来的不是垂直居中.目前还不知道具体细节. 可以看出来两个东西不在一行.老师的解释是line-height对非文字元素解释不一样,但是我没懂细节. ...
- Linux Mysql 忘记用户密码
1.停止mysql 服务 /etc/init.d/mysqld stop 2.启动mysql服务跳过授权表并在后台运行 /etc/init.d/mysqld start -u root --sk ...
- Python for Infomatics 第12章 网络编程三(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.5 HTML分析和网页抓取 ...
- jquery控制radio选中
好久没写jquery了,昨天下午写一个效果,结果倒腾了半天还是没有解决,好在今天早上在网上询问到解决方法了,现在果断记录下. 要实现的效果就是交易方式选择时不同的交易方式下默认选中第一个选项. 如下: ...
- ZeroMQ接口函数之 :zmq_connect - 由一个socket创建一个对外连接
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_connect zmq_connect(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_c ...
- Testing with a mocking framework (EF6 onwards)
When writing tests for your application it is often desirable to avoid hitting the database. Entity ...
- Java底层实现 - CPU术语
1.内存屏障(memory barriers)是一组处理器指令,用于实现对内存操作的顺序限制 2.缓冲行(cache line)CPU高速缓存中可以分配的最小存储单位.处理器填写缓存行时 会加载整个缓 ...
- html基本选择符的使用
一.选择符在运用在CSS设计样式时对HTML的指定有至关重要的作用! 二.研究 普通选择符: 1.类型选择符:它可以选择同一个类型的元素! 例如:h1,h2 { color: ...
- 安卓中級教程(8):pathbutton中的animation.java研究(1)
src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; i ...