Reverse Integer [LeetCode]
2013-11-03 06:11
411 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
Solution: be careful about corner case, like INT_MIN, 0, and -INT_MIN is not equals to INT_MAX. Here is the definition of them in limits.h
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
Solution: be careful about corner case, like INT_MIN, 0, and -INT_MIN is not equals to INT_MAX. Here is the definition of them in limits.h
#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */ #define INT_MAX 2147483647 /* maximum (signed) int value */
int reversePostiveInt(int x){ vector<int> digits; while(true) { if(x < 10 ){ digits.push_back(x); break; }else { int remainder = x % 10; int quotient = x / 10; digits.push_back(remainder); x = quotient; } } // reverse output long long int ret = 0; for(int i = 0; i < digits.size(); i ++) { ret = ret * 10 + digits[i]; if(ret > INT_MAX){ ret = INT_MAX; break; } } return (int) ret; } int reverse(int x) { if(x == 0 ) return 0; if(x > 0) { return reversePostiveInt(x); }else if( x == INT_MIN){ //overflow return INT_MIN; }else if ( x > INT_MIN) { return -reversePostiveInt(-x); } }
相关文章推荐
- leetcode:Reverse Integer 及Palindrome Number
- 每日一练之Reverse Integer[LeetCode No.7]-翻转整数
- leetcode——Reverse Integer
- leetcode——7—— Reverse Integer可仿溢出
- 算法分析与设计课程——LeetCode刷题之Reverse Integer
- Leetcode - Reverse Integer
- LeetCode之“数学”:Reverse Integer && Reverse Bits
- Reverse Integer--LeetCode
- LeetCode每日一练(7) Reverse Integer C++版
- LeetCode (Reverse Integer)
- LeetCode: Reverse Integer
- Reverse Integer--LeetCode
- leetcode之Reverse Integer
- LeetCode Reverse Integer
- Leetcode:reverse_integer
- LeetCode7-Integer Reverse
- leetcode Reverse Integer java基本编程学习笔记(一)
- leetcode — reverse-integer
- leetcode Reverse Integer(python)
- leetcode:Reverse Integer【Python版】