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. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  2. CTreeCtrl::HitTest

    CTreeCtrl::HitTest   调用此函数确定指定的位置点相对树视图控件的工作区的. HTREEITEM HitTest(  CPoint pt,  UINT* pFlags = NULL  ...

  3. 维度属性的KeyColumns如果是Integer类型,那么维度表中该列的值不能有为null的

    如果维度属性的 KeyColumns的DataType设置为了Integer类型,那么要注意该维度属性列在数据库中不能有为null的值. 例如下图中我们有维度DIM_Vehcile,其中有个维度属性叫 ...

  4. 一个input输入内容监听联动的demo

    两个input,一个在其中一个输入,内容在另一个input中实时回显 代码如下 <!DOCTYPE html> <html> <head> <title> ...

  5. 纯javascript实现选择框的全选与反选

    HTML部分 <div id="wrap_input_box" > <input type="checkbox"><br> ...

  6. hdu Exponentiation高精度实数乘幂(用了带小数的高精度模板)

    #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...

  7. MySQL->AUTO_INCREMENT[20180516]

    MySQL表格中自增长主键AUTO_INCREMENT使用,实现序列的最简单的方式   创建一个AUTO_INCREMENT自增的表 mysql> create table seq_test( ...

  8. 什么是ajax和json,说说他们的优缺点

    ajax异步传输的js和xml.实现页面无刷新状态更新页面和异步提交 所谓异步,简单解释就是:向服务器发送请求的时候,我们不必等待结果,而是同时做其他的事情,等到有了结果后它自己会根据设定进行后续操作 ...

  9. VUE 采坑之旅-- Mint-ui 按需引入报出Module build failed: Error: Couldn't find preset "es2015" relative to directory "C:\\phpStudy\\PHPTutorial\\WWW\\text\\vuep\\vue-demo"

    首先按照mint-ui的文档中按需引入的要求,先执行 npm install babel-plugin-component -D 然后,将.babelrc文件替换了,但是后来我又将其改了(采坑过程我也 ...

  10. Elasticsearch 6.3.1、Head插件 安装及配置

    安装Elasticsearch Elasticsearch下载地址:https://www.elastic.co/cn/downloads/elasticsearch 也可以直接使用wget下载到某目 ...