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. flask,gunicorn,supervisor,nginx配置服务器接口

    1,申请阿里云主机 2,apt-get update 3,apt-get install pip 4,pip install virtualenv 5,virtualenv venv 6,source ...

  2. pass

    空语句 do nothing 保证格式完整 保证语义完整 以if语句为例,在c或c++/java中: if(true) ; //do nothing else { //do something } 1 ...

  3. [JAVA]JAVA遍历Map的几种方式

    //遍历key for (String key : dic.keySet() ) { System.out.println(key + dic.get(key)); } //遍历values for ...

  4. c++11 function_typetraits备忘

    function traits. 获取函数或成员函数的返回类型,参数类型,参数长度,类类型. 函数参数列表推断基于typelist:http://www.cnblogs.com/flytrace/p/ ...

  5. Innodb中MySQL如何快速删除2T的大表

    转自:http://database.51cto.com/art/201808/582324.htm OK,这里就说了.假设,你有一个表erp,如果你直接进行下面的命令: drop table erp ...

  6. 常用http/https以及socks5代理总结

    代理 格式 # 设置http代理 export http_proxy= # 设置https代理 export HTTPS_PROXY= # 设置ftp代理 export FTP_PROXY= # 同时 ...

  7. 学习笔记之scikit-learn

    scikit-learn: machine learning in Python — scikit-learn 0.20.0 documentation https://scikit-learn.or ...

  8. Kafka和的安装与配置

    本文主要介绍Kafka的安装与配置: 集群规划 datanode1 datanode2 datanode3 zk zk zk kafka kafka kafka kafka jar包下载地址 http ...

  9. Flask-在Flask中跨请求传递数据资源

    利用 Flask的底层Werkzeug是有缓存支持的,不用使用redis等第三方. 原文地址如下: https://blog.csdn.net/yannanxiu/article/details/52 ...

  10. android 开发 View _6_Canvas详解

    牛逼大神的博客地址:http://www.gcssloop.com/customview/Canvas_BasicGraphics 安卓自定义View进阶-Canvas之绘制图形 在上一篇自定义Vie ...