https://leetcode.com/problems/integer-replacement/#/solutions

这题是一道典型的搜索问题,我采用广度搜索,可以直接输出最短路径。这题的testcase 貌似有bug,在n == INT_MAX 也就是2^31 - 1  的时候最优解是32,我觉得应该是33。在代码里针对这个数字adhoc 一下,直接返回32.

  1. void replaceIter(int &level, vector<vector<int> *> &all) {
  2. vector<int> *k = all.at(level % );
  3. if (k->size() == ) {
  4. return;
  5. }
  6.  
  7. while (k->size() != ) {
  8.  
  9. int n = k->back();
  10. if (n == ) return;
  11. k->pop_back();
  12. bool even = (n % == );
  13. bool pOverflow = false;
  14. bool mOverflow = false;
  15.  
  16. int over2 = n / ;
  17. int plus1 = n + ;
  18. int minus1 = n - ;
  19.  
  20. if (n > && plus1 < ) pOverflow = true;
  21. if (n < && minus1 >= ) mOverflow = true;
  22.  
  23. vector<int> *nk = all.at((level + ) % );
  24. if (even) {
  25. if (over2 == ) nk->push_back();
  26. else nk->push_back(over2);
  27. } else {
  28. if (!pOverflow) nk->push_back(plus1);
  29. if (!mOverflow) nk->push_back(minus1);
  30. }
  31. }
  32.  
  33. level++;
  34. replaceIter(level, all);
  35. }
  36.  
  37. int integerReplacement(int n) {
  38. int level = ;
  39. vector<int> k = {n};
  40. vector<int> nk;
  41. vector<vector<int> *> all = {&k, &nk};
  42. replaceIter(level, all);
  43. return level;
  44. }

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. [模板] 杜教筛 && bzoj3944-Sum

    杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...

  2. [LOJ10121] 与众不同

    题目类型:\(DP\)+\(RMQ\) 传送门:>Here< 题意:给定一个长度为\(N\)的序列,并给出\(M\)次询问.询问区间\([L,R]\)内的最长完美序列.所谓完美序列就是指连 ...

  3. 团体程序设计天梯赛(CCCC) L3019 代码排版 方法与编译原理密切相关,只有一个测试点段错误

    团体程序设计天梯赛代码.体现代码技巧,比赛技巧.  https://github.com/congmingyige/cccc_code

  4. 利用DOS命令窗口进行Mail通信(二)

    一:SMTP协议(对邮件进行发送) <SP>代表空格,<CRLF>代表回车和换行 SMTP命令格式 说明 ehlo<SP><domain><CRL ...

  5. 使用C语言中qsort()函数对浮点型数组无法成功排序的问题

    一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...

  6. Elasticsearch6.3.2启动过程源码阅读记录

    Elasticsearch6.3.2启动过程源码阅读记录 网上有很多关于es的源码分析,觉得自己技术深度还不够,所以这些文章只是看源码过程中的一个笔记,谈不上分析. 整个启动过程以类名.方法名,按顺序 ...

  7. .NET面试题系列(十七)前端面试

    JavaScript  js如何实现继承 CSS 行内元素和块状元素的区别   CSS让2个DIV在同一行显示的解决方法 在CSS中,div属于块级元素,每个块级元素默认占一行高度,一行内添加一个块级 ...

  8. Richard Sabey于2004年给出了由123456789各出现一次的e的估计

  9. [物理学与PDEs]第1章第3节 真空中的 Maxwell 方程组, Lorentz 力 3.1 真空中的 Maxwell 方程组

    1.稍微修正以前局部使用的方程组可以得到真空中的 Maxwell 方程组: $$\beex \bea \Div {\bf E}&=\cfrac{\rho}{\ve_0},\\ \rot{\bf ...

  10. [译]Ocelot - Getting Started

    原文 Ocelot专为.NET Core而设计. .NET Core 2.1 安装 首先需要创建一个netstandard2.0项目,然后再通过nuget安装. Install-Package Oce ...