问题

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123

Output: 321

Example 2:

Input: -123

Output: -321

Example 3:

Input: 120

Output: 21

Note:

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

解题

def reverse(x):
"""
:type x: int
:rtype: int
"""
y = -x if x < 0 else x list = []
last = y
while last > 0:
list.append(last % 10)
last //= 10 sum = 0
length = len(list)
for l in list:
length -= 1
sum += l * 10 ** length result = -sum if x < 0 else sum if result > 2147483647 or result < -2147483648:
return 0 return result

思路

这里利用了不断对10作除法,所获得的余数顺序正好与原数字相反的特性。

LeetCode_7.Reverse Integer的更多相关文章

  1. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

  2. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  3. 65. Reverse Integer && Palindrome Number

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

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

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

  5. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  6. leetcode第七题Reverse Integer (java)

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

  7. Reverse Integer 2015年6月23日

    题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...

  8. Reverse Integer - 反转一个int,溢出时返回0

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

  9. LeetCode之Easy篇 ——(7)Reverse Integer

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

随机推荐

  1. tmux入门

    按键 man tmux和C+b?两个操作可以获得一切. C-b ? 显示快捷键帮助 C-b C-o 调换窗口位置,类似与vim 里的C-w C-b 空格键 采用下一个内置布局 C-b ! 把当前窗口变 ...

  2. 2.Swift快速浏览

    传统认为,在一个新的语言的第一个程序要在屏幕上显示“Hello world!”.在Swift,可以用一行代码来完成: println("Hello, world!") 如果你已经在 ...

  3. Atitit 手机图片备份解决方案attilax总结

    Atitit 手机图片备份解决方案attilax总结 1.1. 图片分类 相机图片与app图片1 1.2. 增量备份,只能使用按照时间法备份..1 1.3. 备份工具选型1 1.4. App图片,只好 ...

  4. git笔记-9-29

    //将工作区的a.txt文件更新到最后一次提交到本地仓库的状态,如果需要将文件回滚到某个特定的版本,将HEAD改成那个commit的id即可 git checkout HEAD a.txt //如果将 ...

  5. 安装astrixx firefox插件

    以上步骤适用于firefox 45.0.1. 将astrixx的firefox插件下载到本地,这个插件的下载地址很难找...全名是astrixx proxy switcher about:config ...

  6. 【XMPP】XMPP类型

    1.ConnectionConfiguration 作为用于与XMPP服务建立连接的配置.它能配置:连接是否使用TLS,SASL加密. 包含内嵌类:ConnectionConfiguration.Se ...

  7. ODbgScript 2.01帮助文档

    -------------------------------ODbgScript original pluginhttp://github.com/odbgscript--------------- ...

  8. Angularjs的那些事 – 视图的生命周期

    Angularjs的最主要的一个应用场景就是单页面应用(SinglePageApplication),但是SPA中会有一个明显的问题,在视图切换的时候,它只会用新视图去替换视图容器内的HTML,但如果 ...

  9. Java知多少(68)面向字符的输出流

    面向字符的输出流都是类 Writer 的子类,其类层次结构如图 10-5 所示. 图10-5 Writer的类层次结构图 表 10-3 列出了 Writer 的主要子类及说明. 表 10-3 Writ ...

  10. Ubuntu下搜狗输入法只显示黑框,不显示输入的汉字选项

    1. cd ~/.config 2.删除三个文件夹: SogouPY, SogouPY.users, sogou-qimpanel 然后重启输入法