【Question】

  • Reverse digits of an integer.

  • Example:

x = 123, return 321

x = -123, return -321


【My Solution】


class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
neg = 1
if x < 0:
neg = -1
if x == 0:
return 0
x = abs(x)
r = 0
while x > 0:
r *= 10
r += x % 10
x = x // 10
ans = r * neg
if ans > (1<<31)-1 or ans < -(1<<31):
return 0
return ans
  • 分析

    这题看似简单,其实挖坑很多。需要考虑到以下几点:
  1. 末尾是0的整数翻转后要舍弃0,如10→1,120→21。因此我使用最常规的“对10取余,对10地板除”的方式来翻转整数,此处不宜采用强制类型转换的方式。
  2. 考虑正负两种情况。设一个符号的flag进行调整。
  3. Overflow的问题,不管是翻转前还是翻转后。如果是翻转前就溢出,就要考虑整型值的范围问题,由于python是动态语言,处理变量类型比较灵活,整型值为64位的范围,远远大于题设的32位。再考虑翻转后溢出的情况,利用移位运算求出范围,翻转之后再进行判断。

【LeetCode】#7 Reverse Integer的更多相关文章

  1. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  2. 【LeetCode】7. Reverse Integer 整型数反转

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

  3. 【leetcode】7. Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思 ...

  4. 【LeetCode】007. Reverse Integer

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

  5. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  6. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  7. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  8. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  9. 【一天一道LeetCode】#7. Reverse Integer

    一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...

随机推荐

  1. 让我轻轻的告诉你AliSQLselect语句中in多少个合适

    在以往的分享中,不止一次被开发问: 在MySQL的官方手册上有这么一句话: the optimizer can estimate the row count for each range using ...

  2. 08-FPGA状态机设计实例——小梅哥FPGA设计思想与验证方法视频教程配套文档

    芯航线--普利斯队长精心奉献   实验目的:1.学习状态机的相关概念 2.理解一段式.两段式以及三段式状态机的区别以及优缺点 实验平台:芯航线FPGA核心板 实验原理: 状态机全称是有限状态机(fin ...

  3. Bash:-3次错误输入退出脚本

    Limit_Condition() { let count++ ]];then echo "超过3次机会,自动关停脚本" exit fi Comfirm() { count= wh ...

  4. C语言共用体(Union)

    通过前面的讲解,我们知道结构体(Struct)是一种构造类型或复杂类型,它可以包含多个类型不同的成员.在C语言中,还有另外一种和结构体非常类似的语法,叫做共用体(Union),它的定义格式为: uni ...

  5. cmake 出现问题

    问题: CMake Error at CMakeLists.txt:3 (find_package): By not providing "FindOpenCV.cmake" in ...

  6. java Servlet小结

    1:什么是Servlet? ① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③ 这个在服务器端运行,用以处理客户端的请求 2:Servlet 生命周期 Ser ...

  7. response项目的各个写法

    这个是一个响应式的页面 原文可参照:http://localhost/response/seejs_index.html

  8. AutoMagic

    AutoMagic 是一个基于WebUI的自动化管理平台.为什么叫AutoMagic呢?因为自动化(Automation)在执行起来的时候是一个很神奇的事情,它可以无人值守的模拟人的操作,就像魔术(M ...

  9. Win10 Hyper-V 配置

    Win10 Hyper-V 配置 安装 Hyper 程序和功能 -> 启用和关闭windows -> Hyper-V Hyper-V 虚拟网类型 外部网络 [外网/物理机/虚拟机] 内部网 ...

  10. centos6.6_64位操作系统安装时候出现kernel panic - not syncing: Attempted to kill init 解决办法

    最近在VM上安装centos时候经常被这个问题虐,后来进入单用户模式在   kernel /vmlinuz-XXXXro root=/dev/vogroup00/logvol00 rhgb quie  ...