413. Reverse Integer【LintCode java】
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】的更多相关文章
- 413. Reverse Integer【easy】
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 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 ...
- 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 ...
- 365. Count 1 in Binary【LintCode java】
Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return ...
随机推荐
- Reading HPSRouter A High Performance Software Router
ICACT 2018 Background High speed traffic SDN NFV Hardware Advantages High performace Disadvantages C ...
- spring boot 输入参数统一校验
1 引入spring boot validate maven 依赖 <!-- 验证 --> <dependency> <groupId>org.hiberna ...
- CentOS7.5最小化安装之后的配置
我是最小化安装的,安装了之后很多基本使用配置没有,接下来要做一些配置,如网络之类的,使系统可用. 1.使命令分页显示(1页显示不不下,又不能上翻页) xxx | more 2.查看系统安装了哪些软件包 ...
- Docker 常用命令——容器
1.新建并启动容器 docker run [option] images [command][arg] #根据镜像新建容器并运行.如果本地没有镜像则从docker hub上拉取. --name ...
- ES6中let与const命令详解
阮一峰ES6入门 let 作用域 let命令用来声明变量,但声明的变量只在let命令所在的代码块内有效. { let a = 10; var b = 1; } a // ReferenceError: ...
- thinkphp3.2 上传图片兼容小程序
第一步在配置文件中设置图片的大小和路径 return array( //'配置项'=>'配置值' 'img_save'=>[ 'size' =>[ 'app_gszc_Card'=& ...
- js清除浏览器缓存
浏览器缓存 所有的数据都可以存到服务器中,但这样并不高效,当我们访问网页的时候,一会卡顿,二会浪费服务器的存储空间,三会给服务器造成压力 浏览器缓存,可以提高网站性能和浏览器的速度,但对于需要经常更新 ...
- 用JS遍历循环时覆盖了之前的值
使用js遍历Echarts时,三个数据项,七个分类,遍历如下, 其他都没有问题,就是series.data里的数据只加载了一组,控制台显示数组的长度是7,可是下面的数据只有一个 发现把给数据项赋值的语 ...
- C# 操作word 模板 值 替换
1.引用 aspose.words dll 2.word 使用doc 3.给word 模板中添加要替换位置的 书签 .引用 aspose.words dll .word 使用doc .给word ...
- react native android模拟机调试
模拟机调试首先要确认你的环境变量的path中是不是有adb的路径,adb一般在android的adk目录下的platform-tools下,android目录默认是在c盘user/administra ...