[LeetCode][Python]Reverse Integer
# -*- 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的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
随机推荐
- Flink Program Guide (7) -- 容错 Fault Tolerance(DataStream API编程指导 -- For Java)
false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-n ...
- [Python] 应用kNN算法预测豆瓣电影用户的性别
应用kNN算法预测豆瓣电影用户的性别 摘要 本文认为不同性别的人偏好的电影类型会有所不同,因此进行了此实验.利用较为活跃的274位豆瓣用户最近观看的100部电影,对其类型进行统计,以得到的37种电影类 ...
- Python命令行参数sys.argv[]
学习C语言的时候就没弄明白命令行参数的用法,在学习Pyton 的时候又遇到了命令行参数,在这里稍微学习了一下,稍微明白了一些在这里做个记录方便后面回顾复习. Sys.argv[]是用来获取命令行参数的 ...
- 安装ngix
第一步:解压源码包 第二步:./configure -->这个时候会提示缺少PCRE 这个时候要安装yum -y install pcre-devel 第三步:./configure --> ...
- [Django 1.5] Django 开发学习资源链接
jQuery : jQuery API introduction:http://api.jquery.com/ jQuery plugins: http://benalman.com/projects ...
- <!DOCTYPE html>
html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5.
- 百度静态资源(JS)公共库
例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js classlist http://apps ...
- mit java open course assignment #4
package come; public class Library { // Add the missing implementation to this class String realLoca ...
- Linux学习之十六、文件的格式化与相关处理
原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0330regularex_4.php 文件的格式化与相关处理 接下来让我们来将文件进行一些简单的编排吧!底下 ...
- Unity大中华区主办 第二届Unity 游戏及应用大赛 实力派精品手游盘点
Unity是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎.包含如今时 ...