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

7 -> 6 -> 3 -> 2 -> 1
详见:https://leetcode.com/problems/integer-replacement/description/

C++:

方法一:

class Solution {
public:
int integerReplacement(int n) {
long long t = n;
int cnt = 0;
while (t > 1)
{
++cnt;
if (t & 1)
{
if ((t & 2) && (t != 3))
{
++t;
}
else
{
--t;
}
}
else
{
t >>= 1;
}
}
return cnt;
}
};

方法二:

class Solution {
public:
int integerReplacement(int n)
{
if (n == 1)
{
return 0;
}
if (n % 2 == 0)
{
return 1 + integerReplacement(n / 2);
}
else
{
long long t = n;
return 2 + min(integerReplacement((t + 1) / 2), integerReplacement((t - 1) / 2));
}
}
};

参考:https://www.cnblogs.com/grandyang/p/5873525.html

397 Integer Replacement 整数替换的更多相关文章

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

  2. LeetCode 397. Integer Replacement

    397. Integer Replacement QuestionEditorial Solution My Submissions   Total Accepted: 5878 Total Subm ...

  3. Week3 - 397. Integer Replacement

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

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

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

  5. 397. Integer Replacement

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

  6. Leetcode 397.整数替换

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

  7. Java实现 LeetCode 397 整数替换

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

  8. [Swift]LeetCode397. 整数替换 | Integer Replacement

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  9. lintcode :reverse integer 颠倒整数

    题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...

随机推荐

  1. Git Cheat Sheet 中文版

    Git Cheat Sheet 中文版 索引 配置 配置文件 创建 本地修改 搜索 提交历史 分支与标签 更新与发布 合并与重置 撤销 Git Flow 配置 列出当前配置: $ git config ...

  2. [bzoj2091][Poi2010]The Minima Game_动态规划

    The Minima Game bzoj-2091 Poi-2010 题目大意:给出N个正整数,AB两个人轮流取数,A先取.每次可以取任意多个数,直到N个数都被取走.每次获得的得分为取的数中的最小值, ...

  3. Servlet处理日期

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html: 使用Servlet的最重要的优势之一是可以使用核心Java ...

  4. Cisco网络设备命名规则

      1. CISCO 开头的产品都是路由器:2. RSP 开头的都是CISCO7500 系列产品的引擎:3. VIP 开头的产品都是CISCO 7500系列产品的多功能接口处理器模块:4. PA 开头 ...

  5. Lua迭代器

    在Lua中我们常常使用函数来描述迭代器,每次调用该函数就返回集合的下一个元素.迭代器需要保留上一次成功调用的状态和下一次成功调用的状态,可以通过闭包提供的机制来实现这个任务(闭包中的外部局部变量可以用 ...

  6. Nginx系列(五)--nginx+tomcat实现负载均衡

    Nginx占有内存少,并发能力强,其实Nginx的并发能力确实在同类型的网页伺服器中表现较好.眼下中国大陆使用Nginx站点用户有:新浪,网易,腾讯等. 本文主要是基于Nginx搭建tomcat集群. ...

  7. 【BLE】CC2541之自己定义长短按键

    本篇博文最后改动时间:2017年01月06日,11:06. 一.简单介绍 本文以SimpleBLEPeripheral为例,介绍怎样将普通IO口(P12)自己定义为长短按键,实现按键3S以内松开为短按 ...

  8. [转] Ubuntu/Linux Mint/Debian 安装 Java 8

    本PPA由webupd8制作,支持Ubuntu .04以及对应的Linux Mint版本,Oracle Java 8包提供JDK8 和 JRE8. sudo add-apt-repository pp ...

  9. Restful WebService简介

    RESTful Web Services已经渐渐開始流行, 主要是用于解决异构系统之间的通信问题.非常多站点和应用提供的API,都是基于RESTful风格的Web Services,比較就有Googl ...

  10. initial ram filesystem

    1 制作最简单的initramfs 1.1 创建init #include <stdio.h> int main() { printf ("\n"); printf ( ...