当前位置:网站首页>LeetCode_Recursive_Medium_397. Integer Substitution
LeetCode_Recursive_Medium_397. Integer Substitution
2022-08-06 14:05:00 【small town old street】
1.题目
给定一个正整数 n ,你可以做如下操作:
① 如果 n 是偶数,则用 n / 2 替换 n .
② 如果 n 是奇数,则可以用 n + 1或 n - 1 替换 n .
返回 n 变为 1 The minimum number of replacements required .
示例 1:
输入:n = 8
输出:3
解释:8 -> 4 -> 2 -> 1
示例 2:
输入:n = 7
输出:4
解释:7 -> 8 -> 4 -> 2 -> 1
或 7 -> 6 -> 3 -> 2 -> 1
示例 3:
输入:n = 4
输出:2
提示:
1 <= n <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/integer-replacement
2.思路
思路参考本题官方题解.
(1)递归 & 暴力穷举法
① 当 n 为偶数时,The only way to replace is,即使用 n / 2 替换 n;
② 当 n 为奇数时,有两种替换方法,即使用 n + 1或 n - 1 替换 n,Regardless of which method is used,The result after substitution must be an even number,So the next replacement operation must be n / 2,So here we can think of using two operations,将 n 变为 n + 1 2 \frac{n + 1}{2} 2n+1 和 n − 1 2 \frac{n - 1}{2} 2n−1.
注意:当 n 去 int 类型的最大值,即 n = Integer.MAX_VALUE = 231 时,n + 1 会出现整数溢出的情况,所以可以通过 ⌊ n / 2 ⌋ \lfloor {n / 2} \rfloor ⌊n/2⌋ + 1 和 ⌊ n / 2 ⌋ \lfloor {n / 2} \rfloor ⌊n/2⌋ 来分别替换 n + 1 2 \frac{n + 1}{2} 2n+1 和 n − 1 2 \frac{n - 1}{2} 2n−1,其中 ⌊ x ⌋ \lfloor x \rfloor ⌊x⌋ 表示向下取整.
==(2)记忆化搜索 ==
Memoized search can be seen asrecursion after pruning.Add memoization to the recursion of method 1,这样递归树的每一层最多只会计算两个 n 值.
3.代码实现(Java)
//思路1————递归 & 暴力穷举法
class Solution {
public int integerReplacement(int n) {
if (n == 1) {
return 0;
}
if (n % 2 == 0) {
return 1 + integerReplacement(n / 2);
}
return 2 + Math.min(integerReplacement(n / 2), integerReplacement(n / 2 + 1));
}
}
//思路2————记忆化搜索
class Solution {
Map<Integer, Integer> memo = new HashMap<>();
public int integerReplacement(int n) {
if (n == 1) {
return 0;
}
if (!memo.containsKey(n)) {
if (n % 2 == 0) {
memo.put(n, 1 + integerReplacement(n / 2));
} else {
memo.put(n, 2 + Math.min(integerReplacement(n / 2), integerReplacement(n / 2 + 1)));
}
}
return memo.get(n);
}
}
边栏推荐
- Redis installation
- redis data types and common commands
- 【安装填坑】-import win32api, sys, os ImportError: DLL load failed: 找不到指定的模块。
- Apple开发如何在设备中切换IAP(内购)沙盒测试账户?
- R语言ggplot2可视化:可视化多分类变量箱图(Box Plot)、自定义箱图箱体的填充色、添加主标题、副标题、题注信息
- LODOP.ADD_PRINT_TEXT 参数解释说明
- 【paper速读】NLNL: Negative Learning for Noisy Labels (ICCV2019)
- How to switch configuration files and deployment in microservices
- 微信小程序原生将两张图片合成一张并保存至手机中
- unity2D横版游戏教程10-场景控制
猜你喜欢

运筹说 第71期|论文速读之时间背包问题

redis data types and common commands

Rocket MQ Crash-Safe机制浅析

Redis安装

LeetCode high frequency question 78. Subset, return all possible subsets (power sets) of the array, generate all subsequences

Total Software Deployment为您的企业网络管理软件部署

HyperLynx(二)LineSim的基本操作

接口测试CURL复制以及postman的Code功能

论文解读:《DeepKla:一种基于注意力机制的深度神经网络,用于蛋白质赖氨酸乳酸化位点预测》

mosquitto使用的基本流程以及一些遇见的问题
随机推荐
Apple开发如何在设备中切换IAP(内购)沙盒测试账户?
如何用 Redis 实现分布式锁
MySQL存储引擎
R语言ggplot2可视化:可视化多分类变量箱图(Box Plot)、自定义箱图箱体的填充色、添加主标题、副标题、题注信息
剖析SGI STL内存池总结
兆骑科创双创服务平台,创新创业高层次人才引进,投融资对接
RPC 基础系列
==和equals()的区别
七夕七夕 - 怎么给女朋友送礼物攻略
从技术全景到场景实战,透析「窄带高清」的演进突破
Go 的 50 度灰: 新 Golang 开发者要注意的陷阱、技巧和常见错误
Swift如何更灵活的使用switch...case操作符
【Autosar 存储栈Memery Stack 3.存储读写流程的要求与时序】
论文解读:《iRice-MS:用于检测水稻多型翻译后修饰位点的集成 XGBoost 模型》
Win10下 QT的安装配置 (亲测可用)
LeetCode Brushing Diary: 1545. Find the Kth bit in the Nth binary string
The difference between == and equals()
Interface test CURL replication and postman's Code function
论文解读:《DeepKla:一种基于注意力机制的深度神经网络,用于蛋白质赖氨酸乳酸化位点预测》
Rocket MQ Crash-Safe机制浅析