Reverse digits of an integer.

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer,
then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

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

要求:反转整形数字,需要考虑到负数和整形数字溢出的问题

10100
=>101
1000000003
=>0
-2147483648
=>
2147483647
-2147483648

public int reverse(int x) {
StringBuilder sb = new StringBuilder();
int mod, flag=0;
boolean temp = false;
if (x==0 || x==Integer.MIN_VALUE)
return 0;
if(x<0){
x = Math.abs(x);
temp = true;
}
System.out.println(x);
while(x>0){
mod = x%10;
if(mod==0 && flag==0){}else{
sb.append(mod+"");
flag++;
}
x = x/10;
}
long val = Long.valueOf(sb.toString());
int res = (val>Integer.MAX_VALUE)? 0:(int)val;
if (temp)
res = -res;
return res;
}

[LeetCode]-algorithms-Reverse Integer的更多相关文章

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

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

  2. leetcode:Reverse Integer 及Palindrome Number

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

  3. LeetCode 7 Reverse Integer & int

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

  4. Leetcode 7. Reverse Integer(水)

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

  5. leetcode:Reverse Integer(一个整数反序输出)

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

  6. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

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

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

  8. LeetCode 7. Reverse Integer (倒转数字)

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

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

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

  10. LeetCode 7. Reverse Integer

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

随机推荐

  1. 360度3D 旋转插件

    Circlr插件是一款基于jQuery的可以对图片进行360度全方位旋转展示的插件.Circlr通过按一定角度规律拍摄的产品图片,制作出可以使用鼠标拖动.鼠标滚轮和移动触摸来进行图片逐帧旋转的效果.比 ...

  2. Bug快到碗里来

    Bug快到碗里来 python错误--'list' object is not callable 原因及解决方法1 你定义了一个变量的变量名和系统自带的关键字冲突,调用变量时关键字被传到调用的位置,就 ...

  3. picgo+typora优化markdown体验

    picgo+typora优化markdown体验 写markdown的时候许多图片的存放的上传是一个大问题,之前一直都是使用先截图,在commit之后,再将线上图片地址粘贴到相应的位置 现在知道了pi ...

  4. 普通交叉验证(OCV)和广义交叉验证(GCV)

    普通交叉验证OCV OCV是由Allen(1974)在回归背景下提出的,之后Wahba和Wold(1975)在讨论 了确定多项式回归中多项式次数的背景,在光滑样条背景下提出OCV. Craven和Wa ...

  5. kafka复习(1)

    一:flume复习 0.JMS(java message service )java消息服务 ----------------------------------------------------- ...

  6. servlet和Struts2的线程安全性对比

    1.>在servlet中,定义成员变量是不安全的,,因为,每次请求操作的是该同一个成员变量,,会出现线程不安全的问题. 2.>而在struts2中,在Action中定义成员变量是安全的,, ...

  7. Coco56公众号关键字索引

    目录 1. 本文地址 2. 公众号介绍 3. 关键词及含义 1. 本文地址 博客园:https://www.cnblogs.com/coco56/p/11182421.html 简书:https:// ...

  8. Centos下载安装grafana

    grafana的官网下载:https://grafana.com/grafana/download 一.安装服务端图像呈现组件 # yum install -y fontconfig freetype ...

  9. Scrapy-redis分布式+Scrapy-redis实战

    [学习目标] Scrapy-redis分布式的运行流程 Scheduler与Scrapy自带的Scheduler有什么区别 Duplication Filter作用 源码自带三种spider的使用 6 ...

  10. yum 安装mysql-server 5.6

    # rpm ivh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm # yum install -y mysql-s ...