[LeetCode 解题报告] Reverse Integer
来源:互联网 发布:mac自带画图工具 编辑:程序博客网 时间:2024/05/28 04:53
1. 问题描述:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
2. 分析:
LeetCode中easy难度中通过率最低的题,动手去做发现果然陷阱重重。
首先总结一下如何通过一个整数的各位数字将其恢复的两种方法:
例如,给定一串数字
int weight = 1, num = 0;for (int i = 0; i < digits.size(); ++i){ num += digit[i] * weight; weight *= 10;}
第二种方法是从高位到低位进行计算:
int num = 0;for (int i = digits.size() - 1; i >= 0; --i) num = num * 10 + digits;
这就是本题的核心方法。但仍有一些细节需要注意:
有了样例的提示,符号的问题基本上都能考虑到。也就是说,应该用绝对值来模10提取每一位。但溢出的问题如果之前没有处理经验的话,很容易搞错。
比如,我一开始受到了判断加法溢出的方法的启发(误导?),认为绝对值在reverse后计算出的值如果是负数,那么就产生了溢出。的确,出现了这种情况肯定是发生了溢出,但并不是所有的溢出都会导致绝对值变成负数,比如测试样例1534236469,9646324351溢出后的结果是1056389759,仍然是一个正数!所以需要其他判断溢出的方法。错误版本的代码如下:
int Solution::reverse(int x){ int sign = (x >= 0) ? 0 : 1; int absolute = abs(x); vector<int> digits; while (absolute) { digits.push_back(absolute % 10); absolute /= 10; } int weight = 1; int result = 0; for (int i = digits.size() - 1; i >= 0; --i) { cout << digits[i] << " " << weight << endl; result += weight * digits[i]; weight *= 10; } if (result < 0) return 0; return (sign == 0) ? result : -result;}
http://www.cnblogs.com/grandyang/p/4125588.html 给出了一个巧妙的方法。我们采用从高位到低位的方法计算反转后的整数值,也就是说,把当前结果乘以10后(相当于向左移动一位)加上下一位数字。如果发生了溢出,那么新的数除以10的结果就不会跟移位之前的结果一致。这种判断溢出的方法较之于使用long long 再和INT_MAX进行比较,要优雅不少。修改后的函数如下:
int Solution::reverse(int x){ int sign = (x >= 0) ? 0 : 1; int absolute = abs(x); int result = 0; int temp; while (absolute) { temp = result * 10 + absolute % 10; if (temp / 10 != result) return 0; result = temp; absolute /= 10; } return (sign == 0) ? result : -result;}
进而顺利AC。
3. 完整代码:
#include <vector>#include <iostream>using namespace std;class Solution {public: int reverse(int x);};int Solution::reverse(int x){ int sign = (x >= 0) ? 0 : 1; int absolute = abs(x); int result = 0; int temp; while (absolute) { temp = result * 10 + absolute % 10; if (temp / 10 != result) return 0; result = temp; absolute /= 10; } return (sign == 0) ? result : -result;}void test(){ Solution inst; vector<int> input = { 0, 123, -123, -100, 1000000003, 1534236469 }; vector<int> output = { 0, 321, -321, -1, 0, 0}; int res; for (int i = 0; i < input.size(); ++i) { res = inst.reverse(input[i]); if (res == output[i]) cout << "yes" << endl; else { cout << "no" << "\t" << res << endl; } }}int main(){ test(); return 0;}
- LeetCode解题报告---Reverse Integer
- LeetCode-Reverse Integer-解题报告
- [LeetCode] Reverse Integer 解题报告
- [LeetCode] Reverse Integer 解题报告
- [LeetCode 解题报告] Reverse Integer
- [leetcode] 7. Reverse Integer 解题报告
- LeetCode 7 Reverse Integer 解题报告
- [Leetcode] 7. Reverse Integer 解题报告
- leetcode解题报告7. Reverse Integer
- [LeetCode 解题报告]007.Reverse Integer
- [leetcode]7. Reverse Integer@Java解题报告
- Reverse Integer解题报告
- Reverse Integer 解题报告
- LeetcodeProblem7 Reverse Integer解题报告
- 【LeetCode】Reverse Integer解题笔记
- 【Leetcode】7.Reverse Integer 解题
- [Leetcode]解题文档- Reverse Integer
- LeetCode解题-#7-Reverse Integer
- 设备相关工具类
- __cplusplus 使用的一种场合
- 【OPNET学习系列文章之3】两发一收
- [Leetcode] 233. Number of Digit One 解题报告
- Redis学习(二)Jedis入门
- [LeetCode 解题报告] Reverse Integer
- hadoop2.8.0 + jdk1.7 + windows7(不用linux环境) 搭建与异常处理
- LeetCode--Palindrome Number
- 如何禁用 Mac OS X 的系统完整性保护
- kinect-点云数据、深度数据&ros中相关主题
- Jmeter测试报告优化
- mybatis入门基础(八)-----查询缓存
- 3DSystems.Geomagic.Sculpt.2016.2.62 Ansys.OptiSLang.5.2.0.38449 Ansys.Products.18.0 win+linux Aspen.
- Hive 快速入门