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. 自己实现ArrayList与LinkedList类

    ArrayList与LinkedList的底层实现 ArrayList内部由数组实现,LinkedList内部由链表实现. 自己动手实现ArrayList与LinkedList中一些常用方法 Arra ...

  2. [ML]机器学习书单

    https://anvaka.github.io/greview/hands-on-ml/1/

  3. Memory Layout for Multiple and Virtual Inheritance

    Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...

  4. 礼物(中国剩余定理+拓展gcd求逆元+分治=拓展Lucus)

    礼物 题意: 求\[C(n,m)\ \%\ p\] \(n,m,p\le 10^9\),且若\(p=\prod_{i=1}^{k}{p_i}^{c_i}\),则\(\forall i\in [1..k ...

  5. centos7搭建gitlab服务器、汉化

    1.下载rpm安装包 https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.8.5-ce.1.el7.x86_64.rp ...

  6. openssl命令行将pfx格式转.key和.crt文件,Apache适用

    以前使用的windows平台作为php运行的平台,感觉整体速度太慢,于是现在改为centos系统的linux平台.需要把windows中用pfx证书转为appache用到key和crt组成的证书 将* ...

  7. Node.js实战项目学习系列(2) 开发环境和调试工具

    前言 上一节让我们对Node.js有一个初步的了解,那么现在可以开始正式学习下Node.js的开发了,但是任何一门语言要设计到开发,就必须先学习开发环境以及调试.本文将主要讲解这些内容. 本文涉及到的 ...

  8. javaScript drag对象进行拖拽使用详解

    目录 drag简介 兼容性 drag事件 拖拽流程 DataTransfer对象 drag拖放桌面文件 drag实例 小结 drag简介 HMTL5提供的支持原生拖拽的实现 兼容性如何? 桌面端的支持 ...

  9. 使用/dev/poll的str_cli函数

    void str_cli(FILE *fp, int sockfd) { int stdineof; char buf[MAXLINE]; int n; int wfd; ]; struct dvpo ...

  10. 异常捕获try----catch

    如果try语句里有return,返回的是try语句块中变量值. 详细执行过程如下: 如果有返回值,就把返回值保存到局部变量中: 执行jsr指令跳到finally语句里执行: 执行完finally语句后 ...