博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LintCode 413. 反转整数
阅读量:4571 次
发布时间:2019-06-08

本文共 649 字,大约阅读时间需要 2 分钟。

LintCode 413. 反转整数

  • LintCode
  • 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。

样例

  • 给定x = 123,返回321
  • 给定x = -123,返回-321

Java 代码

public class Solution {    /**     * @param n: the integer to be reversed     * @return: the reversed integer     */    public int reverseInteger(int n) {        // write your code here        boolean negative = n < 0;        if (negative) n = -n;        long r = 0;        while (n > 0) {            r = r * 10 + n % 10;            n = n / 10;        }        if (negative) r = -r;        if (r > Integer.MAX_VALUE || r < Integer.MIN_VALUE) return 0;        return (int)r;    }}

转载于:https://www.cnblogs.com/hglibin/p/8979248.html

你可能感兴趣的文章
TC1570 DesertWind
查看>>
CF277D Google Code Jam
查看>>
(七)unittest单元测试框架
查看>>
(八) 自动化测试的实例(以浏览器为例)
查看>>
js获取单选框和复选框的值并判断值存在后允许转跳
查看>>
任务一:零基础HTML编码
查看>>
C#类和结构以及堆和栈大烩菜(本来就迷,那就让暴风来的更猛烈吧!)
查看>>
94. Binary Tree Inorder Traversal
查看>>
MongoDB安装及多实例启动
查看>>
[css]我要用css画幅画(三)
查看>>
eletron打包
查看>>
numpy
查看>>
连接池
查看>>
使用易语言COM对象取文件版本
查看>>
2.16.10.init进程详解1
查看>>
centos7 install idea and x-windows
查看>>
Spring Boot + Spring Cloud 构建微服务系统(九):配置中心(Spring Cloud Config)
查看>>
【转】LINQ to SQL语句(1)之Where
查看>>
《基于MVC的javascript web富应用开发》中的一些函数
查看>>
0014---简单的计算
查看>>