本文共 657 字,大约阅读时间需要 2 分钟。
题目链接:
难度:EasyReverse digits of an integer.
Example1: x = 123, return 321 Example2: x = -123, return -321 Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
要点
本题考查的是整数相加的溢出处理,检查溢出有这么几种办法:Java
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; }};
参考:
转载地址:http://bghbm.baihongyu.com/