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 + 1
orn - 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 ...
随机推荐
- MySQL字符串处理函数的几种常见用法
1.字符串大小写转化: (1).将tbl_student表的user_name字段所有小写字母,替换为大写: update tbl_student set user_name=UPPER(user_n ...
- jquery .attr()
在JS中设置节点的属性与属性值用到setAttribute(),获得节点的属性与属性值用到getAttribute(),而在jquery中,用一个attr()就可以全部搞定了,赞一个先 ^^ jque ...
- 如何换ionic里面的图标
(转自http://www.zcool.com.cn/article/ZMTM4NDQw.html) 第一步,你把你的项目的SVG文件跟你自己做的图标的SVG文件都上到icomoon.io/app(上 ...
- C++ STL泛型编程——在ACM中的运用
学习过C++的朋友们应该对STL和泛型编程这两个名词不会陌生.两者之间的关系不言而喻,泛型编程的思想促使了STL的诞生,而STL则很好地体现了泛型编程这种思想.这次想简单说一下STL在ACM中的一些应 ...
- Myeclipse非正常关闭出现问题
Could not create the view: An unexpected exception was thrown. 解决办法: 关闭myeclipse 原来工作空间的.metadata文件夹 ...
- Javascript.//DOM
文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.Document Object Model的历史可以追溯至1990年代后期微 ...
- linux命令之三
0102 文档查阅指令 cat tac nl 简单查阅,-n 可显示行 more, less less is more 查询大文件,可分页. head tail 从头尾看.-n 限制行数. taif ...
- leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- apache https 伪静态
今天很是郁闷,配置了一个https成功了,但是伪静态不成功,经过多方面查看资料终于配置成功了: <VirtualHost *:443> DocumentRoot /var/www/juba ...
- hiho一下20周 线段树的区间修改
线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了 ...