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. Android 4.4系统获取root权限的方法

    1. 准备工作: 准备一台ubuntu机器,将boot.img复制到该机器上,下载必要的工具sudo apt-get install abootimggit clone https://github. ...

  2. mysql当前用户user()与current_user()

    Mysql在进行登陆时,会去匹配mysql库中的user表,并赋予相应的权限,但是怎么知道我们当时的登陆的用户名及相应的权限呢? 在Mysql中,有两个函数,一个是user(),一个是current_ ...

  3. MongoDB的入门

    MongoDB mongodb是非关系型数据库 对于关系型数据库,存储数据的时候需要提前建表建库,随着数据的复杂度越来越高,所建的表的数量也越来越多:但是非关系型却不需要 mongodb的基本的命令的 ...

  4. 【oracle的安装和基本配置】

    /*----------------------------登录和安装--------------------------------------*/ #从官网上选择安装的版本,任何一个版本都可以,目 ...

  5. Angularjs中的超时处理

    关键代码: // 定义一个定时器, 设置5s为请求超时时间 var timer = $timeout(function () { console.log('登录超时!'); // 模拟提示信息 },5 ...

  6. JavaScript入门学习(2)--进度条

    <html> <style type="text/css"> #bar{width:0px; height:20px; background:#ee00ff ...

  7. 大数据学习--day13(字符串String--源码分析--JVM内存分析)

    字符串String--源码分析--JVM内存分析 String 类的对象 , 是不可变的字符串对象呢 这个不可变很重要,之后要讲的intern()也离不开它的不可变性. https://www.cnb ...

  8. nRF52832 BLE_DFU空中升级OTA(三)准备升级工程(SDK14.2.0)

    准备需要加入DFU功能的工程 在工程main文件services_init函数中加入DFU服务 uint32_t err_code; // Initialize the async SVCI inte ...

  9. 内置函数--eval

    eval参数是一个字符串, 可以把这个字符串当成表达式来求值, 比如'x+2'就是一个表达式字符串>>> x = 2>>> print (eval('x+2'))2 ...

  10. Capabilities & ChromeOptions

    https://sites.google.com/a/chromium.org/chromedriver/capabilities http://stackoverflow.com/questions ...