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 所需的最小替换次数是多少?示例 ...
随机推荐
- Codechef April Challenge 2019 Division 2
Maximum Remaining 题意:给n个数,取出两个数$a_{i}$,$a_{j}$,求$a_{i}\% a_{j}$取模的最大值 直接排个序,第二大(严格的第二大)模第一大就是答案了. #i ...
- socketserver和socket的补充(验证客户端合法性)
一.socket的补充 1.参数 socket.socket(family=AF_INET,type=SOCK_STREAM,proto=0,fileno=None) 参数说明: family 地址系 ...
- 打开mac上面的apache 服务器
1. apache 服务器在系统安装的时候就默认安装了 config 文件未知: /etc/apache2/httpd.conf 2. 编辑配置文件 httpd.conf 2.1 查找 Docum ...
- antd的Tree控件实现点击展开功能
antd 的 Tree 控件没有提供点击展开的功能,只能通过左边的三角形实现展开和收起,没办法只好自己实现这个功能. 先看效果 如图实现的是类似 Mac 文件目录形式的结构,有箭头代表是个文件夹,点击 ...
- <五>企业级开源仓库nexus3实战应用–使用nexus3配置npm私有仓库
一两个星期之前,你如果在我跟前说起私服的事情,我大概会绕着你走,因为我对这个东西真的一窍不通.事实上也正如此,开发同学曾不止一次的跟我说公司的私服版本太旧了,许多新的依赖编译之后不会从远程仓库自动缓存 ...
- kubernetes云平台管理实战: 自动加载到负载均衡(七)
一.如何实现外界能访问 外界访问不了 1.启动svc [root@k8s-master ~]# cat myweb-svc.yaml apiVersion: v1 kind: Service meta ...
- 使用C语言中qsort()函数对浮点型数组无法成功排序的问题
一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...
- oldboy s21day11
#!/usr/bin/env python# -*- coding:utf-8 -*- # 1.列举 str.list.dict.set 中的常用方法(每种至少5个),并标注是否有返回值.'''str ...
- [再寄小读者之数学篇](2014-10-18 利用 Lagrange 中值定理求极限)
试求 $$\bex \vlm{n}n^2\sex{x^\frac{1}{n}-x^\frac{1}{n+1}},\quad x>0. \eex$$ 解答: $$\beex \bea \mbox{ ...
- 常用window命令
1. 关闭端口占用程序 先查看端口(8080)占用程序 netstat -ano | findstr 显示结果如下 TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 1066 ...