leetcode7. Reverse Integer
2017-01-09 19:29
363 查看
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?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
主要是要注意当x=INT_MIN时x=-x , 当输入大于INT_MAX时的特殊情况
AC解
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?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
主要是要注意当x=INT_MIN时x=-x , 当输入大于INT_MAX时的特殊情况
AC解
class Solution { public: int reverse(int x) { if (x == INT_MIN) return 0; int m = x; if (x < 0) m = -x; long long result = 0; do { result = 10 * result + m % 10; m = m / 10; }while(m > 0); if (result > INT_MAX) return 0; return x > 0 ? result : -result ; } };
相关文章推荐
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
- leetcode 7. Reverse Integer
- [LeetCode]7. Reverse Integer(自学留存)
- Leetcode: 7. Reverse Integer
- Leetcode7. Reverse Integer
- 【leetcode】7. Reverse Integer(Python & C++)
- leetcode-java-7. Reverse Integer
- [LeetCode]7. Reverse Integer&190. Reverse Bits
- [LeetCode]7. Reverse Integer
- [LeetCode]7. Reverse Integer
- LeetCode 7. Reverse Integer
- LeetCode 7. Reverse Integer
- LeetCode---7. Reverse Integer
- leetcode 7. Reverse Integer
- Leetcode 7. Reverse Integer(java版)
- [LeetCode - 数学] 7. Reverse Integer
- leetcode 7. Reverse Integer
- [LeetCode] 7. Reverse Integer ☆
- leetcode 7. Reverse Integer-数字翻转
- LeetCode 7. Reverse Integer (倒转数字)