Description

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).

Example

Given x = 123, return 321

Given x = -123, return -321

解题:注意别溢出即可,所以结果用long型变量保存,最后返回的时候强转成int型。代码如下:

public class Solution {
/**
* @param n: the integer to be reversed
* @return: the reversed integer
*/
public int reverseInteger(int n) {
// write your code here
long res = 0;
boolean isNative = true;
while(n != 0){
res = res * 10 + n % 10;
n = n / 10;
if(res > Math.pow(2, 31) || res < -1 * Math.pow(2, 31))
return 0;
}
return (int)res;
}
}

Math.pow(2, 31)也可以写成 Integer.MAX_VALUE,-1 * Math.pow(2, 31) 写成 Integer.MIN_VALUE

413. Reverse Integer【LintCode java】的更多相关文章

  1. 413. Reverse Integer【easy】

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  2. leetcode:Reverse Integer【Python版】

    1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...

  3. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  4. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  5. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

  6. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  7. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  8. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  9. 365. Count 1 in Binary【LintCode java】

    Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return  ...

随机推荐

  1. POJ 2524 独一无二的宗教(裸并查集)

    题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K ...

  2. C编程规范, 演示样例代码。

    /*************************************************************** *Copyright (c) 2014,TianYuan *All r ...

  3. CentOS7安装mysql兼容性问题

    Linux上安装MySQL时出现不兼容的解决办法: [root@localhost ~]# rpm -ivh MySQL-server-5.5.24-1.linux2.6.x86_64.rpm Pre ...

  4. PHP介绍(PHP入门1)

    BS架构和CS架构 B:Browser:浏览器 S:Server:服务器 C:Client:客户端 BS 浏览器和服务器的关系,通过浏览器来访问服务器,比如:百度.新浪... 优点:只要有浏览器就能访 ...

  5. File、Paths和Files类的使用详解

    Paths:通过get()方法返回一个Path对象,Path用于表示文件路径和文件. Files:提供了大量处理文件的方法,例如文件复制.读取.写入,获取文件属性.快捷遍历文件目录等..... Fil ...

  6. iview admin 生成环境打包时路径问题

    关于生产打包路径不对,字体图标引用错误的问题.以下是解决方案供参考:1.webpack.base.config.js 17行修改为: path: path.resolve(__dirname, '.. ...

  7. iOS audio不支持循环播放

    解决办法:监听播放完成事件(注意点,audio标签不能设置循环播放,去除标签 loop="loop"或者 loop="false",不然不走播放完成事件) $( ...

  8. mongodb学习一(使用mongoResposity)

    最近公司做一个项目用到了mongodb,下面来介绍一下MongoRepository接口. 大家可以类比Hibernate的jpa,MongoRepository是一个springdata提供的一个有 ...

  9. 学习tp5的第二天(路由)

    一.学习路由 1.phpstudy版本的环境去掉indx.php 直接访问url phpstudy配置的环境需要设置 入口目录的 .htaccess文件如下: <IfModule mod_rew ...

  10. java.io.FileNotFoundException:file:\D:\code\xml-load\target\XX.jar!\XXX(文件名、目录名或卷标语法不正确。)

    1.当使用Spring Boot将应用打成jar时,需要读取resources目录下配置文件时,通常使用ClassLoader直接读取,通常建议使用这种方式,直接将xml文件读成流传入 // 加载xm ...