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. __declspec关键字详细用法

    __declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的ANSI ...

  2. js 返回并刷新

    chrome 下 history.back()或者history.go(-1)都是返回并刷新的,但是ff浏览器只返回不刷新 location = document.referrer 这个在两个浏览器下 ...

  3. celery入门

    认识 这里有几个概念,task.worker.broker.顾名思义,task 就是老板交给你的各种任务,worker 就是你手下干活的人员. 那什么是 Broker 呢? 老板给你下发任务时,你需要 ...

  4. 利用MDK4中的逻辑分析仪分析IO口的PWM波

    1.先设置软件仿真 ,可参看STM32不完全手册的2.4的软件仿真这一章 (原文件名:1.jpg) Example functionality:                             ...

  5. 6.Type and Member Basics

    1.The Different Kinds of Type Members 1.Constants:a symbol that identifies a never-changing data val ...

  6. Windows Internals学习笔记(一)概念与工具

    参考资料: 1. <Windows Internals> 2. Windows Drive Kit 3. Microsoft Windows SDK 4. WDK下载地址 知识点: 1. ...

  7. 面向对象的JavaScript系列一,创建对象

    1.最简单的创建对象方法 var person = new Object(); person.name = "sam wu"; person.age = 25; person.jo ...

  8. nodePPT 这可能是迄今为止最好的网页版PPT

    demo例子为:http://qdemo.sinaapp.com/#0 下载地址为:https://github.com/ksky521/nodeppt/

  9. jquery animate 改变元素背景颜色

    通过animate不能直接设置css样式可以通过https://cdnjs.cloudflare.com/ajax/libs/jquery-color/2.1.2/jquery.color.min.j ...

  10. bootstrap学习笔记<二>(标题,段落样式)

    标题.样式:class="h1"~class="h6" bootstrap中重新定义了h1~h6标签,具体差别如下: 在bootstrap中其他任何标签使用cl ...