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?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

题目大意就是要翻转一个整数,可是提示了两点须要考虑的问题,一个是末尾是0的。另一个是翻转后会越界,用c++编可能处理起来麻烦一些,然而用Python就很easy。直接将其转成str类型来处理。并调用内置函数reverse,并且转为int时它会自己主动处理首位带的零。很easy。可是我的程序没有处理是否越界的问题。可是依旧AC

class Solution:
# @return an integer
def reverse(self, x):
if x<0:
sign = -1
else:
sign = 1
strx=str(abs(x))
r = strx[::-1]
return sign*int(r)

【LeetCode】【Python题解】Reverse Integer的更多相关文章

  1. C# 写 LeetCode easy #7 Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...

  2. Leetcode练习题 7. Reverse Integer

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

  3. leetcode题解||Reverse Integer 问题

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

  4. LeetCode题解——Reverse Integer

    题目: 数字翻转,即输入123,返回321:输入-123,返回-321. 代码: class Solution { public: int reverse(int x) { , sign = ; ) ...

  5. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  6. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  7. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  8. 【一天一道LeetCode】#7. Reverse Integer

    一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...

  9. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  10. 【LeetCode】007. Reverse Integer

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

随机推荐

  1. [Git] git代码统计

    copy : https://www.cnblogs.com/liyropt/archive/2012/12/31/2841053.html 命令行 查看git上的个人代码量: git log --a ...

  2. json和gson的区别

    json是一种数据格式,便于数据传输.存储.交换gson是一种组件库,可以把java对象数据转换成json数据格式 GSON简单处理JSON json格式经常需要用到,google提供了一个处理jso ...

  3. 深度学习阅读列表 Deep Learning Reading List

    Reading List List of reading lists and survey papers: Books Deep Learning, Yoshua Bengio, Ian Goodfe ...

  4. SXH232摄像头使用示范

    It occurred to me suddenly that I wanted to program the our camera sensor for PC desktop, just like ...

  5. spring中ApplicationContextAware接口使用理解

    一.这个接口有什么用?当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以直 ...

  6. Rabbit MQ UI 重置 用户名 密码

    新增admin用户 rabbitmqctl add_user newadmin s0m3p4ssw0rd rabbitmqctl set_user_tags newadmin administrato ...

  7. MySQL语法大全

    select * from emp; #注释 #--------------------------- #----命令行连接MySql--------- #启动mysql服务器 net start m ...

  8. iOS开发-Interface Builder的前世今生

    Interface Builder,是用于苹果公司Mac OS X操作系统的软件开发程序,Xcode套件的一部分,于1988年创立.它的创造者Jean-Marie Hullot自称是“一个热爱旅行.充 ...

  9. 分享七个绚丽夺目的JQuery导航(还有苹果、猪八戒等),有图有真相

    今天来一起看看几个个人觉得比较好的导航.有好几个导航是仿的,比如仿苹果.仿猪八戒等等,但仿得还都不错.也有不少是基于jQuery的.特别是像我这样的懒人,就可以在这些基础上修修改改作为自己网站项目的导 ...

  10. $.jsonp()的简单使用

    // jsonp 获取 json 数据: $.jsonp({ url: GLOBAL.baseUrl + '/company/mobi_getposter.action', callback: 'ca ...