Reverse Integer
来源:互联网 发布:mac如何ping ip地址 编辑:程序博客网 时间:2024/05/16 08:38
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.
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); } };
0 0
- Reverse Integer
- Reverse Integer
- Reverse Integer
- reverse integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- Reverse Integer
- reverse Integer
- Reverse Integer
- Reverse integer
- Reverse Integer
- Reverse Integer
- 基于win平台的高性能服务器底层通信模型设计(1)
- 有道云笔记
- 《Java程序》将数字进行倒序输出
- 批量获取主机名对应的ip地址
- 聆听中国—一款收音机类App的前世今生
- Reverse Integer
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)
- Hibernate与Mybatis对比
- 数据表的基本操作(一)
- 黑马程序员——JAVA笔记——集合框架3——map
- 经过一年时间的沉淀 再次回首 TCP Socket服务器编程
- 线程的学习(一)
- Design Pattern Explained 读书笔记一 重新认识面向对象|规范使用UML
- Swift基本使用-数据类型(一)