[LeetCode][Python]Reverse Integer

时间:2022-08-26 23:31:10
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321
Example2: x = -123, return -321 Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of
1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. ===Comments by Dabay===
先简单是否是负数,用minus来记录。
然后把字符串reverse。
最后检查是否越界。
'''
class Solution:
# @return an integer
def reverse(self, x):
if len(x) <= 1:
return x
x = str(x)
minus = False
if x[0] == '-':
minus = True
x = x[1:]
stack = []
for n in x:
stack.append(n)
ret = ""
while len(stack) > 0:
ret = ret + stack.pop()
ret = int(ret)
if minus:
ret = ret * -1
if ret < -2147483647:
return 0
else:
if ret > 2147483647:
return 0
return ret def main():
s = Solution()
print s.reverse("-2147483648") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]Reverse Integer的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. LeetCode 7 Reverse Integer &amp&semi; int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  3. leetcode:Reverse Integer 及Palindrome Number

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

  4. Leetcode 7&period; Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  5. leetcode:Reverse Integer(一个整数反序输出)

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

  6. 蜗牛慢慢爬 LeetCode 7&period; Reverse Integer &lbrack;Difficulty&colon; Easy&rsqb;

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

  7. 【LeetCode】Reverse Integer&lpar;整数反转&rpar;

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. LeetCode 7&period; Reverse Integer (倒转数字)

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

  9. &lbrack;LeetCode&rsqb; 7&period; Reverse Integer 翻转整数

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

随机推荐

  1. BuildingAssetBundles in 5&period;x

    http://docs.unity3d.com/Manual/BuildingAssetBundles5x.html

  2. Git pull 强制覆盖本地文件

    git fetch --all git reset --hard origin/master git pull

  3. Android -- 滑式抽屉SlidingDrawer&lpar;非原创&rpar;

    SlidingDrawer(滑动式抽屉)隐藏屏外的内容,并允许用户拖拽一个handle以显示隐藏的内容.SlidingDrawer可以在垂直或者水平使用.它由两个子视图组成:一个是用户拖拽的handl ...

  4. APM代码学习笔记1

    libraries目录 传感器 AP_InertialSensor 惯性导航传感器 就是陀螺仪加速计 AP_Baro 气压计 居然支持BMP085 在我印象中APM一直用高端的MS5611 AP_Co ...

  5. Python入门学习(二)

    1 字典 1.1 字典的创建和访问 字典不同于前述的序列类型,它是一种映射类型.它的引入是为了简化定义索引值和元素值存在特定关系的定义和访问问题. 字典的定义形式为:字典变量名 = {key1:val ...

  6. python flask 基础入门

    1. Flask 中的Hello World! 在使用pycharm创建一个Flask项目后,将会默认创建一个flask的app,这里来讲下每行代码表示的意义,代码入下 from flask impo ...

  7. php核心技术与最佳实践(笔记一)

    1.1面向对象的型与本 类是对象的抽象组织,对象是类的具体存在. 1.1.1对象的形 <?php class Person{ public $name; public $gender; publ ...

  8. JS 面向对象 ~ 创建对象的 9 种方式

    一.创建对象的几种方式 1.通过字面量创建 var obj = {}; 这种写法相当于: var obj = new Object(); 缺点:使用同一个接口创建很多单个对象,会产生大量重复代码 2. ...

  9. python常用模块json

    python jons模块 json模块 主要是解决数据格式的转换问题,比如python接收到json对象需要转换为python对象,供python处理,亦或者python数据需要发送到其给其他客户端 ...

  10. 【Codeforces 848C】Goodbye Souvenir

    Codeforces 848 C 题意:给\(n\)个数,\(m\)个询问,每一个询问有以下类型: 1 p x:将第p位改成x. 2 l r:求出\([l,r]\)区间中每一个出现的数的最后一次出现位 ...