Reverse digits of an integer.

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

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).

分析:我们可以从余数和除数方面考虑,我们一般会想到怎么样才能得到给定的数各个位上的数字,我们就可以用除10取余。知道除数为0为止。

class Solution {
public:
int reverse(int x) {
int res=;
while(x!=)
{
res=res*+x%;
x=x/;
}
return res;
}
};

python:

class Solution:
# @return an integer
def reverse(self, x):
flag=False
res=0
if x<0:
x=-x
flag=True
while x>0:
res=res*10+x%10
x=x/10
if flag:
res=-res
return res

Reverse Interger的更多相关文章

  1. Leetcode--easy系列1

    近期開始刷Leetcode题目.花了一个星期先完毕了easy难度级别的题目,easy级别的题目思路比較简单,但不一定就直接AC了,主要是问题要考虑全然.特别是对特殊情况的处理. #6 ZigZag C ...

  2. HDOJ 1062 Text Reverse

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. hdu 1062 Text Reverse 字符串

    Text Reverse                                                                                  Time L ...

  4. HDOJ/HDU 1062 Text Reverse(字符串翻转~)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  5. HDU1062:Text Reverse

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  6. Problem : (1.2.1) Text Reverse

    #include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...

  7. (reverse) Text Reverse hdu1062

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. HDUOJ--------Text Reverse

      Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. HDU——1062Text Reverse(水题string::find系列+reverse)

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. 根据div 标签 查看数组@class=modulwrap 下面的/table/tbody/tr/td

    <div class="modulwrap"> <div class="request_title"> <span class=& ...

  2. (转载)PHP strtotime函数详解

    (转载)http://www.jb51.net/article/21495.htm strtotime函数是一个很好的函数,灵活的运用它,会给你的工作带来不少方便.但PHP的手册中却对此函数的参数没作 ...

  3. zoj 3672 Gao The Sequence

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4915题意:a[k]-一个任意的数,这个数要等于a[1]~a[k]每个数减去任意 ...

  4. Evaluate Reverse Polish Notation——LeetCode

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. 启动tomcat时报classpath not found

    启动tomcat时报classpath  not found 原因是缺包,首先查看tomcat安装地址,然后找到webapps目录下,找到该项目,看lib下是否缺包,不能单纯的看项目下是否缺包.

  6. 深度克隆---js对象引用

    首先,我们要知道,javascript中除了基本类型(number,string,boolean,null,undefined)之外就是引用类型了,也可以说就是js对象了. 引用类型的赋值其实是对象保 ...

  7. 30余套系统模版|DIV+CSS网页模板|简洁大气系统模板

    30余套系统模版|DIV+CSS网页模板|简洁大气系统模板.都是一些后台系统的模版,很适合开发一些管理系统,办公系统,网站后台系统等.使用很广泛,很实用的系统模版. 下载地址: 点击下载

  8. Demo_CS(移动,切换枪支,发射子弹)

    using UnityEngine; using System.Collections; public class Gun : MonoBehaviour { private Animator ani ...

  9. PreTranslateMessage作用和用法

    PreTranslateMessage作用和用法  PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗体的消息都要通过这里,比較经常使用, ...

  10. Java之多线程断点下载的实现

    RandomAccessFile类: 此类的实例支持对随机訪问文件的读取和写入.随机訪问文件的行为相似存储在文件系统中的一个大型 byte 数组. 存在指向该隐含数组.光标或索引,称为文件指针.输入操 ...