7.[LeetCode]Reverse Integer
2016-08-15 23:50
323 查看
我的算法:
大神算法:
public class Solution { public int reverse(int x) { boolean isPos = (x >= 0)?true:false; x = (isPos)?x:(-x); String num = String.valueOf(x); StringBuilder numBuilder = new StringBuilder(num); numBuilder.reverse(); num = numBuilder.toString(); // 处理当 overflow 的情况,返回0 try{ x = Integer.valueOf(num); } catch(NumberFormatException e){ return 0; } if(!isPos){ x = -x; } return x; } }
大神算法:
public class Solution { public int reverse(int x) { boolean isPos = x>0; if(!isPos) x = x* -1; int ans = 0, tmp = 0; while(x>0){ if( (ans) > (Integer.MAX_VALUE/10)) return 0;// overflows ans = ans* 10 + x % 10; x /=10; } return isPos? ans: -1*ans; } }
相关文章推荐
- 【leetcode c++】Reverse Integer
- leetcode :reverse integer
- LeetCode-7:Reverse Integer
- LeetCode:Reverse Integer
- leetcode 7 Reverse Integer
- 【Leetcode】【Easy】Reverse Integer
- LeetCode刷题笔录 Reverse Integer
- LeetCode-easy-Reverse Integer
- leetcode 8. 将整数的位数反序 Reverse Integer
- LeetCode-Reverse Integer
- Reverse Integer -- LeetCode
- [LeetCode]1 Reverse Integer
- 【LeetCode with Python】 Reverse Integer
- [Leetcode]Reverse Integer
- 【leetcode】 Reverse Integer
- LeetCode 7:《Reverse Integer》
- Leetcode007 Reverse Integer
- [LeetCode]Reverse Integer
- Leetcode练习- Reverse Integer
- LeetCode题解 #7 Reverse Integer