lintcode-413-反转整数
2017-08-14 12:10
281 查看
413-反转整数
将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。
样例
给定 x = 123,返回 321给定 x = -123,返回 -321
标签
整数思路
利用取余和除法取出整数每一位,需要注意整数越界问题code
class Solution { public: /* * @param n: the integer to be reversed * @return: the reversed integer */ int reverseInteger(int n) { // write your code here bool isNeg = (n < 0); n = abs(n); long long res = 0; do { res *= 10; res += n % 10; n = n / 10; } while (n > 0); if (res > INT_MAX) { res = 0; } if (isNeg == true) { return -1 * res; } return res; } };
相关文章推荐
- LintCode python 小白-简单题-413-反转整数
- Lintcode反转整数
- LintCode之反转整数
- LintCode Reserve Integer 翻转整数
- lintcode,转换字符串到整数
- LintCode:整数排序
- Lintcode--1(463)--整数排序
- LintCode:转换字符串到整数
- LintCode:M-两个整数相除
- Java for LintCode 颠倒整数
- LintCode解题笔记 - 将整数A转换为B
- LintCode : 颠倒整数
- lintcode刷题——罗马数字转整数
- LintCode_464_整数排序||
- LintCode 整数排序
- lintcode:整数排序
- lintcode 容易题:reverse integer 颠倒整数
- Leet Code 7 Reverse Integer - 整数反转 - Java
- Lintcode---统计比给定整数小的数的个数
- LintCode:整数转罗马数字