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. Tomcat服务器使用和debug

    1 在写程序的过程中,遇到了tomcat服务器不能重启的情况,要排查出这个错误并解决它. tomcat就像一棵树,我不能对书上的每片叶子的纹理都熟悉,我只能看到树的轮廓.好像之前出现过这个问题,在se ...

  2. spring框架学习感悟

    学习了一段时间的spring,但是在练习时老是出现bug,一方面,框架封装了很多东西,简化了开发,但是万一出现问题,就很难排查.这说明应该找个慢慢的熟悉它,并且掌握它. 在这个过程中,可能要不断地试错 ...

  3. window.history.go(-1)返回且刷新页面 点击返回上一层

    windows窗口对象(历史)history.go(),history.back(),history.forward(). 因为windows对象引用不是必须的.所以windows.history.g ...

  4. li下的ul----多级列表

    <ul id="ul_Style1"> <li>第1级第1行</li> <li> <ul id="ul_Style2 ...

  5. d4

    # s = '132a4b5c'# s1 = s[0]+s[2]+s[1]# print(s1)#使用while和for循环分别打印字符串s=’asdfer’中每个元素.s = 'fkld'# for ...

  6. 洛谷P1219 八皇后【dfs】

    题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...

  7. js点击按钮保存数据到本地

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. content-type对照表

  9. 使用djiango 创建网站

    如果发现用户登录异常等情况请不要惊慌,换个浏览器就好了,谷歌有这个问题,也困扰我很久. 如果搭建过程,发现新建topic异常等现象,把不要惊慌,可能是你没有进行数据重新清理,请彻底格式化数据库就好了. ...

  10. shell脚本之使用sed和awk进行文本处理

    Shell这种脚本语言特点是,结果松散,场景复杂,针对于一些参数都有特殊意义.针对于大部分工程师而言,使用中的情况是你可能会经常忘记参数或其意义,使你不得不查阅man或网上寻求帮助.此篇文档作用就是在 ...