Reverse Integer(java)
2016-07-03 15:19
621 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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.
这道题思路非常简单,就是按照数字位反转过来就可以,基本数字操作。但是这种题的考察重点并不在于问题本身,越是简单的题目越要注意细节,一般来说整数的处理问题要注意的有两点,一点是符号,另一点是整数越界问题。
上面的代码为了后面方便处理,先将数字转为正数。注意Integer.MIN_VALUE的绝对值是比Integer.MAX_VALUE大1的,所以经常要单独处理。如果不先转为正数也可以,只是在后面要对符号进行一下判断。这种题目考察的就是数字的基本处理,面试的时候尽量不能错,而且对于corner
case要尽量进行考虑,一般来说都是面试的第一道门槛。
Example1: x = 123, return 321
Example2: x = -123, return -321
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.
这道题思路非常简单,就是按照数字位反转过来就可以,基本数字操作。但是这种题的考察重点并不在于问题本身,越是简单的题目越要注意细节,一般来说整数的处理问题要注意的有两点,一点是符号,另一点是整数越界问题。
上面的代码为了后面方便处理,先将数字转为正数。注意Integer.MIN_VALUE的绝对值是比Integer.MAX_VALUE大1的,所以经常要单独处理。如果不先转为正数也可以,只是在后面要对符号进行一下判断。这种题目考察的就是数字的基本处理,面试的时候尽量不能错,而且对于corner
case要尽量进行考虑,一般来说都是面试的第一道门槛。
相关文章推荐
- Java类型信息与用反射机制编写通用的Excel导入导出
- JAVA---udp数据的发送与接收
- Java编程题目-4:数组操作问题
- quartz集群分布式(并发)部署解决方案-Spring
- JavaWeb概述(二)
- Java中public,private,protected,和默认的区别
- myeclipse中的svn的安装问题
- Java 中new Date()之hashCode
- Spring MVC 拦截器使用及实现
- WebService java实现简单实例
- MyBatis Spring整合配置映射接口类与映射xml文件
- 整合Struts2,Hibernate和Spring的一个简单例子
- SpringMVC 重定向
- 2015蓝桥杯决赛Java A组 第三题--显示二叉树
- Java 集合框架:HashMap
- CountDownLatch、CyclicBarrier和Semaphore
- 安装配置JDK1.7
- Two Sum(java)
- Java中常用的代码汇总
- JavaSE_关键字