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. poj3417

    poj3417 题意 给出一颗 n 个节点, n - 1 条边的树,再加上 m 条新边,允许删掉树边和新边各一条,问能使树分为两部分的方案数. 分析 在树的基础上加上不重复的新边一定会构成环,那么考虑 ...

  2. Hadoop集群搭建(非HA)

    1.准备Linux环境 1.0先将虚拟机的网络模式选为NAT 1.1修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=itcast ### ...

  3. js的函数返回值

    今天从跟公司牛人那学到的~避免以后忘了赶快记录下来 平时JS的function的返回值可以是一个数值,也可以是一个对象({name:abc,age:123}),更可以是一个函数(这里我是第一次听说), ...

  4. 一篇文章介绍GItHub的基础使用

    最近复习了一下Git的使用,简单总结了一些.以供以后查阅和同行参考. 一,安装 首先是Linux下: 打开shell ,输入 sudo apt-get install git-core 之后回车输入密 ...

  5. jQuery之stop()

    开启第一篇原创博客,内容朴实,但绝对属实. 先来看看w3c的定义和语法: 定义:stop() 方法停止当前正在运行的动画. 语法:$(selector).stop(stopAll,goToEnd) 参 ...

  6. [bzoj4872]分手是祝愿

    Description Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态 ...

  7. MyBatis介绍

    MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的 XML或注解用于配 ...

  8. C#中switch的使用

    今天在网上看到有人给出这么一个程序需求,博主就拿来回忆回忆C#中switch的用法 程序需求如下: 根据星期几(一 ~日) ,输出特价菜“一”.“二”.“三”,输出“干煸扁豆6元.”“四”.“五”,输 ...

  9. nodejs构建多房间简易聊天室

    1.前端界面代码 前端不是重点,够用就行,下面是前端界面,具体代码可到github下载. 2.服务器端搭建 本服务器需要提供两个功能:http服务和websocket服务,由于node的事件驱动机制, ...

  10. PHP中常量和变量的区别

    1.常量只能赋一次值: 以下是申请常量的两种方法: const THE_VALUE="one"; define("THE_VALUE","one&qu ...