前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode - 007

LeetCode - 007

作者头像
咸鱼学Python
发布2019-06-03 16:10:33
2830
发布2019-06-03 16:10:33
举报
文章被收录于专栏:咸鱼学Python
原题

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

题干

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 dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数

代码语言:javascript
复制
class Solution:    def reverse(self, x: int) -> int:        if x == 0:            return 0        str_x = str(x)        x = ''        if str_x[0] == "-":            x += "-"        x += str_x[::-1].lstrip("0").rstrip("-")        x = int(x)        if -2**31<x<2**31-1:            return x        return 0
代码语言:javascript
复制
class Solution(object):    def reverse(self, x):        """        :type x: int        :rtype: int        """        if x < 0:            return -self.reverse(-x)        res = 0        while x:            res = res * 10 + x % 10            x //= 10        return res if res <= 0x7fffffff else 0
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-05-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 咸鱼学Python 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原题
  • 题干
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档