Reverse digits of an integer.

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

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.


反转数字的整数。

示例1:x = 123,返回321
示例2:x = -123,返回-321

注意:

假定输入为32位有符号整数。 当反转的整数溢出时,你的函数应该返回0。


  做LeetCode我个人最大的缺点就是上来就干,因为英语毕竟没有中文看着方便,导致好多提示根本没有注意。如本题溢出时返回0。


 class Solution {
public int reverse(int x) {
boolean isAbove=true;
StringBuffer sBuffer=new StringBuffer();
int newX=x;
if (x < 0) {
isAbove = false;
x = Math.abs(x);
}
while (x > 0) {
sBuffer.append(x % 10);
x = x / 10;
}
if (newX!=0) {
try {
if (isAbove)
return Integer.parseInt(sBuffer.toString());
else
return -Integer.parseInt(sBuffer.toString());
} catch (Exception e) {
return 0;
} }
else
return 0;
}
}

  顺道来复习下JAVA基本数据类型的数值范围:

byte的取值范围为-128~127,占用1个字节(-2的7次方到2的7次方-1) 
short的取值范围为-32768~32767,占用2个字节(-2的15次方到2的15次方-1) 
int的取值范围为(-2147483648~2147483647),占用4个字节(-2的31次方到2的31次方-1) 
long的取值范围为(-9223372036854774808~9223372036854774807),占用8个字节(-2的63次方到2的63次方-1)

  

 

LeetCode记录之7——Reverse Integer的更多相关文章

  1. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  2. leetcode第七题Reverse Integer (java)

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

  3. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  4. Leetcode 题目整理-2 Reverse Integer && String to Integer

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

  5. leetCode练题——7. Reverse Integer

    1.题目:   7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...

  6. leetcode第七题--Reverse Integer

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

  7. 【LeetCode算法-7】Reverse Integer

    LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...

  8. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

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

  9. LeetCode(7)Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...

随机推荐

  1. Tensorflow学习(练习)—下载骨骼图像识别网络inception数据集

    import tensorflow as tfimport osimport tarfileimport requests #inception模型下载地址inception_pretrain_mod ...

  2. Inception安装

    前言: MySQL语句需要审核,这一点每个DBA及开发人员都懂,但介于语句及环境的复杂性,大部分人都是望而却步,对其都是采取妥协的态度,从而每个公司都有自己的方法. 大多数公司基本都是半自动化(脚本+ ...

  3. django: rest-framework的 分页和过滤

    django: rest-framework的 分页和过滤 2018年06月28日 10:09:01 weixin_42359464 阅读数:136 标签: flaskrestframeworkdja ...

  4. WOX快速搜索

    WOX wox和mac上的Aflred类似,虽然在功能上稍有逊色,但是还是可以给我们使用windows电脑带来很多福利.首先你不需要在桌面放一堆应用软件的快捷方式,桌面可以非常干净整洁,想要打开某个应 ...

  5. OM Responsibility Flow

  6. 警惕C#事件使用过程中的GC陷阱

    关于C#中的事件,园里已经有大量的文章对其内在实现做过剖析,如果还不甚了解的可以阅读这篇文章 通过Demo来细看C#事件的内在机制 虽然比较早,但非常清楚地展示了事件的内部机制,总结一下就是 1.事件 ...

  7. EF 配置实现建表与迁移

    通过EF 作为操作数据库的工具有一段时间了,也做了几个相对不大的项目,慢慢的也对EF的使用摸索出来了一些规则,虽然说不是技术难点,但是,我说的是但是,能够提高我们开发效率的棉花糖有时我们还是必须要吃的 ...

  8. MVC 登陆鉴权

    public ActionResult Login(string data) { var _params = JsonConvert.DeserializeAnonymousType(data, ne ...

  9. iOS应用审核时间注意点

    1.重大节假日不审核 美国重大节假日期间不审核,具体审核时间查看官方通知

  10. 上课总结-模电chapter 1

    Chapter 1 半导体器件 一.杂质半导体(N型半导体/P型半导体) N型 p型 特点 N型 将少量5价元素参入四价元素中==> 电子多 ==>电子为多子 p型 将少量3价元素参入四价 ...