[LeetCode]题解(python):007-Reverse Integer

题目来源:

https://leetcode.com/problems/reverse-integer/


题意分析:

这道题目很简单,就是将一个数反转,123变321,-123变321.


题目思路:

这题目很简单,先把数字求绝对值x,然后x%10取最后一位,然后ans = ans*10 + x%10,加上最后一位。然后x去掉最后一位。知道x = 0.要注意的时候,超出32位int类型的时候设置等于0就行了。


代码(python):

 1 class Solution(object):
 2     def reverse(self, x):
 3         """
 4         :type x: int
 5         :rtype: int
 6         """
 7         positive = True
 8         if x < 0:
 9             positive = False
10         x = abs(x)
11         ans = 0
12         while x > 0:
13             ans = ans * 10 + x % 10
14             x //= 10
15         if ans > 2147483647:
16             return 0
17         if not positive:
18             return -1*ans
19         return ans



转载请注明出处:http://www.cnblogs.com/chruny/p/4798828.html

时间: 2024-08-07 00:16:44

[LeetCode]题解(python):007-Reverse Integer的相关文章

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

LeetCode-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails

LeetCode-Algorithms #007 Reverse Integer 给定一个32位整数, 将其各位反转并返回, 如果结果超出取值范围就返回0 1 class Solution { 2 public int reverse(int x) { 3 //对原数取绝对值 4 int y = Math.abs(x); 5 //将原数转换为字符串 6 String s1 = Integer.toString(y); 7 //将字符串转换为字符数组 8 char[] arr = s1.toCha

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

LeetCode之“数学”:Reverse Integer &amp;&amp; Reverse Bits

1. 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

Leetcode 题目整理-2 Reverse Integer &amp;&amp; String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. 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

LeetCode:Palindrome Number,Reverse Integer

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

Java for LeetCode 007 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字翻转并不难,可以转成String类型翻转,也可以逐位翻转,本题涉及到的主要是边界和溢出问题,使用Long或者BigInteger即可解决. 题目不难: JAVA实现如下: public class Solution { static public int reverse(int x) { if(x=

LeetCode 007 Reverse Integer - Java

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. 定位:简单题 将输入的数反转输出,注意的是负数符号保持在最前,反转后的

leetcode第七题--Reverse Integer

Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 终于什么都没参考就一次Accept了.可能是这题比较简单,同时自己也进步了一点点,leetcode就是这样给我们增加信心的吧. 我是这样想的,利用除10和模10两个操作,将每个数字分离在vec中,然后相应的乘以10的倍数输出就行.如果这个数在-10与10之间直接返回就好. 代码如下: class S

007. Reverse Integer

题目链接:https://leetcode.com/problems/reverse-integer/description/ 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 dea