题目来源:

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):

 class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
positive = True
if x < 0:
positive = False
x = abs(x)
ans = 0
while x > 0:
ans = ans * 10 + x % 10
x //= 10
if ans > 2147483647:
return 0
if not positive:
return -1*ans
return ans

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

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

  1. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  2. No.007 Reverse Integer

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

  3. leetcode第七题Reverse Integer (java)

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

  4. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  5. LeetCode--No.007 Reverse Integer

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

  6. Leetcode 题目整理-2 Reverse Integer && String to Integer

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

  7. leetCode练题——7. Reverse Integer

    1.题目:   7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...

  8. 【LeetCode】007. Reverse Integer

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

  9. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  10. [Leetcode]007. Reverse Integer

    public class Solution { public int reverse(int x) { long rev=0; while(x!=0){ rev = rev*10+x%10; x=x/ ...

随机推荐

  1. [置顶] How to compile openjdk 7 in RHEL5

    1. 为什么要编译openjdk的code? 因为从Eclipse调试JDK的代码时,方法中的局部变量不能显示,这样是因为编译JDK时,没有编译成debug版本. 2. RHEL5中自带的开发者JDK ...

  2. 经常使用的DB2命令(2)

    catalog数据库: catalog indirect: db2 catalog database on /db2sys[dir_name] catalog remote:    db2 catal ...

  3. Android采用HttpClient下载图片

    在上一章中谈到Android采用HttpURLConnection下载图片,本章使用HttpClient下载图片 HttpURLConnection与HttpClient的差别: HttpClient ...

  4. asp.net MVC Razor 语法(2)

    变量是用于存储数据的命名实体. 变量 变量用于存储数据. 变量名必须以字母字符开头,不能包含空格和保留字符. 变量可以是某个具体的类型,指示其所存储的数据类型.字符串变量存储字符串值 ("W ...

  5. jquery prop()方法 解决全选 不全选 反选 问题 解决执行一次不不能再执行问题

    //1.如果通过prop()函数更改<input>和<button>元素的type属性,在多数浏览器上将会抛出一个错误,因为该属性一般不允许在后期更改.//如果使用prop() ...

  6. PHP去除Notice警告提示

    最近刚接触PHP,开发过程中可能会遇到Notice: Use of undefined ……这样的警告提示,可能是代码写的不太规范, 有两种解决途径:关闭 PHP 提示的方法, 搜索php.ini:e ...

  7. MATLAB中digits和vpa

    digits: DIGITS Set variable precision digits. Digits determines the accuracy of variable precision n ...

  8. iOS App集成Apple Pay教程(附示例代码)

    苹果在本周一发布了iOS 8.1版本,并正式开放了Apple Pay支付系统.Apple Pay是一个基于NFC的支付系统,不久将被数以万计的线下零售商店予以支持.即便这项科技并不是彻底的突破性进展, ...

  9. Nancy 搭建

    Nancy 框架 1.是一个轻量级用于构建http相应的web框架: 2.与mvc类似,有自己的路由机制: 3.可以处理 DELETE ,   GET ,   HEAD ,   OPTIONS ,   ...

  10. div模拟滚动条

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="html.aspx.cs&q ...