lintcode反转整数
2017-09-07 15:52
190 查看
反转整数
描述笔记
数据
评测
将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。
您在真实的面试中是否遇到过这个题?
Yes
样例
给定
x = 123,返回
321
给定
x = -123,返回
-321
标签
相关题目
本题采用数学计算法而不是文本翻转,所以末尾为0的翻转不用考虑。
坑就是要考虑溢出啊!!一开始还没搞明白怎么溢出。int型的数值范围是 -2147483648~2147483647, 那么如果我们要翻转 1000000009 这个在范围内的数得到 9000000001,而翻转后的数就超过了范围。用INT_MAX 表示int最大值
class Solution { public: /* * @param n: the integer to be reversed * @return: the reversed integer */ int reverseInteger(int n) { // write your code here if(n==0) return 0; int sum=0; while(n!=0){ if(abs(sum)>INT_MAX/10)//abs为取绝对值,INT_MAX是int最大值 return 0; sum=sum*10+n%10; n=n/10; } return sum; } };
反转整数
描述笔记
数据
评测
总耗时: 382 ms
100% 数据通过测试.
太牛了,把AC的喜悦分享给你的朋友吧!
反转整数
描述笔记
数据
评测
总耗时: 382 ms
100% 数据通过测试.
太牛了,把AC的喜悦分享给你的朋友吧!
7
错误的代码也能通过?
为LintCode提供数据吧!
提供数据
反转整数
描述笔记
数据
评测
总耗时: 382 ms
100% 数据通过测试.
太牛了,把AC的喜悦分享给你的朋友吧!
7
错误的代码也能通过?
为LintCode提供数据吧!
提供数据
相关文章推荐
- 反转整数-LintCode
- [LintCode]413.反转整数
- lintcode ----反转整数
- Lintcode 反转整数
- lintcode,反转整数
- LintCode 第413题 反转整数 【整数】
- LintCode - 413.反转整数
- LintCode入门-37-反转一个3位整数
- 【LintCode】整数排序II
- LintCode:两个整数相除
- (LeetCode)反转整数
- leetcode_7. Reverse Integer 反转整数的各个数字
- LeetCode 7. Reverse Integer(反转整数)
- LeetCode7——Reverse Integer(将一个整数反转,注意溢出的处理)
- 位运算实现反转字节和求二进制整数中1的个数(无循环)
- 按位反转整数问题
- 编程之美--反转整数二进制位
- 算法之反转整数
- LintCode笔记(5)——整数转罗马数字
- Lintcode - 整数排序