397. Integer Replacement

QuestionEditorial Solution

My Submissions

 
  • Total Accepted: 5878
  • Total Submissions: 21519
  • Difficulty: Easy

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

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的更多相关文章

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

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

  2. Week3 - 397. Integer Replacement

    Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...

  3. 397 Integer Replacement 整数替换

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

  4. 397. Integer Replacement

    先正统做法. public class Solution { public int integerReplacement(int n) { if(n == 1) return 0; int res = ...

  5. [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 ...

  6. 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 ...

  7. LeetCode——Integer Replacement

    Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...

  8. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  9. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

随机推荐

  1. C++模板实例化

    深入理解C++中第七章提到模板实例化参数的选择:函数的决议结果只和函数参数有关和返回值无关.记录一下. 测试程序如下: #include <iostream> using namespac ...

  2. 如何成为python高手

    本文是从 How to become a proficient Python programmer 这篇文章翻译而来. 这篇文章主要是对我收集的一些文章的摘要.因为已经有很多比我有才华的人写出了大量关 ...

  3. SqlServer 行转列(统计某年一到十二个月数据总和)

    select * from(    select sum(case MONTH(purchase_date) when '1' then SumMoney else 0 end) as January ...

  4. php 把数字1-1亿换成汉字表述,例如 150 转成 一百五十

    /* 额,重新修改了下.现在估计没什么问题了.... */ 直接上实例 写到 千亿上了. /** * @author ja颂 * 把数字1-1亿换成汉字表述,如:123->一百二十三 * @pa ...

  5. 人类大脑只开发了10%? I don't think so.

    既然程序执行时有些部分是彼此互斥的(在程序的一次执行中,执行了这部分就不会去执行另一部分),那么所谓的 人类大脑只开发了10%? 是不是其实只是程序互斥的一种体现. 而往往"智商" ...

  6. 自定义响应结构 Json格式转换 工具类

    import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterx ...

  7. cocos2dx 3.2 Touch Listen和menu回调实现截屏

    在Cocos2d-X 3.x里面,已经集成了截屏功能,单独放在utils命名空间里,实现在base/ccUtils.h文件里面.看下函数申明 /** Capture the entire screen ...

  8. VC++常用函数

    // 获取文件运行路径 void GetAppFilePath(const CString& strProjectName) { TCHAR szFileName[MAX_PATH]; HMO ...

  9. unity行为树制作AI简单例子(2)

    继续昨天的工程,给Monster添加一个空物体命名为AI,在AI添加脚本BehaviorTree,然后就可以打开行为树编辑器进行编辑了 先写好自定义的节点脚本,下面是一个寻找漫游点的行为节点脚本 us ...

  10. 《Matrix Computation 3rd》读书笔记——第3章 一般线性系统