1. 题目

2.思路

1. 题目

给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

示例 1:

输入: 123
输出: 321
 示例 2:

输入: -123
输出: -321
示例 3:

输入: 120
输出: 21

2. 思路

   python的坑就在于取余,python的-123 %10 为7 -123 %-10 才为-3,还有就是/10 应该转化为int型,其余的就按照一位一位的弹出,然后判断是否溢出就可以了。

  1. class Solution:
  2. def reverse(self, x: int) -> int:
  3. res = 0
  4. max1 = 2**31 -1
  5. min1 = -2**31
  6. temp1 = x
  7. while temp1 != 0:
  8. if x > 0:
  9. temp2 = temp1%10
  10. if (res*10 > max1) or ((res == int(max1/10))and (temp2 > max1%10) ):
  11. return 0
  12. if x < 0:
  13. temp2 = temp1%-10
  14. if (res*10 < min1) or((res == int(min1/10))and (temp2 < min1%-10) ):
  15. return 0
  16. res = res*10 + temp2
  17. temp1 = int(temp1 /10)
  18. return res

LeetCode 第七题--整数反转的更多相关文章

  1. LeetCode刷题:第七题 整数翻转 第九题 回文数

    第七题题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入 ...

  2. LeetCode刷题--整数反转(简单)

    题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 12 ...

  3. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  4. Leetcode(7)整数反转

    Leetcode(6)Z字形变换 [题目表述]: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 第一次:转字符串处理 执行用时:40 ms: 内存消耗:11.6MB 效果: ...

  5. leetcode第七题--Reverse Integer

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

  6. [LeetCode]7. Reverse Integer整数反转

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

  7. LeetCode第七题

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

  8. leetcode第七题Reverse Integer (java)

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

  9. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

随机推荐

  1. 【ts】 VSCode自动编译TypeScript终端报错

    一.点击终端--运行任务--选择tsc:监视 - tsconfig.json后,终端报出了如下错误:error TS5058: The specified path does not exist 在网 ...

  2. linux 定时器 API

    内核提供给驱动许多函数来声明, 注册, 以及去除内核定时器. 下列的引用展示了基本的 代码块: #include <linux/timer.h> struct timer_list { / ...

  3. linux 在 open 时复制设备

    管理存取控制的另一个技术是创建设备的不同的私有拷贝, 根据打开它的进程. 明显地, 这只当设备没有绑定到一个硬件实体时有可能; scull 是一个这样的"软件"设备 的例子. /d ...

  4. springboot+jpa分页(Pageable+Page)

    Pageable+Page实现分页无需配置,也不需要加入jar包(maven依赖) Controller控制层 package com.gxuwz.late.controller; import co ...

  5. yum安装gcc和gcc-c++

    本次总结参考 博客:http://blog.csdn.net/robertkun/article/details/8466700  ,非常 感谢他的博客,帮我解决了问题. 今天安装gcc-c++时出现 ...

  6. jquery自己写的幻灯片插件,好用不解释

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. HDU 6444 Neko's loop(单调队列)

    Neko has a loop of size nn. The loop has a happy value aiai on the i−th(0≤i≤n−1)i−th(0≤i≤n−1) grid.  ...

  8. [板子]Kruskal

    众所周知求最小生成树的两种方法: 1.Kruskal 2.Prim 这里只挂第一种,因为noip掌握第一种就够了. 两种做法的区别可以参考这个博客:http://blog.csdn.net/molln ...

  9. 从头学pytorch(三) 线性回归

    关于什么是线性回归,不多做介绍了.可以参考我以前的博客https://www.cnblogs.com/sdu20112013/p/10186516.html 实现线性回归 分为以下几个部分: 生成数据 ...

  10. requests库结合selenium库共同完成web自动化和爬虫工作

    我们日常工作中,单纯的HTTP请求,程序员都倾向于使用万能的python Requests库.但大多数场景下,我们的需求页面不是纯静态网页,网页加载过程中伴随有大量的JS文件参与页面的整个渲染过程,且 ...