LeetCode Reverse Integer
2015-09-06 21:46
393 查看
题目:
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.
首先的一个问题就是如果碰到末尾是0结尾的,那么在反转过来后的数的开头就是0,那么这些个0就得去掉。
因为题目中给的数字是int类型的,而int是32位的数,例如1000000003,但是反转以后是3000000001,而这个数已经越界了,不在int的范围内了,所以这个问题得考虑,如何使得翻转过来的数不越界。考虑是用将翻转过来的是先取为long类型,然后再用类型的强制转换成int类型,如果考虑翻转过来的数大于Integer.MAX_VALUE或者是小雨Interger.MIN_VALUE,那么直接就取为0.
以下是我的代码:
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.
首先的一个问题就是如果碰到末尾是0结尾的,那么在反转过来后的数的开头就是0,那么这些个0就得去掉。
因为题目中给的数字是int类型的,而int是32位的数,例如1000000003,但是反转以后是3000000001,而这个数已经越界了,不在int的范围内了,所以这个问题得考虑,如何使得翻转过来的数不越界。考虑是用将翻转过来的是先取为long类型,然后再用类型的强制转换成int类型,如果考虑翻转过来的数大于Integer.MAX_VALUE或者是小雨Interger.MIN_VALUE,那么直接就取为0.
以下是我的代码:
public class Solution { public static int reverse(int x) { String str = ""; long sum = 0; boolean bool = false; if (x >= 0) { str = String.valueOf(x); StringBuilder strb = new StringBuilder(); for (int i = str.length() - 1; i >= 0; i--) { strb.append(str.charAt(i)); } str = strb.toString(); //System.out.println(str); sum = Long.parseLong(str); if (sum > Integer.MAX_VALUE) return 0; else return (int) sum; } else { //x = Math.abs(x); //else if(x < 0) //x = x * -1; //System.out.println(x); str = String.valueOf(x); StringBuilder strb = new StringBuilder(); for (int i = str.length() - 1; i >= 1; i--) { strb.append(str.charAt(i)); } str = strb.toString(); //System.out.println(str); sum = Long.parseLong(str); sum = -1 * sum; if (sum < Integer.MIN_VALUE) return 0; else return (int) sum; } } }中间可能有点繁琐,就是分别考虑了为非负数和负数的情况,其实可以考虑用一个标示来表示这个数是正数还是负数,然后每次只要考虑标示就可以了。
相关文章推荐
- 用NSOperation写下载队列
- Fresco 源码分析(二) Fresco客户端与服务端交互(2) Fresco.initializeDrawee()分析 续
- oc- 关键字
- 密码123456,这意味着什么?
- 关于测试程序运行时间
- 杭电OJ-2023_求平均成绩
- UIMenuController的使用,对UILabel拷贝以及定制菜单
- 扬州一大爷数落宝马横停占道 车主驾车将其撞飞
- 进出口贸易被指遭港口部门挡路收费2000亿
- 写一个标题试一试。新的开始。
- SGU106 The equation
- Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题
- 如何修改MFC对话框的窗口标题
- testlink和mantis角色整理
- 计算机网络基础知识总结
- TCP连接的11种状态
- Regionals 2014 Asia Xian(几道简单题)
- magento注册
- css制作三角形,下拉框三角形
- 摄像机模型与标定——摄像机标定