【LeetCode】7. 整数反转
2019-02-20 13:56
351 查看
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
[code]输入: 123 输出: 321
示例 2:
[code]输入: -123 输出: -321
示例 3:
[code]输入: 120 输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
答案:
[code]class Solution { public int reverse(int x) { int res = 0; while (x != 0) { if (Math.abs(res) > Integer.MAX_VALUE / 10) return 0; res = res * 10 + x % 10; x /= 10; } return res; } }
相关文章推荐
- leetcode 7 使用字符串操作反转整数
- leetcode的python实现 刷题笔记7: 反转整数
- [置顶] [LeetCode] Reverse Integer 整数反转
- leetcode——Reverse Integer 反转整数数字(AC)
- LeetCode第7题:整数反转 之 先从简单刚起(C++)
- LeetCode-Python-7. 整数反转
- (LeetCode每日一刷02)反转整数
- LeetCode 7. Reverse Integer(反转整数)
- python实现leetcode-第七题-整数反转
- [leetcode 7] Reverse Integer(整数反转) c函数实现
- leetcode_7. Reverse Integer 反转整数的各个数字
- Leetcode反转整数
- Leetcode:Reverse Integer ,整数反转
- golang_算法: leetcode_字符串02-整数反转
- LeetCode题解(python)-7. 整数反转
- [Leetcode] reverse integer 反转整数
- LeetCode - 7. 反转整数
- LeetCode--Reverse Integer(整数反转)Python
- LeetCode 7 反转整数
- LeetCode里的7.反转整数问题 Java15行