397 Integer Replacement 整数替换
给定一个正整数 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 整数替换的更多相关文章
- [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 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- Week3 - 397. Integer Replacement
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 397. Integer Replacement
先正统做法. public class Solution { public int integerReplacement(int n) { if(n == 1) return 0; int res = ...
- Leetcode 397.整数替换
整数替换 给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是 ...
- Java实现 LeetCode 397 整数替换
397. 整数替换 给定一个正整数 n,你可以做如下操作: 如果 n 是偶数,则用 n / 2替换 n. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n. n 变为 1 所需的最小替换次数 ...
- [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 ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
随机推荐
- 不错的 iOS 工具
1.LSUnusedResources,移除不用图片资源
- NOIP2013火柴排队[逆序对]
题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...
- Mac os装软件时提示显示需要安装旧Java SE 6运行环境解决办法
这个时Java版本的问题,换用合适的低版本即可,下面是官方的 https://support.apple.com/kb/DL1572?viewlocale=zh_CN&locale=en_US ...
- 数据结构之图 Part2 - 2
邻接表 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...
- C++四种强制类型转换详解
什么是类型转换? 类型转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式.为了类型转换一个简单对象为另一个对象你会使用传统的类型转换操作符. C与C++的类型转换 //C中: //复 ...
- PHP使用ueditor上传配置
引入 按照ueditor官网demo, 引入好ueditor之后, 默认是不能进行上传操作的 在上传时,在上传时会有如下图提示 配置上传 在editor/php目录下,有一个config.json文件 ...
- Haproxy原理(1)
一.四层和七层负载均衡的区别 所谓的四层就是ISO参考模型中的第四层.四层负载均衡也称为四层交换机,它主要是通过分析IP层及TCP/UDP层的流量实现的基于IP加端口的负载均衡.常见的基于四层的负载均 ...
- ConchAPI | 更智能的API监控,提升团队效率
“昨天调好的API,怎么又挂了,竟然没有人发现?” “喂喂喂,你的API挂了,无法调用成功?哪里出问题了?” “这段时间的API数量越来越多了,有谁能好好理清下?” 现在服务端技术越来越讲究微服务化, ...
- LinkedList与Queue
https://blog.csdn.net/u013087513/article/details/52218725
- linux命令 常用
1.linux手动连接主机ssh '主机地址' 2.编辑 vi 查看i 编辑esc 退出编辑:wq 保存退出:q 直接退出 3. linux 防火墙 centOS7 firewall-cmd --sa ...