题目: 

  Reverse digits of an integer.

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

思路:不断取最低位加到先前值得后面,如下:

        while(x!=0){
          res=res*10+x%10;
          x/=10;
        }

   还要注意反转后可能会出现溢出的情况。

public class Solution {
public int reverse(int x) {
long res=0;
while(x!=0){
res=res*10+x%10;
x/=10;
} if(res>Integer.MAX_VALUE||res<Integer.MIN_VALUE){
return 0;
}else{
return (int)res;
}
}
}

  

【LeetCode】7. Reverse Integer 整型数反转的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  3. leetcode题解||Reverse Integer 问题

    problem: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 ...

  4. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  5. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  6. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  7. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  9. [leetcode]7. Reverse Integer反转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

随机推荐

  1. linux 鼠标中键粘帖功能?!!

    转载自:http://yjhexy.iteye.com/blog/785564 ubuntu鼠标中键问题,其实也不是什么问题,ubuntu的鼠标中键是用来快速粘贴的,只是windows用惯了,时不时手 ...

  2. c++随机数生成

    算机的随机数都是由伪随机数,即是由小M多项式序列生成的,其中产生每个小序列都有一个初始值,即随机种子.(注意: 小M多项式序列的周期是65535,即每次利用一个随机种子生成的随机数的周期是65535, ...

  3. C#程序集编译输出XML文档的作用

    下图是ClassLib1类库的项目属性 /// <summary> /// 读取INI文件 /// </summary> /// <param name="Se ...

  4. iphone dev 入门实例2:Pass Data Between View Controllers using segue

    Assigning View Controller Class In the first tutorial, we simply create a view controller that serve ...

  5. Java的安全性和可移植性

        Java的这两个特性,关键在于Java编译器的输出并不是可执行的代码,而是字节码 bytecode.      字节码是一套设计用来在Java运行时系统下执行的高度优化的指令集,该Java运行 ...

  6. Linux使用笔记: 定制core dump文件的文件名

    在开发过程中,当一个Linux程序异常退出时,我们可以通过core文件来分析它异常的详细原因.缺省情况下,Linux在程序异常时不产生core文件,要想让程序异常退出时产生core dump文件,需要 ...

  7. 转__Android Studio ,基于intellij idea

    看到论坛里一些关于Android Studio的帖子,基本上是停留在使用教程上.在此做一些功能性的分析和测评 下载地址 :http://developer.android.com/index.html ...

  8. java多线程的使用2

    1.join与interrupt的用法 class Sleeper extends Thread { private int duration; public Sleeper(String name, ...

  9. block的传值简单示例仅供参考,大牛勿喷

    #import "ViewController.h" typedef void(^sumBlock)(int s);//声明为一个类型; /** *  用声明的block类型 su ...

  10. ubuntu 命令学习大全

    http://wiki.ubuntu.org.cn/UbuntuSkills#.E6.98.BE.E7.A4.BA.E5.BD.93.E5.89.8D.E7.A1.AC.E4.BB.B6.E4.BF. ...