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. .NET中的工作目录一览!

    定义:       当前工作目录——进行某项操作的目的目录,会随着OpenFileDialog.SaveFileDialog等对象所确定的目录而改变. 当前执行目录——该进程从中启动的目录,即文件自身 ...

  2. MySQL性能优化的最佳经验,随时补充

    1.为查询优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存 ...

  3. mybaties # , $

    mybaties会对#引入的值加上双引号, 如: #{buildingName} -------->"buildingName";mybaties会将$引入的值直接显示到sq ...

  4. Windows Phone自带的语音识别

    WindowsPhone下语音操作包括: 1.程序内部的语音识别,用户可以通过语音识别进行输入或完成相关任务 2.控制程序的语音命令,控制程序启动.打开,并可对页面跳转等进行操作 这篇文章将构建一个简 ...

  5. java之Date

    1.java时间处理 package com.bmkit.util.date; import java.text.DateFormat; import java.text.ParseException ...

  6. Javascript高级程序设计——引用类型

    对象在javascript中被称为引用类型的值,而且有一些内置的引用类型可以创建特定的对象: 引用类型与传统面向对象中的程序设计的类相似,但实现不同: Object是一个基础类型,其他所有类型都从Ob ...

  7. int long 等基础类型在不同平台的大小

    转自http://www.cnblogs.com/yishuiliunian/archive/2011/03/18/1988244.html 上次腾讯面试,问我int和long分别几个字节,结果被鄙视 ...

  8. C语言中free函数是如何确定要释放多少内存空间的

    本文链接:http://www.cnblogs.com/xxNote/p/4009359.html 今天看书的时候看到free函数释放动态申请的内存时只需要把内存块的首地址传过去就行了,显然仅仅依靠首 ...

  9. 【转】phpcms授课学习

    来自:http://blog.csdn.net/yanhui_wei/article/category/1220735 <?php 思路: 一.目前在企业中使用比较多的cms内容管理有如下几种: ...

  10. ProgressDialog使用总结

    以前对ProgressDialog不太熟练,特地看到的这个文章 ProgressDialog的使用  ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialo ...