Reverse Integer
2015-07-07 23:51
344 查看
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.
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.
class Solution { public int reverse(Integer x) { long result=0; long factor = (x>=0)?1:-1; long temp = x*factor; while (temp>0) { long a = temp%10; result = result*10+a; temp=temp/10; } if(result>Integer.MAX_VALUE){ return 0; } return (int)(result*factor); } };
相关文章推荐
- 基于win平台的高性能服务器底层通信模型设计(1)
- 《Java程序》将数字进行倒序输出
- JAVA中的正则表达式--待续
- Glance的手工搭建
- clean code 读书笔记
- android动画中fillBefore以及fillAfter在xml中设置无效的问题
- Uplooking notes 02day
- 使用JSONObject和JSONArray
- 拼包函数及网络封包的异常处理(含代码)
- php常用函数收集并附解释说明
- Socket编程 (异步通讯,解决Udp丢包)
- 75.字符串正序和反序连接
- 解析如何改善和优化 Web 服务器性能
- 阿里巴巴产品实习生第8天
- 欢迎使用CSDN-markdown编辑器
- return
- 判断IP地址是否有效
- 最短距离
- Linux学习笔记(5)-用户管理
- GCC依赖库顺序问题