Integer Replacement
https://leetcode.com/problems/integer-replacement/#/solutions
这题是一道典型的搜索问题,我采用广度搜索,可以直接输出最短路径。这题的testcase 貌似有bug,在n == INT_MAX 也就是2^31 - 1 的时候最优解是32,我觉得应该是33。在代码里针对这个数字adhoc 一下,直接返回32.
- void replaceIter(int &level, vector<vector<int> *> &all) {
- vector<int> *k = all.at(level % );
- if (k->size() == ) {
- return;
- }
- while (k->size() != ) {
- int n = k->back();
- if (n == ) return;
- k->pop_back();
- bool even = (n % == );
- bool pOverflow = false;
- bool mOverflow = false;
- int over2 = n / ;
- int plus1 = n + ;
- int minus1 = n - ;
- if (n > && plus1 < ) pOverflow = true;
- if (n < && minus1 >= ) mOverflow = true;
- vector<int> *nk = all.at((level + ) % );
- if (even) {
- if (over2 == ) nk->push_back();
- else nk->push_back(over2);
- } else {
- if (!pOverflow) nk->push_back(plus1);
- if (!mOverflow) nk->push_back(minus1);
- }
- }
- level++;
- replaceIter(level, all);
- }
- int integerReplacement(int n) {
- int level = ;
- vector<int> k = {n};
- vector<int> nk;
- vector<vector<int> *> all = {&k, &nk};
- replaceIter(level, all);
- return level;
- }
Integer Replacement的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- LeetCode——Integer Replacement
Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...
- 397. Integer Replacement
先正统做法. public class Solution { public int integerReplacement(int n) { if(n == 1) return 0; int res = ...
- 397 Integer Replacement 整数替换
给定一个正整数 n,你可以做如下操作:1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是多少?示例 ...
随机推荐
- [模板] 杜教筛 && bzoj3944-Sum
杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...
- [LOJ10121] 与众不同
题目类型:\(DP\)+\(RMQ\) 传送门:>Here< 题意:给定一个长度为\(N\)的序列,并给出\(M\)次询问.询问区间\([L,R]\)内的最长完美序列.所谓完美序列就是指连 ...
- 团体程序设计天梯赛(CCCC) L3019 代码排版 方法与编译原理密切相关,只有一个测试点段错误
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- 利用DOS命令窗口进行Mail通信(二)
一:SMTP协议(对邮件进行发送) <SP>代表空格,<CRLF>代表回车和换行 SMTP命令格式 说明 ehlo<SP><domain><CRL ...
- 使用C语言中qsort()函数对浮点型数组无法成功排序的问题
一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...
- Elasticsearch6.3.2启动过程源码阅读记录
Elasticsearch6.3.2启动过程源码阅读记录 网上有很多关于es的源码分析,觉得自己技术深度还不够,所以这些文章只是看源码过程中的一个笔记,谈不上分析. 整个启动过程以类名.方法名,按顺序 ...
- .NET面试题系列(十七)前端面试
JavaScript js如何实现继承 CSS 行内元素和块状元素的区别 CSS让2个DIV在同一行显示的解决方法 在CSS中,div属于块级元素,每个块级元素默认占一行高度,一行内添加一个块级 ...
- Richard Sabey于2004年给出了由123456789各出现一次的e的估计
- [物理学与PDEs]第1章第3节 真空中的 Maxwell 方程组, Lorentz 力 3.1 真空中的 Maxwell 方程组
1.稍微修正以前局部使用的方程组可以得到真空中的 Maxwell 方程组: $$\beex \bea \Div {\bf E}&=\cfrac{\rho}{\ve_0},\\ \rot{\bf ...
- [译]Ocelot - Getting Started
原文 Ocelot专为.NET Core而设计. .NET Core 2.1 安装 首先需要创建一个netstandard2.0项目,然后再通过nuget安装. Install-Package Oce ...