leetcode---reverse-integer---复杂度
2018-01-27 10:21
274 查看
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).
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) { long long ans = 0; while(x) { ans = ans * 10 + x % 10; x /= 10; } return ans > INT_MAX ? 0 : ans; } };
相关文章推荐
- leetcode Reverse Integer python
- Reverse Integer Leetcode Python Java
- LeetCode Reverse Integer C++
- LeetCode Palindrome Number && Reverse Integer 解法集合
- LeetCode-7 Reverse Integer(倒置整型数)
- Leetcode 7 Reverse Integer 反转数字
- LeetCode(7)Reverse Integer
- 【LeetCode】Reverse Integer解题笔记
- 【leetcode-7】Reverse Integer(C语言)
- LeetCode Reverse Integer
- LeetCode 第 7 题(Reverse Integer)
- Reverse Integer -leetcode
- [LeetCode]Reverse Integer解题报告
- 【leetcode-7】Reverse Integer(C语言)
- Leetcode [Reverse Integer]
- Reverse Integer | LeetCode
- LeetCode Reverse Integer
- leetcode--reverse_integer
- 刷LeetCode(7)——Reverse Integer
- LeetCode 第7题 Reverse Integer (easy)——python