1、题目
 
7. Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

  1. Input: 123
  2. Output: 321

Example 2:

  1. Input: -123
  2. Output: -321

Example 3:

  1. Input: 120
  2. 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.

 
2、我的解法

  1. # -*- coding: utf-8 -*-
  2. # @Time : 2020/1/26 12:33
  3. # @Author : SmartCat0929
  4. # @Email : 1027699719@qq.com
  5. # @Link : https://github.com/SmartCat0929
  6. # @Site :
  7. # @File : 7. Reverse Integer.py
  8. class Solution:
  9. def reverse(self, x: int) -> int:
  10. if x >= -2147483648 and x <= 2147483647:
  11. if x >= 0:
  12. r = ""
  13. else:
  14. r = "-"
  15. w = abs(x)
  16. y = str(w)
  17. n = len(y)
  18. d = []
  19. for i in (y):
  20. d.append(i)
  21. for j in range(n):
  22. c = d.pop()
  23. r = r + c
  24. v = int(r)
  25. if v >= -2147483648 and v <= 2147483647:
  26. return v
  27. else:
  28. return 0
  29. else:
  30. return 0
 

leetCode练题——7. Reverse Integer的更多相关文章

  1. leetcode算法题笔记|Reverse Integer

    /** * @param {number} x * @return {number} */ var reverse = function(x) { var s; if(x<0){ s=-x; } ...

  2. 乘风破浪:LeetCode真题_013_Roman to Integer

    乘风破浪:LeetCode真题_013_Roman to Integer 一.前言 上一节我们讨论了如何把阿拉伯数字转换成罗马数字,现在我们需要思考一下如何把罗马数字转换成阿拉伯数字,其实我们仔细观擦 ...

  3. 乘风破浪:LeetCode真题_008_String to Integer (atoi)

    乘风破浪:LeetCode真题_008_String to Integer (atoi) 一.前言 将整型转换成字符串,或者将字符串转换成整型,是经常出现的,也是必要的,因此我们需要熟练的掌握,当然也 ...

  4. 【算法】LeetCode算法题-Roman To Integer

    这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...

  5. LeetCode 【2】 Reverse Integer --007

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

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

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

  7. Leetcode算法题 7. Reverse Integer2

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

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

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

  9. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. HTML代码中<%%>、<%=%>

    运行.获取后台代码或值.<%%>之间可以写服务器端代码,比如<%for(var i=0;i<10;i++){//执行循环体}%>又如<%for(var i=0;i& ...

  2. [CF755B] PolandBall and Game - 博弈论

    [CF755B] Description 两个人轮流说单词,每个人只能说自己的的单词库里的单词.被任意方说过的单词不能再说.一个单词只会在某个人的单词库中出现一次,但是可能同时出现在两个人的单词库中. ...

  3. 第一篇,VScode插架以及配置项

      由于在开发的时候我们经常因为ESLint规范把自己搞的头晕眼花,修改起来又很浪费时间.所以我特别做个记录,如下代码可以轻松搞的.   工欲善其事必先利器,如果想要在开发的道路上如履平地必须要有得心 ...

  4. 基于pyqt5的图片素材批量处理工具

    功能 分辨率的批量转换,文件夹递归查找 像素偏移量批量调整,文件夹单层查找 画布的大小的批量进行调整,不进行缩放,文件夹单层查找 界面 通过PyUIC生成的代码 # -*- coding: utf-8 ...

  5. Electron – 项目报错整理(打包~1): WARNING: Make sure that .NET Framework 4.5 or later and Powershell 3 or later are installed, otherwise extracting the Electron zip file will hang.

    WARNING: Make sure that .NET Framework 4.5 or later and Powershell 3 or later are installed, otherwi ...

  6. 2019-08-02 纪中NOIP模拟B组

    T1 [JZOJ1420] 佳肴 题目描述 佳肴就是非常美味的菜的意思,佳肴最关键的是选择好原料. 现在有N种原料,每种原料都有酸度S和苦度B两个属性,当选择多种原料时,总酸度为每种原料的酸度之积,总 ...

  7. CAN总线电平(隐性与显性)

            CAN2.0B规范定义了两种互补的逻辑数值:显性和隐性.同时传送显性和隐性位时,总线呈现显性状态:同时传送显性状态位时,总线呈现显性状态:同时传送隐性状态位时,总线呈现隐性状态.显性数 ...

  8. css美化Div边框的样式实例*(转载)

    css美化Div边框的样式实例   很多时候如果不是用了很多样式,很难把边框修饰得好看,看了一篇博文,觉得真的挺漂亮,也挺好看. 转载的博文地址 将这段美化的css代码 border:1px soli ...

  9. AcWing 899. 编辑距离

    #include <iostream> #include <algorithm> #include <string.h> using namespace std; ...

  10. P1582 倒水(贪心 + lowbbit)

    https://www.luogu.com.cn/problem/P1582 #include <bits/stdc++.h> using namespace std; #define i ...