Reverse digits of an integer.

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

总结:处理整数溢出的方法

①用数据类型转换long  或 long long

②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好)

③整体恢复,看数字是否相等。

思路:注意30000这样以0结尾的数字,注意越界时返回0.

我检查越界是通过把翻转的数字再翻转回去,看是否相等。

int reverse(int x) {
int xx = abs(x);
int ans = ;
int zeros = ; //如果x = 2000 这种后面有很多0 那翻转后是2 再翻转还是2 需要乘以1000才能恢复成2000 while(xx != )
{
int r = xx % ;
xx /= ;
ans = ans * + r;
if(ans == ) zeros *= ;
}
ans = (x < ) ? -ans : ans; //检测是否溢出 思路把数字重新翻转回去,看结果是否相同
int anss = abs(ans), check = ;
while(anss != )
{
int r = anss % ;
anss /= ;
check = check * + r;
}
check = (x < ) ? -check : check;
check *= zeros; if(check != x) //溢出了
return ;
else
return ans;
}

基于②的判断:

public int reverse(int x)
{
int result = ; while (x != )
{
int tail = x % ;
int newResult = result * + tail;
if ((newResult - tail) / != result)
{ return ; }
result = newResult;
x = x / ;
} return result;
}

基于①的判断:

int reverse(int x) {
long num = abs((long)x);
long new_num = ;
while(num) {
new_num = new_num* + num%;
num /= ;
} if (new_num > INT_MAX) {
return ;
}
return (x< ? -*new_num : new_num);
}

【leetcode】Reverse Integer(middle)☆的更多相关文章

  1. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  2. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  3. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  4. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【leetcode】Dungeon Game (middle)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  7. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  8. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  9. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. glusterFS的常用命令 (转)

    1.       启动/关闭/查看glusterd服务 # /etc/init.d/glusterd start # /etc/init.d/glusterd stop # /etc/init.d/g ...

  2. tc 146 2 BridgeCrossing(n人过桥问题)

    SRM 146 2 1000BridgeCrossing Problem Statement A well-known riddle goes like this: Four people are c ...

  3. Hadoop 面试题之storm 3个

    Hadoop 面试题之八 355.metaq 消息队列 zookeeper 集群 storm集群(包括 zeromq,jzmq,和 storm 本身)就可以完成对商城推荐系统功能吗?还有其他的中间件? ...

  4. Ubuntu 12 修改当前用户密码:new password is too simple

    修改当前登录用户的密码,通常使用如下命令: $ passwd Old password:****** New password:******* Re-enter new password:****** ...

  5. HDU 5073 Galaxy(2014鞍山赛区现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...

  6. iOS开发网络篇—大文件的多线程断点下载

    http://www.cnblogs.com/wendingding/p/3947550.html iOS开发网络篇—多线程断点下载 说明:本文介绍多线程断点下载.项目中使用了苹果自带的类,实现了同时 ...

  7. NOIP“对偶”题:还教室

    先说一下思路: 方差可以经过恒等变形变成 x12 + x22 + ... + xn2 + 2a(x1 + x2 + ... + xn) + na2 所以维护平方和.连续和即可 平均数我就不再推了…… ...

  8. opencv png和jpg的叠加

    char *bgFile = "C:/C_Project/HandTraining/Debug/res/bg.jpg"; FILE *file = fopen(bgFile, &q ...

  9. php批量下载文件

    最近用codeigniter开发一个图片网站,发现单文件下载很容易实现,批量下载的话,就有点麻烦. 普通php下载比较简单,比如我封装的一个函数: function shao_download($fi ...

  10. Python自动化之常用模块

    1 time和datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理 ...