leetcode: Reverse Integer
2013-10-17 10:40
351 查看
http://oj.leetcode.com/problems/reverse-integer/
思路:
其实溢出什么的完全没必要考虑,因为题目根本就没要求你处理溢出的情况。
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).
思路:
其实溢出什么的完全没必要考虑,因为题目根本就没要求你处理溢出的情况。
class Solution { public: int reverse(int x) { bool minus = (x < 0); int y = 0; x = abs(x); while (x > 0) { y = y * 10 + x % 10; x /= 10; } return minus ? -y : y; } };
相关文章推荐
- LeetCode 7 - Reverse Integer
- LeetCode 7 Reverse Integer 这道题被血虐
- LeetCode:Reverse Integer
- leetcode 7: Reverse Integer
- LeetCode:Reverse Integer
- LeetCode之Reverse Integer
- 【leetcode】Reverse Integer(考虑溢出问题)
- Reverse Integer && Palindrome Number-LeetCode
- [Leetcode] Reverse Integer (Java)
- [LeetCode] Reverse Integer 翻转整数
- leetcode: Reverse Integer 菜鸟解法
- leetcode 7 Reverse Integer golang实现
- leetcode — reverse-integer
- LeetCode: Reverse Integer
- leetcode 75: Reverse digits of an integer.
- Reverse Integer LeetCode 第七题
- [LeetCode]Reverse Integer
- Reverse Integer 翻转数字@LeetCode
- LeetCode(7)Reverse Integer
- Reverse Integer Leetcode 每日一题 ↖(^ω^)↗