7. Reverse Integer 翻转整型

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

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.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

  1. class Solution(object):
  2. def reverse(self, x):
  3. """
  4. :type x: int
  5. :rtype: int
  6. """
  7. b = (x>0) - (x<0)
  8. num = b * int(str(abs(x))[::-1])
  9. if num.bit_length() < 32:
  10. return num
  11. else:
  12. return 0

来自为知笔记(Wiz)

时间: 2024-12-17 06:15:20

7. Reverse Integer 翻转整型的相关文章

LeetCode:Reverse Integer - 翻转数字

1.题目名称 Reverse Integer(翻转数字) 2.题目地址 https://leetcode.com/problems/reverse-integer/ 3.题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形成一个新数字 例如:x = 123, return 321:x = -123, return -321 4.一个有瑕疵的方法(不能AC) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

leetcode_26题——Reverse Integer(int型的表示范围)

Reverse Integer Total Accepted: 73207 Total Submissions: 281775My Submissions Question Solution Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Hide Tags Math Have you met this questi

[LintCode] Reverse Integer 翻转整数

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). Have you met this question in a real interview? Example Given x = 123, return 321 Given x = -123, return -321 LeetCode上的原题,请参见我之前的博客Reverse Integer.

[LeetCode] Reverse Integer 翻转整数

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throu

Integer.valueOf(String)方法字符串转整型- 你肯定不知道的疑惑!

有个仁兄在 StackOverflow 上发起了一个问题,是这么问的: " 我被下面的代码搞晕了,为什么它们会返回不同的值?" 1 2 3 System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));

[LeetCode] 190. Reverse Bits 翻转二进制位

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up:If this function

Php数据类型之整型详解

php中支持的数据类型 在php中主要支持8种数据类型.和3中伪类型的一个形式.8种数据类型分为以下三3大类,第一个就是我们的标量类型,标量类型它只能存储单一数据,那第二大类就是我们的复合类型,第三个呢就是特殊类型,分为这三大类,那首先我们来一类一类的来看. 在第一类中,标量类型中又分为以下四个小类,第一个就是我们的整形,那整型呢你可以通过int代表整型,或者是integer代表整型.第二个就是我们的浮点类型,可以通过float或者是double双精度浮点或者是real实数代表浮点.第三个就是我

LeetCode题解 #7 Reverse Integer

上一次做完一个中等题觉得还挺简单,做一下简单题看看. 题目大意:给定一个整型(即int),将它的数位反过来,如321变为123,-321变为-123. 做了很久,因为简单题耗时的地方在于恶心!! 最后才看见溢出的时候应该返回0!! 一点都不难,特别是对于java. 我的思路: 1.将读入的int型包装成Integer,然后在转换成String.(看看前面有没有+-号) 2.将String 倒置,再转换为long.(一定要是long,否则无法判断溢出) 3.如果没溢出,再将long转换为int.

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA