LeetCode 397. Integer Replacement
397. Integer Replacement
- Total Accepted: 5878
- Total Submissions: 21519
- Difficulty: Easy
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
Subscribe to see which companies asked this question
这个题目没思路,参考了别人的想法 网址:https://discuss.leetcode.com/topic/58334/a-couple-of-java-solutions-with-explanations
class Solution {
public:
int integerReplacement(int n) {
if(INT_MAX==n)return ; //INT_MAX是个特殊值,如果加1就越界了
//我们也可以用long long 类型的数令它等于n,如 long long N=n;然后把下面的n替换成N即可
int c = ;
while (n != ) {
if ((n & ) == ) {
n /= ;
}
else if (n == || ((n) & (<<)) == ) { //留意后两位 奇数是最后一位是1 如果倒数第二位是0那么减1 使得1的位数更少
--n;
}
else {
++n;
}
++c;
}
return c;
}
};
LeetCode 397. Integer Replacement的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- Week3 - 397. Integer Replacement
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
- 397 Integer Replacement 整数替换
给定一个正整数 n,你可以做如下操作:1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是多少?示例 ...
- 397. Integer Replacement
先正统做法. public class Solution { public int integerReplacement(int n) { if(n == 1) return 0; int res = ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- Leetcode Integer Replacement
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- LeetCode——Integer Replacement
Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
随机推荐
- folly::AtomicHashmap源码分析(二)
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ 背景 上一篇只是细致的把源码分析了一遍,而源码背后的设计思想并没有写,设计思想往往是最重要的,没有它,基本无法做整 ...
- 批量创建SO
生成一般销售订单和退货订单所要使用的BAPI不同, 一般销售订单: BAPI_SALESORDER_CREATEFROMDAT2 退货订单: BAPI_CUSTOMERRETURN_CREATE 二者 ...
- 关于cookie 取不到值的问题
当前我们设置cookie时,跨路径的话,我们会取不到设置的cookie的值 我们要添加上path路径的值,就可以啦.(经过验证) path 的设置方法:path=/; function setcook ...
- vs增加第三方依赖库的方法总结
转自http://blog.csdn.net/raodotcong/article/details/8998379 先说说普通的两种方法: 方法1: 通过设置工程配置来添加lib库. 转自网上: A ...
- github:如何获取项目源代码
github是流行的源码管理平台.这上面有很多开源的项目.作为普通的用户,如何获取这些开源项目的源码呢? 1.首先需要注册一个github账号. 2.安装windows下的git工具:下载地址: ht ...
- java的三大框架(二)---Struts2
Strtu2框架 1.控制器:ActionServlet充当控制层 2.模型层:由ActionForm及业务JavaBean实现 3.视图:用户的看到并与之交互的界面 由struts标签库和jsp ...
- linux 添加基于weblogic的nodemanager的服务
用nodemanager来添加weblogic服务启动. 1.新建一个server,命名为Server1,端口设置为7055,其他采用默认值. 2.新建一个Machine,命名为Machine1.配置 ...
- HA-0302 退役
2016年11月20日 上午12:00许,NOIP2016(复赛)结束. HA-0302正式退役.
- CentOS上yum安装JDK
转: http://blog.csdn.net/onepiecehuiyu/article/details/17189571
- 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题
1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...