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 ...
随机推荐
- mssql 2008 游标 临时表 作业批处理失败问题
在项目中,写过一个作业,当订单超过1个小时未支付,则取消订单,同时返回订单使用的积分,优惠券,余额. 经过调试发现,作业存储过程中,使用了游标,而且使用了临时表,在游标第一次创建临时表时,没有问题,但 ...
- Latex 数学符号
本文完全转自 <常用数学符号的 LaTeX 表示方法>,在此转载仅仅为了便于查阅,谨向原作者致以崇高的敬意. 常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LA ...
- PHP mysql_fetch_array() 函数
PHP mysql_fetch_array() 函数 从结果集中取得一行作为关联数组,或数字数组,或二者兼有.返回根据结果集取得的行生成的数组,如果没有更多行则返回false. 提示:有很重要的一点必 ...
- jsonp解决跨域
ajax请求: $.ajax({ type: "get",//必须使用get方式 async: false, url: "htt ...
- java封装性之private
public class TestDemo{ public static void main(String args[]){ Person perA= new Person(); perA.setNa ...
- Kali Linux 2.0配置并安装常用的工具
Kali Linux 前身是著名渗透测试系统BackTrack ,是一个基于 Debian 的 Linux 发行版,包含很多安全和取证方面的相关工具. Kali Linux 2.0是基于Debian ...
- LeetCode-Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- REGEX.C GNU 提取过滤数据
今天被@SVCHAO 勾起兴趣来了.. 有把正则表达式兴趣捡起来了,试了下notepad++基本上语法倒是没有忘记,不过如果是用在嵌入式的方案的话,似乎还是有点费劲的. 先mark一个基础语法. 单 ...
- word使用技巧-批量删除图片技巧
通过查找替换方法:ctrl+h,查找输入^g,替换输入空,然后替换即可. 今天看到一同事写的文档,发现里面很多word基础功能都不会用,比如同一级的标题居然有好几个样式,并且会级别搞错:列表里的数字居 ...
- [转]Windows 下的进程间通讯及数据共享
http://blog.codingnow.com/2005/10/interprocess_communications.html Windows 下有很多方法实现进程间通讯,比如用 socket, ...