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. Tomcat 6 --- 使用Jasper引擎解析JSP

    熟悉JAVA web开发的朋友都知道JSP会被转换成java文件(预编译),然后编译成class使用,即按照JSP-->java-->class的过程进行编译. 由于JVM只认识class ...

  2. linux下的库冲突问题

    lib1.c #include <stdio.h>int fun(){ printf("lib1\n"); return 0;} lib2.c #include < ...

  3. Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析

    经过前面几篇文章的铺垫,今天我们终于要看看AQS的庐山真面目了,建议第一次看AbstractQueuedSynchronizer 类源码的朋友可以先看下我前面几篇文章: <Java并发包源码学习 ...

  4. Hibernate之映射一对一关联

    一.一对一关联的概念: 一对一之间的关联是指:两张表中的信息是一对一的关系,比如我们每个人和身份证的关系,一个人对应一张身份证,一张身份证也只能对应一个人. Hibernate提供了两种映射一对一关联 ...

  5. linux 下开放端口问题

    Linux安装Tomcat后本地可以正常访问,可是这时Tomcat还不能被外界访问需要在Linux默认防护墙上打开8080端口 打开 /etc/sysconfig/iptables   [root@l ...

  6. thusc滚粗记

    day0 下午到了北京,雾霾还是那么大.. 到宾馆报个到,和哥哥吃了一波饭,去不起西郊...只能去五道口了... 晚上和wyz队长见面,wyz队长好帅啊...没带手机拍照真是个错误TAT day1 今 ...

  7. python 循环语句的else语句用法,当循环条件变为假,切不是通过breakbreak终止的时候,就会执行这个else语句。

    循环语句可以有一个else子句:当(for)循环迭代完整个列表或(while)循环条件变为假,而非由break语句终止时,就会执行这个else语句.下面循环搜索质数的代码例示了这一点: >> ...

  8. Caffe学习系列(9):solver优化方法

    介绍了各种优化算法 参考:http://www.cnblogs.com/denny402/p/5074212.html

  9. Yii2.0中文开发向导——控制器(Controller)

    控制器(Controller) 本节包含以下方面的内容 基本概念 路由 默认路由 动作的参数 在动作中定义参数 从请求(request)中获取参数 独立动作 动作过滤器(Action Filters) ...

  10. python自动化之装饰器

    1 高阶函数 满足下列条件之一就可成函数为高阶函数 某一函数当做参数传入另一个函数中 函数的返回值包含n个函数,n>0 高阶函数示范 def bar(): print 'in the bar' ...