[LintCode]413.反转整数
2017-10-05 23:16
225 查看
将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。
样例
给定
给定
样例
给定
x = 123,返回
321
给定
x = -123,返回
-321
class Solution { public: /* * @param n: the integer to be reversed * @return: the reversed integer */ int reverseInteger(int n) { long long res = 0; //不断求余和乘以十,注意溢出 while (n != 0) { res = 10 * res + n % 10; n /= 10; } //使用long来保存可能溢出的结果,再与最大/最小整数相比较 return (res < INT_MIN || res > INT_MAX) ? 0 : res; } };
相关文章推荐
- LintCode - 413.反转整数
- LintCode 第413题 反转整数 【整数】
- LintCode入门-37-反转一个3位整数
- lintcode反转整数
- lintcode ----反转整数
- 反转整数-LintCode
- Lintcode 反转整数
- lintcode,反转整数
- 按位反转无符号整数问题bitoperate函数(转)
- [LintCode] Sort Integers 整数排序
- 【LintCode-655】大整数加法(Java实现)
- lintcode_整数排序 II
- Leetcode Reverse Integer 反转整数
- 整数反转
- LintCode 第175题 反转二叉树
- 反转整数
- lintcode 463.整数排序
- [LintCode] Integer to Roman 整数转化成罗马数字
- 整数反转
- Lintcode 54. 转换字符串到整数