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. 第五节《Git基本操作》

    我们给原来的数据打一个tag(标签),专业术语叫做“里程碑”,我们先不介绍里程碑的奥秘,只要知道里程碑无非也是一个引用而已. [root@git demo]# pwd/git/my/workspace ...

  2. mongodb 安装使用备记

    # 1.1 安装 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931B ...

  3. wps表格开发C#

    1.需要添加引用etapi.dll,这个dll在你的wps的安装目录下面可以找到. 2.主要的类: Excel.Application:顶层对象 WorkBook:工作簿 WorkSheet:表 Ra ...

  4. 可空类型(Nullable)

    C# 单问号 ? 与 双问号 ?? ? : 单问号用于对 int,double,bool 等无法直接赋值为 null 的数据类型进行 null 的赋值,意思是这个数据类型是 NullAble 类型的. ...

  5. WPF dev 获取gridControl筛选后的数据

    GridControl.DataController.GetAllFilteredAndSortedRows();

  6. web应用 与 http协议

    一.web 应用 Web应用程序是一种可以通过Web访问的应用程序,用户只需要有浏览器即可访问应用程序,不需要再安装其他软件. 应用程序有两种模式C/S.B/S.C/S即客户端—服务端程序这类程序一般 ...

  7. java反射的性能问题

    java反射效率到底如何,花了点时间,做了一个简单的测试.供大家参考. 测试背景: 1. 测试简单Bean(int,Integer,String)的set方法2. loop 1亿次3. 测试代码尽可能 ...

  8. Java 学习 UUID 与 时间格式化、时间操作

    UUID : UUID 是 通用唯一识别码(Universally Unique Identifier)的缩写,是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分.其目的,是让分 ...

  9. python 处理 https链接 socket报错 链接https

    // socket 链接 https 有问题 得去看看ssl文档 用法 import socketimport ssl def https_test(url): proto = "http& ...

  10. python数据分析库pandas

    在我看来,对于Numpy以及Matplotlib,Pandas可以帮助创建一个非常牢固的用于数据挖掘与分析的基础.而Scipy(会在接下来的帖子中提及)当然是另一个主要的也十分出色的科学计算库,但是我 ...