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

  1. LeetCode 397. Integer Replacement

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

  2. Week3 - 397. Integer Replacement

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

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

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

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

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

  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. 397. Integer Replacement

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

  9. 397 Integer Replacement 整数替换

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

随机推荐

  1. MongoDB和pymongo的CURD

    一.mongodb 1.介绍 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之 ...

  2. Android N和O中使用adb shell dpm set-device-owner 'com.android.cts.verifier/com.android.cts.verifier.managedprovisioning.DeviceAdminTestReceiver' setup Device Owner失败

    PC端出现如下log: D:\workspace\AndroidO\CTS\CTS_Verifier>adb shell dpm set-device-owner 'com.android.ct ...

  3. AWS设置允许root登陆

    Refer to the following to set root login: sudo -s (to become root) vi /root/.ssh/authorized_keys Del ...

  4. 树&堆

    树 什么是树? 大概像下面这样: 树的概念 树的每个点被称为节点: 连接的两个点,一个为父节点,一个为子节点,例如上图中,\(1\)是\(4\)的父节点,\(4\)是\(1\)的子节点: 没有父节点的 ...

  5. Yii2.0 安装使用报错:yii\web\Request::cookieValidationKey must be configured with a secret key.

    下载了Yii2.0的basic版,配置好apache之后,浏览器访问,出现如下错误: Invalid Configuration – yii\base\InvalidConfigException y ...

  6. Linux端口被占用的解决(附Python专版)

    先说一般情况的解决: lsof -i:8000 查出PID,然后 kill掉程序,接着就可以了 软件重启之后绑定没有释放,lsof -i:8080也查不出来占用的情况 再来个长连接版Python解决法 ...

  7. Python之——CentOS 6.5安装Python2.7.14

    Python之——CentOS 6.5安装Python2.7.14   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/l1028386804/art ...

  8. spring boot junit controller

    MockMvc 来自Spring Test,它允许您通过一组方便的builder类向 DispatcherServlet 发送HTTP请求,并对结果作出断言.请注意,@AutoConfigureMoc ...

  9. Zabbix通过Orabbix监控Oracle数据库

    一.背景 公司业务使用的是一直Oracle数据库,因为多次出现表空间满的时候不能及时发现,每次都是业务组的人员通知处理,这样下来DBA这边就比较被动,所以老大要求监控表空间剩余大小并且当剩余过小时能够 ...

  10. 我眼里K-Means算法

    在我眼里一切都是那么简单,复杂的我也看不懂,最讨厌那些复杂的人际关系,唉,像孩子一样交流不好吗. 学习K-Means算法时,会让我想起三国志这个游戏,界面是一张中国地图,诸侯分立,各自为据.但是游戏开 ...