Reverse Integer

Reverse digits of an integer.

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

若反转的数溢出,直接返回0

可以用计算结果来判断溢出,也可以用因数来判断

Java代码实现:

 public class ReverseInteger {
     public static int reverseInt(int x){
         if (x == 0) {
             return 0;
         }
         int flag = -1;
         int result = 0;
         if (x < 0) {
             x = x * flag;
             if (x < 0) {
                 return 0;
             }
         } else {
             flag = 1;
         }
         int digits = 1;
         int temp = x;
         while(temp/10 != 0){
             digits++;
             temp/=10;
         }
         //judge before calculate at every may out of range place
         for (int i = 1; i <= digits; i++) {
             int a,b;
             a = (int) (x/Math.pow(10, i-1));
             a = a%10;//get the single number
             b = (int) Math.pow(10, digits-i);
             if ((a*b<0) || (a > 2 && (digits - i) == 9)) {//use number to judge
                 return 0;
             } else {
                 temp = a*b;
             }
             if (result + temp < 0) {//judge
                 return 0;
             } else {
                 result += temp;
             }
         }
         return result*flag;
     }
     public static void main(String args[]){
         int max = 2147483647;
         int min = -2147483648;
         System.out.println(reverseInt(0));
         System.out.println(reverseInt(1534236469));
         System.out.println(reverseInt(-2133847412));
     }
 }

输出:

0
0
-2147483312

Reverse Integer - 反转一个int,溢出时返回0的更多相关文章

  1. Reverse bits - 按位反转一个int型数字

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  2. [Leetcode] reverse integer 反转整数

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

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

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

  4. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

  5. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

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

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

  7. 问题-[Delphi]用LoadLibrary加载DLL时返回0的错误

    问题现象:用LoadLibrary加载DLL一直返回0句柄,无法进行下一步操作,但同样的代码可以访问到别的DLL.问题处理:1.你加载的路径是不对的,一定要看好路径.2.你是在虚拟机中操作的DLL,因 ...

  8. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

  9. 写一个函数,实现两个字符串的比较。即实现strcmp函数,s1=s2时返回0,s1!=s2时返回二者第一个不同字符的ASCII值。

    #include<stdio.h> #include<stdlib.h> int main(){ setvbuf(stdout,NULL,_IONBF,); ],s2[]; i ...

随机推荐

  1. Scheme 中的 pair 和 list 简述

    pair (cons 1 2) > (1 . 2) 系统返回(1 . 2).cons 操作给两个地址分配了内存空间,并把存放指向 1 的地址放在一个空间,把存放指向2的地址放在另一个空间.存放指 ...

  2. 容器常用操作 - 每天5分钟玩转 Docker 容器技术(25)

    前面讨论了如何运行容器,本节学习容器的其他常用操作. stop/start/restart 容器 通过 docker stop 可以停止运行的容器. 容器在 docker host 中实际上是一个进程 ...

  3. boost.property_tree读取中文乱码问题正确的解决方式

    开发项目的时候在使用boost,在宽字符下遇到中文乱码问题 上网上看大家都是先转成utf8在进行解析的,例如: http://blog.csdn.net/hu_jiangan/article/deta ...

  4. java基础(二章)

    java基础(二章) 一,变量 1.变量是内存中的一个标识符号,用于存储数据 2.变量命名规则 l  必须以字母.下划线 _ .美元符号 $ 开头 l  变量中,可以包括数字 l  变量中,不能出现特 ...

  5. 主题模型(概率潜语义分析PLSA、隐含狄利克雷分布LDA)

    一.pLSA模型 1.朴素贝叶斯的分析 (1)可以胜任许多文本分类问题.(2)无法解决语料中一词多义和多词一义的问题--它更像是词法分析,而非语义分析.(3)如果使用词向量作为文档的特征,一词多义和多 ...

  6. Webpack 资源管理

    Webpack 资源管理

  7. java基础之IO篇

    IO流 在计算机中的流是有方向的即为IO流,分为输入流和输出流,他们的方向都是以服务的方向为主,向服务器中发送指令等等就是输出流,服务器给出的反应等等,我们都说为输出流. 字节流 字符流 输入流 In ...

  8. Windows定时关机

    用shutdown命令.开始菜单>运行,输入shutdown -s -t 7200 (两个小时之后关机)at 12:00 shutdown -s (12:00关机) 其他设置:shutdown ...

  9. Python处理Excel文件

    因为工作需求,需要审核一部分query内容是否有效,query储存在Excel中,文本内容为页面的Title,而页面的URL以HyperLink的格式关联到每个Cell. 于是本能的想到用Python ...

  10. [原创]Floodlight+ovs的基本使用

    一.配置好openflow交换机 配置好交换机的管理地址,可先用串口登,使管理口地址与controller地址在同一个网络中. 在交换机上配置controller地址: 如: 先用命令新建一个brid ...