leetcode: reverse-integer
来源:互联网 发布:算法工程师需要学什么 编辑:程序博客网 时间:2024/05/02 02:51
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) {
int r = 0;
for(;x;x/=10)
{
r = r*10+x%10;
}
return r;
}
};
0 0
- LeetCode:Reverse Integer
- LeetCode: Reverse Integer
- [Leetcode] Reverse Integer
- LeetCode : Reverse Integer
- [LeetCode] Reverse Integer
- Leetcode: Reverse Integer
- LeetCode 7 - Reverse Integer
- leetcode 104: Reverse Integer
- 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: Reverse Integer
- Leetcode: Reverse Integer
- 托管与非托管代码的区别
- Android drawable微技巧,你所不知道的drawable的那些细节
- 随机步程序
- CodeForces 711A Bus to Udayland(水题)
- 存储控制器
- leetcode: reverse-integer
- 口才学习
- 2016.8.29
- openstack源码阅读笔记2 配置与oslo_config
- linux netfilter框架中比较重要的结构体和函数
- STL模版学习(转载)
- 高级Bash脚本编程指南(1): 为什么使用shell编程
- iOS 知识点 - 前奏更新
- Java集合的遍历Iterator的方法