[LeetCode 007] Reverse Integer
2016-02-17 07:32
369 查看
Reverse Integer
判断是否overflow的部分注意:根据x的正负检测。
根据result不准确,我们需要检测x还剩一位数的时候result是否overflow
Implementation
public class Solution { public int reverse(int x) { int result = 0; final int MIN_THRESHOLD = Integer.MIN_VALUE / 10; final int MAX_THRESHOLD = Integer.MAX_VALUE / 10; while (x != 0) { int newResult = result * 10 + x % 10; x = x / 10; if (x > 0 && (newResult > MAX_THRESHOLD || (newResult == MAX_THRESHOLD && x > 7))) return 0; if (x < 0 && (newResult < MIN_THRESHOLD || (newResult == MIN_THRESHOLD && x < -8))) return 0; result = newResult; } return result; } }
public class Solution { public int reverse(int x) { int result = 0; while (x != 0) { int newResult = result * 10 + x % 10; x = x / 10; if (newResult / 10 != result) { return 0; } result = newResult; } return result; } }
相关文章推荐
- 【Android】8.4 让主题自适应不同的Android版本
- 【Android】8.3 自定义主题
- 【Android】8.2 动态选择和设置主题
- 【Android】8.1 主题基本用法
- OSChina 周三乱弹 —— 冰枫~这里绝对没有你想要的内容
- 地图之美(二)——帅爆了!!!
- 【Android】7.8 MyDemos项目的结构和主界面相关代码
- 【BZOJ 1010】 [HNOI2008]玩具装箱toy
- 造轮子:大型网站架构模式
- 3252: 攻略 dfs序+线段树
- 【BZOJ 2730】 [HNOI2012]矿场搭建
- 【BZOJ 1791】 [Ioi2008]Island 岛屿
- 你找到自己的路了么?
- 【BZOJ 2654】tree
- 【BZOJ 2143】 飞飞侠
- Full TrustZone exploit for MSM8974
- Unlocking the Motorola Bootloader (Feb, 2016)
- ButterKnife Zelezny 抛出 ArrayIndexOutOfBoundsException
- [Lintcode]Longest Words
- [Lintcode]Longest Increasing Continuous Subsequence