LEETCODE: Reverse Integer
2014-12-10 17:17
387 查看
这个题目比较简单! 但是我发现居然没有通过测试,这里就有一个问题。
下面的代码是copy的,当然我自己写的思路和它是一样的。
当然现在有个错误的结果,因为溢出了。
所以,要考虑溢出。
下面的代码是copy的,当然我自己写的思路和它是一样的。
class Solution { public: int reverse(int x) { int res = 0; while (x != 0) { // don't care positive or negetive res = res * 10 + x % 10; // get lowest digit then multi 10 x /= 10; } return res; } };
当然现在有个错误的结果,因为溢出了。
Submission Result: Wrong Answer
Input: | 1534236469 |
Output: | 1056389759 |
Expected: | 0 |
int reverse(int x) { int res = 0; int highlimit = 0x7fffffff / 10; int lowlimit = int(0x80000000) / 10; while (x != 0) { // don't care positive or negetive if(res > highlimit || res < lowlimit) return 0; res = res * 10 + x % 10; // get lowest digit then multi 10 x /= 10; } return res; }
相关文章推荐
- 【leetcode】Reverse Integer整数反转----Java代码实现
- [Leetcode] 7 - Reverse Integer
- Reverse Integer LeetCode 第七题
- LeetCode编程练习 - Reverse Integer学习心得
- Leetcode Reverse Integer
- LeetCode:Reverse Integer
- LeetCode:Reverse Integer
- LeetCode之Reverse Integer
- leetcode: reverse-integer
- LeetCode之Reverse Integer
- [Leetcode] Reverse Integer
- LeetCode 7 — Reverse Integer(C++ Java Python)
- LeetCode : 7 Reverse Integer C++
- LeetCode: Reverse Integer 解题报告
- LeetCode : Reverse Integer
- leetcode 7 Reverse Integer
- LeetCode-7(Reverse Integer)
- LeetCode--Reverse Integer (Cpp,Java,Python)
- Leetcode Reverse Integer
- leetcode: Reverse Integer