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. 图论(KM算法):COGS 290. [CTSC2008] 丘比特的烦恼

    290. [CTSC2008] 丘比特的烦恼 ★★★   输入文件:cupid.in   输出文件:cupid.out   简单对比 时间限制:1 s   内存限制:128 MB 随着社会的不断发展, ...

  2. UVa11419 SAM I AM(构造最小点覆盖)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27475 [思路] 二分图的最小点覆盖以及构造最小覆盖. 二分图的最 ...

  3. [Audio processing] FFMPEG转音频格式和采样率

    利用FFMPEG转音频格式和采样率 import os import string import subprocess as sp #Full path of ffmpeg FFMPEG_BIN = ...

  4. lightoj 1005 组合数学

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1005 #include <cstdio> #include <cst ...

  5. [置顶] [混迹IT职场系列]一、转正的那些事儿

    讲起转正,是每个IT人进入职场后要面对的第一关,只有越过这第一关卡才能更加顺利玩弄职场或被职场玩弄或互相玩弄. 很多人觉得转正只需自身努力即可,譬如有句话叫做 “只要功夫深,铁针磨成棒”.其实不然,职 ...

  6. UVa 108: Maximum Sum

    这道题用暴力解法+动态规划.分析如下: 对于某个1*m的矩阵,即一个数列,求其maximal sub-rectangle,可以通过求最大长连续字串和来求得(这个用到了动态规划). 那么对于n*m的矩阵 ...

  7. Java 实现字符串反转

    方法一: public class StringReverse { public void swap(char[] arr, int begin, int end) { while(begin < ...

  8. hash定义

    * 若结构中存在关键字和K相等的记录,则必定存储在f(K)的位置上.由此,不需比较便可直接取得所查记录.这个对应关系f称为 散列函数(Hash function),按这个思想建立的表为 散列表. * ...

  9. JDK5-自动拆装箱

    拆装箱:在基本类型与其对应的引用类型之间转换 装箱:Integer iObj = 5; 拆箱:int i = 5 + iObj; 装箱时,一个字节以内的数据在一个常量池中(小整数的使用频率高),即-1 ...

  10. Android程序版本更新--通知栏更新下载安装(转)

    Android应用检查版本更新后,在通知栏下载,更新下载进度,下载完成自动安装,效果图如下: 检查当前版本号 AndroidManifest文件中的versionCode用来标识版本,在服务器放一个新 ...