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

题意:

给定一个10进制整数,翻转它。

Solution1: directly do the simulation.

Two tricky parts to be handled:

(1) overflow :  32-bit signed integer range: [−231,  231 − 1],  whihc means [−2,147,483,648  2,147,483,647].  What if input is 2,147,483,647, after reversing, it will be 7,463,847,412.

(2) negative numbers: for each iteration, we do multiplication or division with a position number -- 10 , which means if sign is '-' , the sign will be kept all the time.

code:

 /*
Time Complexity: O(log(n)) coz we just travese half part of original input
Space Complexity: O(1)
*/
class Solution {
public int reverse(int input) {
long sum = 0;
while(input !=0){
sum = sum*10 + input %10;
input = input /10; if(sum > Integer.MAX_VALUE || sum < Integer.MIN_VALUE){
return 0; // returns 0 when the reversed integer overflows
}
}
return (int)sum;
}
}

[leetcode]7. Reverse Integer反转整数的更多相关文章

  1. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

  2. [Leetcode] reverse integer 反转整数

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

  3. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

  4. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  5. LeetCode 7. Reverse Integer 一个整数倒叙输出

    潜在问题:(1)随着求和可能精度会溢出int 范围,需要使用long 来辅助判断是否溢出,此时返回 0 Assume we are dealing with an environment which ...

  6. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

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

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

  8. leetCode(62)-Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 clic ...

  9. leetcode:Reverse Integer 及Palindrome Number

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

随机推荐

  1. Android 异步下载

    package com.example.demo1; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  2. cannot find package "context"

    导入  github.com/go-sql-driver/mysql 和 database/sql 时,报cannot find package "context"的错误因为go1 ...

  3. Netty学习记录

    一.Netty简介 Netty 是一个基于 JAVA NIO 类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性. Netty 是一个 NIO client-s ...

  4. Spring+SpringMVC+Mybatis+Maven+CXF+WebService整合之服务端

    WebService服务端项目结构: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...

  5. SQL注入检测

    目前只支持单个对象,不支持对象中包含对象 /// <summary> /// 检查SQL中的非法字符 /// </summary> public class SQLInject ...

  6. Java继承(上)

    继承的定义 在人类生活中的学徒关系,父子关系等等都属于继承: 但是在程序中是一种类与类的关系 在程序中可以使用父类的方法,也可以添加自己的方法,但是不能选择的继承,要继承就需要把所有的特点全部继承下来 ...

  7. linux中telnet后退出连接窗口的方法?

    linux中telnet后退出连接窗口 [root@a cron]# telnet www.baidu.com 80Trying 115.239.211.112...Connected to www. ...

  8. Cglib源码分析 invoke和invokeSuper的差别(转)

    原文 https://blog.csdn.net/makecontral/article/details/79593732 Cglib的实例 本文重在源码的分析,Cglib的使用不再复述. //被代理 ...

  9. 输入框UITextField禁止输入空格方法

    方法一:添加代理 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range rep ...

  10. [UE4]记录瞬移目标点

    1.判定射线是否击中一个物体:LineTraceByChannel的Return Value返回值 2.击中的目标点:LineTraceByChannel.Out Hit.Location,如图提示文 ...