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.

思路:对于负数设置一个标志位,然后将其转化成正整数,再来计算,最后判断大小是否超过了[−231,  231 − 1]这个范围。时间复杂度为O(log 10 x), 空间复杂度为O(1)。

代码


 class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x == : # 为0直接返回
return
res, neg_flag = ,False # 设置结果和负数标志位
if x < :
neg_flag = True
x = abs(x)
while x: # 进行反转
tem = x %
x = x//
res = (res* + tem)
if neg_flag: # 判断舒服标志位,并进行相应的转换
res = -res
if res > pow(,)- or res < -pow(,): # 判断是否超出范围, 直接返回0
return
return res # 返回结果

【LeetCode每天一题】Reverse Integer(反转数字)的更多相关文章

  1. leetcode第七题Reverse Integer (java)

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

  2. leetcode第七题--Reverse Integer

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

  3. LeetCode记录之7——Reverse Integer

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

  4. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  5. 【LeetCode算法-7】Reverse Integer

    LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...

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

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

  7. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  8. Leetcode 题目整理-2 Reverse Integer && String to Integer

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

  9. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

随机推荐

  1. spring的自生一个bug

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. db2pd工具

    内容 概览 简介 使用 db2pd 工具 监控的例子 db2pd 工具 用于监控 DB2 实例和数据库的新的 DB2 UDB 工具 简介 DB2 UDB V8.2 带来了一种新工具称为 db2pd,用 ...

  3. 知识驱动对话-Learning to Select Knowledge for Response Generation in Dialog Systems-阅读笔记

    今日看了一篇文章<Learning to Select Knowledge for Response Generation in Dialog Systems>,以知识信息.对话目标.对话 ...

  4. Away3D引擎学习入门笔记

    (1). 准备工作,一些必须知道的东西 (创建时间:2014-06-05) A.必要的开发语言基础.至少要懂点ActionScript 3.0语法(ActionScript 3.0语法及API参考), ...

  5. 若父设置了overflow: hidden;子如何不受影响

    若父设置了overflow: hidden;子如何不受影响 1.如图: 2.只需要给一个position: absolute;定位 3.相当于重新给页面进行定位,右侧便会有滚动条出现. 4.overf ...

  6. arcgis10.4 server第一次发布地图报错:We were unable to connect to...Error:Proxy server got bad address...

    arcgis 10.4发布地图跟10.2不一样.server url里的http要改为https,否则就会连接不上.

  7. a buzzword to refer to modern Web technologies

    https://html.spec.whatwg.org/multipage/introduction.html#is-this-html5? HTML Living Standard — Last ...

  8. Aspects源码解析(转载)

    文章来源:https://www.jianshu.com/p/2c93446d86bd

  9. Java8 in action

    解决的问题: behavior parameterization,即可以把一段code,逻辑作为参数传入: 这样做的目的,当然为了代码抽象和重用,把变化的逻辑抽象出去: 在java中,如果要实现beh ...

  10. java之反射的基本介绍

    什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能称为Java的反射 ...