Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

Solution: be careful about corner case, like INT_MIN, 0, and -INT_MIN is not equals to INT_MAX.  Here is the definition of them in limits.h

#define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX 2147483647 /* maximum (signed) int value */
     int reversePostiveInt(int x){
vector<int> digits;
while(true) {
if(x < ){
digits.push_back(x);
break;
}else {
int remainder = x % ;
int quotient = x / ;
digits.push_back(remainder);
x = quotient;
}
}
// reverse output
long long int ret = ;
for(int i = ; i < digits.size(); i ++) {
ret = ret * + digits[i];
if(ret > INT_MAX){
ret = INT_MAX;
break;
}
}
return (int) ret;
} int reverse(int x) {
if(x == )
return ;
if(x > ) {
return reversePostiveInt(x);
}else if( x == INT_MIN){
//overflow
return INT_MIN;
}else if ( x > INT_MIN) {
return -reversePostiveInt(-x);
}
}

Reverse Integer [LeetCode]的更多相关文章

  1. Reverse Integer LeetCode Java

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public cl ...

  2. Reverse Integer ---- LeetCode 007

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Solution ...

  3. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  4. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  5. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  6. LeetCode之Easy篇 ——(7)Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...

  7. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  8. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  9. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

随机推荐

  1. COM/ATL 资料收集

    COM/ATL COM基础知识 COM技术分类

  2. Hibernate——基础及XML配置

    1.入门 hibernate是跟数据库打交道的,一般跟数据库打交道的都不简单 原始.底层直接的一些操作.编码量比较大.费时.用框架高效 把原来一点一点实现的东西,现在给个半成品,不用在这上边发时间,把 ...

  3. DIV与IDIV的用法

    DIV (unsigned divide) 无符号数除法 格式:DIV SRC 执行的操作: 字节操作:16位被除数在AX,8位除数为源操作数,结果的8位商在AL中,8位余数在AH中.表示为 (AL) ...

  4. Android 测试工具

    有时候会发现给手机烧入的信息里少了某一些文件,比如一个图标,或者一个mp3文件之类的等等,为此做了一个小工具检查指定手机里面是否包含相应的文件. 通过程序执行手机的命令来操作手机,感觉还挺有意思的. ...

  5. 软/硬链接指令:ln

    语法: ln  [选项]  原文件  目标文件 选项: -s 创建软连接(创建软链接时,若所在文件夹不一致,原文件要使用绝对路径) 硬链接特征: 1.拥有相同i节点和存储block块,可以看成是同一个 ...

  6. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

  7. Codeforces Round #376 (Div. 2) F. Video Cards 数学,前缀和

    F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. FLASH CC 2015 CANVAS (一) 与AS3的写法区别

    注意 此贴 为个人边“开荒”边写,所以不保证就是最佳做法,也难免有错误! 正式教程会在后续开始更新 AS3 JS stop() this.stop(); mc.stop() this.mc.stop( ...

  9. MongoDB入门教程之C#驱动操作实例

    实体类: using MongoDB.Bson; namespace WindowsFormsApp { class User { //public ObjectId _id; //BsonType. ...

  10. [转载] 每周推荐阅读 BFQ:实现IO的隔离共享与高吞吐访问

    磁盘IO和网络IO隔离与共享是混部应用中基本需求,从早些年的BVC到现在的Matrix,以及Galaxy,或者未来的BS/Mint混部都遇到类似的问题:由于无法有效实现IO级的隔离(包括吞吐隔离.延时 ...