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. ELF文件格式理解

    ELF(Executable and Linking Format)是一种对象文件的格式,用于定义不同类型的对象文件(Object files)中都放了什么东西.以及都以什么样的格式去放这些东西.它自 ...

  2. 自己实现一个简化版Mybatis框架

    MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码.本文完成的Mybatis功能比较简单,代码还有许多需要改进的地方,大家可以结合M ...

  3. MySQL 5.7 免安装版 access denied 解决办法

    MySQL 5.7 在Windows 下安装的过程很多人都写过了 但是安装完成后用 root 第一次登录时需要密码 可是我根本就没设密码嘛... 搞了半天最后终于搞定了 在执行 mysqld --in ...

  4. 判断页面是在移动端还是PC端打开的

    $(function () { var curWwwPath = window.document.location.href; var pathName = window.document.locat ...

  5. 架构师成长之路5.7-Saltstack数据系统

    点击架构师成长之路 架构师成长之路5.7-Saltstack数据系统 1. Saltstack的两种数据系统 Grains Pollars 2. Saltstack数据系统---Grains Grai ...

  6. JetBrains IDEA Web开发简单配置

    很早前因为使用了一年的MyEclipse,不想更换其他的IDE工具,是因为各项配置,以及快捷键等.前段时间更换了IDEA工具,初步了解了一些功能,包括快捷,调试,配置,都很优于MyEclipse.但是 ...

  7. 小程序UI设计(3)-符合视觉规范-列表视觉规范

    上一篇我们介绍了字体规范,此贴介绍一下在列表中如何组合使用.下图是微信官方的要求   在工具中通过拖拽组件可以制作出一样的效果拖拽一个WViewRow.这个组件是小程序的view,flex-direc ...

  8. oracle修改某个表的字段顺序

    有时候会发现某个表的列顺序不理想,想修改 -1查询表, select * from AIRWAY_TYPE t --2 查询用户和表名,找到obj#,select object_id from all ...

  9. 【hiho1087】Hamiltonian Cycle

    题目大意:给定一个 N 个点的有向图,计数图上哈密顿回路的条数. 题解:哈密顿回路需要经过除了初始位置,每个点恰好一次.如果已知一条哈密顿回路的方向,那么从这条路上任意一个点出发,得到的都是同样的结果 ...

  10. Spring整合Hibernate实现Spring Data JPA (简单使用)

    直接上代码: pom.xml <!-- hibernate start --> <!-- spring data jpa --> <dependency> < ...