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

  1. public int reverse(int x) {
  2. StringBuilder sb = new StringBuilder();
  3. int mod, flag=0;
  4. boolean temp = false;
  5. if (x==0 || x==Integer.MIN_VALUE)
  6. return 0;
  7. if(x<0){
  8. x = Math.abs(x);
  9. temp = true;
  10. }
  11. System.out.println(x);
  12. while(x>0){
  13. mod = x%10;
  14. if(mod==0 && flag==0){}else{
  15. sb.append(mod+"");
  16. flag++;
  17. }
  18. x = x/10;
  19. }
  20. long val = Long.valueOf(sb.toString());
  21. int res = (val>Integer.MAX_VALUE)? 0:(int)val;
  22. if (temp)
  23. res = -res;
  24. return res;
  25. }

[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. [转载]Linux内核编译

    原文地址:https://blog.csdn.net/qq_34247099/article/details/50949720 写在前面的话: 本人大二,东南大学一个软工狗,正在修一门名为<操作 ...

  2. Vue常用修饰符

    Vue提供了事件修饰符用于DOM的事件处理,常用的事件修饰符有以下几个: (1). stop:阻止冒泡(通俗讲就是阻止事件向上级DOM元素传递) 点击内层div的结果: 点击外层div的结果: 修改代 ...

  3. php前端做过滤校验

    http://www./test_form.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E 以上的 URL 中,将被解析为如下代码并执行: < ...

  4. 使用switchshow/supportshow命令确认Brocade交换机型号(转载)

    switchshow命令(或supportshow日志)中的switchType是以数字来代表不同的交换机型号,完整的对应表格如下: Switchtype EMC / Brocade名称 / 端口 / ...

  5. 总结下Nginx的功能模块

    nginx-1.10.3]# ./configure  \ --prefix=/usr/local/nginx   \        #指定安装路径 --user=nginx --group=ngin ...

  6. java.lang.Integer 类(JDK1.7)

    1.Integer 和int 的区别 ①.Integer 是 int 包装类,int 是八大基本数据类型之一(byte,char,short,int,long,float,double,boolean ...

  7. calculate_gain

    torch.nn.init.calculate_gain(nonlinearity,param=None) 对于给定的非线性函数,返回推荐的增益值.这些值如下所示: relu_gain=nn.init ...

  8. Monty 大厅问题(Monty Hall Problem)也称作三门问题,出自美国大型游戏节目 Let's Make a Deal。

    Monty 大厅的问题陈述十分简单,但是它的答案看上去却是有悖常理.该问题不仅引起过很多争议,也经常出现在各种考试题中. Monty 大厅的游戏规则是这样的,如果你来参加这个节目,那么 (1)Mont ...

  9. CPU、CPU核与线程的关系

    CPU相关概念: CPU:独立的中央处理单元,体现在主板上是有多个CPU的插槽. CPU cores:在每一个CPU上,都可能有多个核(core),每一个核中都有独立的一套ALU.FPU.Cache等 ...

  10. ZROI 19.08.08模拟赛

    传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. 首先恭喜swk今天翻车! "小心大样例演你."--天祺鸽鸽 果然swk今天被大样例演死了,天祺鸽鸽诚不欺我! A ...