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. HNUSTOJ-1621 Picking Cabbage(状态压缩DP)

    1621: Picking Cabbage 时间限制: 2 Sec  内存限制: 32 MB提交: 26  解决: 14[提交][状态][讨论版] 题目描述 Once, Doraemon and  N ...

  2. java集合之 ConcurrentHashMap的产生

    ConcurrentHashMap:  在java集合中 最常用的是ArrayList 效率最高的还是HashMap 但是线程不安全   HashTable是线程安全的(里面的方法是同步方法) 但相比 ...

  3. Java学习笔记-----eclipse中建立Java项目并成功运行

    环境:WIN7 64位 +eclipse 2018 12version 具体方法:https://jingyan.baidu.com/album/9c69d48fefa53113c9024eb3.ht ...

  4. tf.get_variable函数的使用

    tf.get_variable(name,  shape, initializer): name就是变量的名称,shape是变量的维度,initializer是变量初始化的方式,初始化的方式有以下几种 ...

  5. 一、Core授权-2 之.net core 基于Jwt实现Token令牌

    一.Startup类配置 ConfigureServices中 //添加jwt验证: services.AddAuthentication(JwtBearerDefaults.Authenticati ...

  6. thinkphp 多条件联合查询 where例句

    $where['username'] = array("eq",$username); $where['phone'] = array("eq",$userna ...

  7. windows 快捷键相关命令

    Mstsc  远程链接 Taskmgr 任务管理器 Regedit  打开注册表 Netstat -ano | find  “80” 查找内容 tasklist /fi "pid eq 57 ...

  8. Nginx优化_访问并发量(进程可以打开的最大文件数量)

    如果客户端访问服务器提示“Too many open files”如何解决? [root@proxy ~]# ab -n 2000 -c 2000 http://192.168.1.100/    # ...

  9. PAT Basic 1091 N-自守数 (15 分)

    如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3,而 2 的末尾两位正好是 9,所以 9 是一个 3-自守数. 本题就请你编写程序判断一个给定的 ...

  10. YARN组件详细介绍

    一.ResourceManager 内部主要有两个组件: 1.Scheduler:这个组件完全是插拔式的,用户可以根据自己的需求实现不同的调度器,目前YARN提供了FIFO.容量以及公平调度器.这个组 ...