zoukankan      html  css  js  c++  java
  • leetcode7 Reverse Integer

     1 """
     2 Given a 32-bit signed integer, reverse digits of an integer.
     3 Example 1:
     4 Input: 123
     5 Output: 321
     6 Example 2:
     7 Input: -123
     8 Output: -321
     9 Example 3:
    10 Input: 120
    11 Output: 21
    12 """
    13 """
    14 为了防止溢出,本题需要先转str。再转int
    15 还要考虑正负
    16 考虑超出范围 返回0
    17 用到了切片str[::-1],逆序
    18 """
    19 class Solution:
    20     def reverse(self, x):
    21         """
    22         :type x: int
    23         :rtype: int
    24         """
    25         x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
    26         return x if x < 2147483648 and x >= -2147483648 else 0
  • 相关阅读:
    java 编码问题
    关于时间
    页面
    关于微信
    01-jQuery的介绍
    15-BOM
    14-定时器
    13-JS中的面向对象
    12-关于DOM操作的相关案例
    购物车练习
  • 原文地址:https://www.cnblogs.com/yawenw/p/12333005.html
Copyright © 2011-2022 走看看