Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution 1: comparison to handle overflow

class Solution {
public int reverse(int x) {
int res = 0;
while (x != 0) {
int cur = res;
res = 10 * res + x % 10;
if (cur != res / 10) {
return 0;
}
x /= 10;
}
return res;
}
}

Solution 2: use Long to handle overflow

class Solution {
public int reverse(int x) {
long res = 0;
while (x != 0) {
res = 10 * res + x % 10;
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {
return 0;
}
x /= 10;
}
return (int)res;
}
}

[LC] 7. Reverse Integer的更多相关文章

  1. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

  2. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  3. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  4. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  5. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  6. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  7. Reverse Integer 2015年6月23日

    题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...

  8. Reverse Integer - 反转一个int,溢出时返回0

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  9. LeetCode之Easy篇 ——(7)Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...

随机推荐

  1. 基于python的arcgis底图添加(转)

    本文翻译自:Qingkai‘s Blog 当使用python的Basemap库绘制地图时,选择一个漂亮的底图会为图片增色不少,但是使用map.bluemarble().map.etopo()或者map ...

  2. vim 马哥

    VIM编辑器 编辑模式   默认 输入模式   i 末行模式   : vim +# file   #打开文件后直接跳到第#行 vim + file   直接跳到尾行 vim  +/关键字    跳转到 ...

  3. Spring Cloud Alibaba 教程 | Nacos(二)

    源码解析客户端注册过程 nacos作为注册中心,包含了nacos服务端(注册中心服务)和nacos客户端,nacos注册中心服务上面一讲已经介绍过了它是一个用Java语言编写开源web项目,并且拥有自 ...

  4. Println(Object)小贴士

    println public void println(Object x) 打印 Object,然后终止该行.此方法首先调用 String.valueOf(x) 获取打印对象的字符串值,然后的行为如同 ...

  5. 17.3.10--->关于数值溢出问题

    取值范围: short.int.long 占用的字节数不同,所能表示的数值范围也不同.以32位平台为例,下面是它们的取值范围: 数据类型                             所占字 ...

  6. Spring MVC中防止csrf攻击

    Spring MVC中防止csrf攻击的拦截器示例 https://blog.csdn.net/qq_40754259/article/details/80510088 Spring MVC中的CSR ...

  7. Java web之javascript(2020.1.6)

    1.js输出: windows.alert()---警告框 document.write()---写到html文档中 innerHTML---写到HTML元素 console.log()---写到浏览 ...

  8. 漫谈设计模式(三):桥接(Bridge)模式 —— 将类功能、结构两层次分离

    1.前言 类主要有两个层次,一个是功能层次,另一个是实现层次. 功能层次,一般应用于当前类不能满足多样化的业务需求,让子类去继承(具体)父类,添加加一些父类中没有的功能(一般是增加新的方法),这就属于 ...

  9. fread 和fgets 函数的使用

    两个函数都是对FILE *fp 文件进行读取信息,fgets是每次读取一行,fread是一下子读完所有的文件内容. //一.fread的使用 FILE *fp; int nread; ] fp = f ...

  10. tensorflow2使用中的一些问题

    from tensorflow import keras import tensorflow as tf import numpy as np print(tf.__name__,tf.__versi ...