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 ...
随机推荐
- DPDK中使用VFIO的配置
VFIO VFIO是一个可以安全地把设备I/O.中断.DMA等暴露到用户空间(userspace),从而可以在用户空间完成设备驱动的框架.用户空间直接设备访问,虚拟机设备分配可以获得更高的IO性能. ...
- flink统计根据账号每30秒 金额的平均值
package com.zetyun.streaming.flink; import org.apache.flink.api.common.functions.MapFunction;import ...
- 【Javascript-基础-Date】本地时间与UTC(GMT)时间转换
UTC(GMT) 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinat ...
- 获取 iOS APP 内存占用的大小
当我们想去获取 iOS 应用的占用内存时,通常我们能找到的方法是这样的,用 resident_size: #import <mach/mach.h> - (int64_t)memory ...
- MySQL 日常运维业务账号权限的控制
在MySQL数据库日常运维中,对业务子账号的权限的统一控制十分必要. 业务上基本分为读账号和写账号两种账号,所以可以整理为固定的存储过程,让数据库自动生成对应的库的账号,随机密码.以及统一的读权限,写 ...
- react脚手架环境搭建流程
1.安装与配置node.js:1.1软件下载地址:https://nodejs.org/en/,推荐下载.msi文件,其中npm已经集成在了node.js中.1.2 双击下载的.msi文件进行安装,安 ...
- Redis之Redis事务
Redis事务的概念: Redis 事务的本质是一组命令的集合.事务支持一次执行多个命令,一个事务中所有命令都会被序列化.在事务执行过程,会按照顺序串行化执行队列中的命令,其他客户端提交的命令请求不会 ...
- phporjquery生成二维码
一.php生成二维码 下载文章末尾链接中phpcode文件 include "./phpqrcode/qrlib.php"; //QRcode::png('http://www.b ...
- 局域网,Internet,广域网
局域网:覆盖范围小,自己花钱买设备,带宽固定,自己单位维护 网线100米以内 带宽10m 100m 1000m Internet:ISP,自己的机房,对网民提供访问Internet连接 广域网:距离远 ...
- C语言堆排序
堆是一种类似二叉树的数据结构,分为最大堆和最小堆,最大堆得定义是当前节点必须大于左右子节点,堆中所有节点都要符合这个定义.最小堆反之.这一点不同于二叉树排序.假设有数组int a[10] = {90, ...