mycode

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
a = str(x)
if a[0] == '-':
flag = '-'
a = a[1:]
else :
flag = ''
a = a[::-1]
while True:
if a[0] == '':
if len(a) > 1: a = a[1:]
else: return 0
else:
a = int(flag+a)
if a > 2147483647 :
return 0
elif a < -2147483648 :
return 0
else:
return a

注意:用int(x)时,会自动把x前面的0去掉

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
a = str(x)
if a[0] == '-':
flag = '-'
a = a[1:]
else :
flag = ''
a = a[::-1]
#while True:
# if a[0] == '0':
# if len(a) > 1: a = a[1:]
# else: return 0
# else:
a = int(flag+a)
if a > 2147483647 :
return 0
elif a < -2147483648 :
return 0
else:
return a

参考

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
return x if x < 2147483648 and x >= -2147483648 else 0

  

leetcode-easy-string-7 Reverse Integer的更多相关文章

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

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

  2. LeetCode 【2】 Reverse Integer --007

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

  3. leetcode第七题Reverse Integer (java)

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

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

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

  5. leetCode练题——7. Reverse Integer

    1.题目:   7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...

  6. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

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

  7. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  8. leetcode第七题--Reverse Integer

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

  9. 【LeetCode算法-7】Reverse Integer

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

  10. LeetCode记录之7——Reverse Integer

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

随机推荐

  1. python之requests示例

    一) import requests def download(url, num_tries=, user_agent='wswp', proxies=None): ''' 下载指定url并返回网页内 ...

  2. 多线程编程-- part 6 共享锁和ReentrantReadWriteLock

    介绍: ReadWriteLock,顾名思义,是读写锁.它维护了一对相关的锁 — — “读取锁”和“写入锁”,一个用于读取操作,另一个用于写入操作.(1)“读取锁”用于只读操作,它是“共享锁”,能同时 ...

  3. 数据绘图工具之Matplotlib

    一.安装:绘图和可视化 pip install matplotlib 我们已经下好了anaconda 包含了绘图工具包 直接导入即可 import matplotlib.pyplotlib as pl ...

  4. Mac 常用软件

    command+space开启新的一天 窗口管理:Magnet 写代码:Pycharm.vscode.sublime text 虚拟机:VMware Fusion(买不起Parallels Deskt ...

  5. jq自动触发事件

    $('.btn_fath ').trigger("click");$('.btn_fath ').click();

  6. 无Xwindow的linux系统安装VMware Tools

    首先登陆linux 系统,最好是root用户: 然后挂载cdrom: mkdir -p /mnt/cdrom mount -t iso9660 /dev/cdrom /mnt/cdrom 进入cdro ...

  7. python-迭代器与生成器2

    python-迭代器与生成器2 def fib(max): n,a,b=0,0,1 while n<max: #print(b) yield b a,b=b,a+b #t=(b,a+b) 是一个 ...

  8. 华为ensp问题:云映射本地网卡,直连路由器可以ping通,pc却不行?

    拓扑图:cloud 云映射本机物理网卡:192.168.56.1     R1可以Ping通,所有Pc都不行,路由表也存在路由信息,不知道什么问题?

  9. date( ) 日期函数

    date('Y-m-dT2:00')    实际时间为14:00 date('Y-m-d 2:00')     实际时间为2:00 扩展:每天的时间戳秒数为 86400

  10. Let's write a framework.

    Let's write a framework. create a model var model={a:1,b:'b'} let's create a router, router maps url ...