leetcode 397. Integer Replacement
来源:互联网 发布:eclipse端口号 编辑:程序博客网 时间:2024/06/05 14:33
开始想的是采用类似于广度搜索图的算法计算,发现它超时了。
超时的版本如下:
class Solution {public: typedef struct trace { int num; int residual; }TRACE; int integerReplacement(int n) { int rel = INT_MAX; int ini = 0; queue<TRACE> remain; cout << rel << endl; while(1) { if(!ini) { if(n == 1) return 0; if(n&1) { TRACE t_h = {n+1, 1}; TRACE t_l = {n-1, 1}; remain.push(t_h); remain.push(t_l); } else { TRACE t = {n >> 1, 1}; remain.push(t); } ini = 1; } else { int cur_size = remain.size(); cout << cur_size << endl; for(int i = 0; i < cur_size; i++) { TRACE t_top = remain.front(); remain.pop(); cout << t_top.num << " " << t_top.residual << endl; if(t_top.num == 1 && t_top.residual < rel) { rel = t_top.residual; return rel; } else if(t_top.num & 1) { TRACE t_h = {t_top.num+1, t_top.residual+1}; TRACE t_l = {t_top.num-1, t_top.residual+1}; remain.push(t_h); remain.push(t_l); } else { TRACE t = {t_top.num >> 1, t_top.residual+1}; remain.push(t); } } if(remain.empty()) return rel; } } return rel; }};
后来我觉得这里面可能有问题,原来是cout占了不少时间。后来我运行,发现输入的n居然有可能是最大的有符号的正整数,所以我进行一个类型转换。下面就是可以的代码。
class Solution {public: typedef struct trace { unsigned int num; int residual; }TRACE; int integerReplacement(int n) { int rel = INT_MAX; int ini = 0; queue<TRACE> remain; cout << rel << endl; while(1) { if(!ini) { if(n == 1) return 0; if(n&1) { TRACE t_h = {(unsigned int)(n+1), 1}; remain.push(t_h); TRACE t_l = {n-1, 1}; remain.push(t_l); } else { TRACE t = {n >> 1, 1}; remain.push(t); } ini = 1; } else { int cur_size = remain.size(); // cout << cur_size << endl; for(int i = 0; i < cur_size; i++) { TRACE t_top = remain.front(); remain.pop(); // cout << t_top.num << " " << t_top.residual << endl; if(t_top.num == 1 && t_top.residual < rel) { rel = t_top.residual; return rel; } else if(t_top.num & 1) { TRACE t_h = {t_top.num+1, t_top.residual+1}; TRACE t_l = {t_top.num-1, t_top.residual+1}; remain.push(t_h); remain.push(t_l); } else { int tmp = t_top.num >> 1; TRACE t = {tmp, t_top.residual+1}; remain.push(t); } } if(remain.empty()) return rel; } } return rel; }};
0 0
- 【Leetcode】397. Integer Replacement
- [leetcode]397. Integer Replacement
- LeetCode 397. Integer Replacement
- LeetCode 397. Integer Replacement
- LeetCode 397. Integer Replacement
- LeetCode 397. Integer Replacement
- LeetCode 397. Integer Replacement
- leetcode 397. Integer Replacement
- 【LeetCode】 397. Integer Replacement
- [LeetCode]397. Integer Replacement
- Leetcode-397. Integer Replacement
- [leetcode]397. Integer Replacement
- [leetcode]397. Integer Replacement
- LeetCode—397. Integer Replacement
- Leetcode 397. Integer Replacement[medium]
- 【Leetcode】397. Integer Replacement QuestionEditorial Solution【E】
- [leetcode] 397. Integer Replacement 解题报告
- Leetcode 397. Integer Replacement (Easy) (cpp)
- ios 计算有行间距的label的高度
- WebRTC学习之八:函数的异步执行
- 用Groovy思考 第二章 Groovy语言的关键特征
- Mac上使用Idea发热严重
- jQuery ajax - serialize() 方法
- leetcode 397. Integer Replacement
- 关于double转string出现科学计数法的问题
- 多进程与多线程的区别与选择(转强力推荐)
- 手把手教你使用Git
- linux基础之completion
- react native 自定义基础组件
- 滑动 CollectionView 图片混乱问题
- GOF23的一些总结(十九)
- CTF解题笔记(1)