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

    hdu5803 题意 给出四个整数 A B C D,问有多少个四元组 (a, b, c, d) 使 a + c > b + d 且 a + d >= b + c ,0 <= a &l ...

  2. Docker - docker machine

    前言 之前在使用docker的时候,对于docker-machine的理解有一些误解(之前一直以为docker-machine和docker-engine等价的,只不过是在window或者mac平台上 ...

  3. Java之枚举

    1.定义 enum 是一种数据类型,与 全局常量比较相似,都是全局的并且是可以通过类名调用的 与全局常量区别 枚举功能更强大,可以有属性和方法 枚举比全局常量更加的规范 2.枚举特性 1)可以有属性以 ...

  4. Python抓取成都房价信息

    Python里scrapy爬虫 scrapy爬虫,正好最近成都房价涨的厉害,于是想着去网上抓抓成都最近的房价情况,顺便了解一下,毕竟咱是成都人,得看看这成都的房子我以后买的起不~ 话不多说,进入正题: ...

  5. solr学习笔记-开篇

    由于工作需要,近期接触了一下全文检索的相关技术,从lucenen到solr开始慢慢一路摸爬滚打,仅以此文记录自己的学习里程和记录下各种坑. 本次学习基于以下环境版本: java7,solr5.5.4( ...

  6. 卷积神经网络的变种: PCANet

    前言:昨天和大家聊了聊卷积神经网络,今天给大家带来一篇论文:pca+cnn=pcanet.现在就让我带领大家来了解这篇文章吧. 论文:PCANet:A Simple Deep Learning Bas ...

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

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

  8. MongoDB--GridFS 文件存储系统

    GridFS是Mongo的一种专门用存储小型文件的功能. 使用于下列场景: 1.写入文件:mongofiles put 文件路径 注意,当前mongo实例链接的哪个库,将写文件在哪个实例里面的grid ...

  9. 简单轻量级的一套JS 类库(RapidDevelopmentFramework.JS)

    1.最近好久没有更新自己的博客了,一直在考虑自己应该写一些什么.4.2日从苏州回到南京的路上感觉自己的内心些崩溃和失落,我就不多说了? 猛然之间我认为自己需要找一下内心的平衡.决定开发属于自己一套快速 ...

  10. sql with as 用法(转载)

    一.WITH AS的含义     WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...