题目来源:

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. 自定义标签体、MVC

    自定义标签 文件以tld结尾,放在webinfo中 标签名 引用类 标签体 继承SimpleTagSupport,复写doTag() getContext(); getjspBody()   invo ...

  2. 教你爱上Blocks(闭包)

    传值 Blocks是C语言的扩充功能:带有自动变量(局部变量)的匿名函数.通过Blocks,源代码中就能使用匿名函数,即不带名称的函数.在我们 的工作中,命名占据了很大一部分,函数名,变量名,属性名, ...

  3. CTabCtrl - 如何使用TabCtrl控件

    CTabCtrl - 如何使用TabCtrl控件 建立一个基于对话框的MFC应用程序TabCtrlTest Step 1:在资源视图中,新建对话框资源,资源ID:IDD_TabCtrlExample_ ...

  4. HTML之学习笔记(六)添加链接

    html添加链接所用的标签为<a>标签 语法: 定义:从当前页面,跳转到指定页面或文件的一个标签            <a href="URL">热点文字 ...

  5. svm评价指标公式

    在做svm分类试验时,对于结果的处理,仅用一种指标很难得到正确评估算法的效果.所以,一般要用到precision(精确率),recall(召回率),F-measure.accuracy(准确率)四个指 ...

  6. BZOJ 3870: Our happy ending( 状压dp )

    dp(i, s)表示考虑了前i个数后, 能取到的数的集合为s时的方案数.对于1~min(L, K)枚举更新, 剩下的直接乘就好了. 复杂度O(T*K*2^N)...好像有点大, 但是可以AC.... ...

  7. poj 1966 Cable TV Network 顶点连通度

    题目链接 给一个图, n个点m条边, 求至少去掉多少个点可以使得图不再联通.随便指定一个点为源点, 枚举其他点为汇点的情况, 跑网络流, 求其中最小的情况. 如果最后ans为inf, 说明是一个完全图 ...

  8. MySQL Select 优化

    准备: create table t(x int primary key,y int unique,z int); insert into t(x,y,z) values(1,1,1),(2,2,2) ...

  9. UPPH、UPH

    UPPH=units Per Hour Per Person,单位小时人均产能,是公司作为衡量员工工作绩效的重要指标. UPPH是衡量员工单位时间工作量的一种绩效指标. UPPH计算方式如下: UPP ...

  10. Make Yahoo! Web Service REST Calls With C#

    原文 http://developer.yahoo.com/dotnet/howto-rest_cs.html The .NET Framework provides classes for perf ...