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(翻转整数)的更多相关文章

  1. [LintCode] Reverse Integer 翻转整数

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

  2. [LeetCode] Reverse Integer 翻转整数

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

  3. [LeetCode] 7. Reverse Integer 翻转整数

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

  4. [leetcode]7. Reverse Integer反转整数

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

  5. 7. Reverse Integer[E]整数反转

    题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...

  6. [Leetcode] reverse integer 反转整数

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

  7. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

  8. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  9. lintcode :reverse integer 颠倒整数

    题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...

  10. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

随机推荐

  1. Mac终端运行java程序

    1.编辑源文件HelloWorld.java 2.编译源文件 javac HelloWorld.java 生成HelloWorld.class文件 3.执行java字节码 注意,一定要到源目录下,并且 ...

  2. JavaWeb的编码问题

    http://blog.csdn.net/yuhaiqiang_123/article/details/51811419

  3. Struts2_day02--封装数据到集合里面

    封装数据到集合里面 封装数据到List集合 第一步 在action声明List 第二步 生成list变量的set和get方法 第三步 在表单输入项里面写表达式 封装数据到Map集合 第一步 声明map ...

  4. C语言跳出循环

    使用while或for循环时,如果想提前结束循环(在不满足结束条件的情况下结束循环),可以使用break或continue关键字. break关键字 在<C语言switch语句>一节中,我 ...

  5. wap开发体会<转载>

    前二天因工作需要,上头要求做一个wap版的网站,到网上学习了一天,弄了个beta版出来(http://wap.luckty.com 功能很一般),整理几点经验如下: 1.wap网站用的是wml标识,非 ...

  6. 浏览器出现“ net::ERR_BLOCKED_BY_CLIENT”错误的解决方法

    转载自:http://www.dbmng.com/art-2136.html Failed to load resource: net::ERR_BLOCKED_BY_CLIENT错误报告 错误原因: ...

  7. Bootstrap中关于input里file的样式更改

    给input里file类型加button样式 1.在Bootstrap中input里的file类型样式很不美观,一个按钮加一段文字,还会随浏览器的不同呈现不同的样式,所以开发的时候可以将file的样式 ...

  8. CH5E01 乌龟棋【线性DP】

    5E01 乌龟棋 0x5E「动态规划」练习 描述 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物.乌龟棋的棋盘是一行N 个格子,每个格子上一个分数(非负整数).棋盘第1 格是唯一的起点,第N 格是终点 ...

  9. 修改mysql root的秘密

    修改mysql root的秘密 ');

  10. Servlet------>jsp jstl核心标签库

    这里不需要刻意记,在jar里,c.tld文件都有,可以自己找源码看