7. Reverse Integer(翻转整数)
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 hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
用的是Python 转换成str ,然后翻转,需要注意
1.翻转之后如果溢出,输出0
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
neg = x<0
res = int(''.join(str(abs(x))[::-1]))
if res > 2**31:
res = 0
if neg:
res = -res
return res
7. Reverse Integer(翻转整数)的更多相关文章
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [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 ...
- 7. Reverse Integer[E]整数反转
题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- 7. Reverse Integer 反转整数
[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
- leetcode 7 reverse integer 反转整数
描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...
随机推荐
- 【linux】硬盘分区
fdisk -l fdisk /dev/sda d--删除分区 n-新建分区 p--主分区 e--扩展分区 t--改变分区格式 82为swap分区 w--保存退出 http://www.blogjav ...
- js获取表单数据
var modelObj = {}; var modelFieldsArray = $('#AddMusicCategory').serializeArray(); $.each(modelField ...
- JBPM4 经常使用表结构及其说明
首先我想说.这不一篇原创博文. 这里设置成原创.主要是为了分享,由于它对于jbpm的刚開始学习的人,真的值得一看.原作者的博文地址并没有查到,我是在还有一位转载此文的博主那儿获得的.地址在这儿. 本文 ...
- Python 正则表达式规则
正则表达式的一些匹配规则: . :用于匹配任意一个字符,如 a.c 可以匹配 abc .aac .akc 等^ :用于匹配以...开头的字符,如 ^abc 可以匹配 abcde .abcc .abca ...
- 让iOS应用支持不同版本的系统与设备
本文转载至 http://blog.csdn.net/pucker/article/details/11980811 最近一直在做app的iOS 6和7的同时适配工作,所以在此介绍一下系统与设备的兼 ...
- 1855: [Scoi2010]股票交易[单调队列优化DP]
1855: [Scoi2010]股票交易 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1083 Solved: 519[Submit][Status] ...
- 【BZOJ3033】太鼓达人 暴力+欧拉回路
[BZOJ3033]太鼓达人 Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员 ...
- linux下如何启动nginx?
命令: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ........... [root@localhost sbin ...
- LeetCode-Add and Search Word
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [Domino]执行命令load design的时候出现Warning: Cannot locate design template
发现问题 在做Pseudo Test的时候被QA测出了一个问题.在Domino concole打命令“load design”,被Workbench翻译过后的数据库提示了一个警告:Warning: C ...