LeetCode 第七题--整数反转
1. 题目
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
2. 思路
python的坑就在于取余,python的-123 %10 为7 -123 %-10 才为-3,还有就是/10 应该转化为int型,其余的就按照一位一位的弹出,然后判断是否溢出就可以了。
class Solution:
def reverse(self, x: int) -> int:
res = 0
max1 = 2**31 -1
min1 = -2**31
temp1 = x
while temp1 != 0:
if x > 0:
temp2 = temp1%10
if (res*10 > max1) or ((res == int(max1/10))and (temp2 > max1%10) ):
return 0
if x < 0:
temp2 = temp1%-10
if (res*10 < min1) or((res == int(min1/10))and (temp2 < min1%-10) ):
return 0
res = res*10 + temp2
temp1 = int(temp1 /10)
return res
LeetCode 第七题--整数反转的更多相关文章
- LeetCode刷题:第七题 整数翻转 第九题 回文数
第七题题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入 ...
- LeetCode刷题--整数反转(简单)
题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 12 ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- Leetcode(7)整数反转
Leetcode(6)Z字形变换 [题目表述]: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 第一次:转字符串处理 执行用时:40 ms: 内存消耗:11.6MB 效果: ...
- leetcode第七题--Reverse Integer
Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode]7. Reverse Integer整数反转
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- LeetCode第七题
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
随机推荐
- UVA 247"Calling Circles"(floyd求传递闭包+SCC)
传送门 题意: 如果两个人相互打电话(直接或间接),则说他们在同一个电话圈里. (a,b) 表示 a 打给 b: 例如,(a,b),(b,c),(c,d),(d,a),则这四个人在同一个电话圈里: 输 ...
- H3C生成树的不足
- linux 重用 short 为 I/O 内存
short 例子模块, 在存取 I/O 端口前介绍的, 也能用来存取 I/O 内存. 为此, 你必须告 诉它使用 I/O 内存在加载时; 还有, 你需要改变基地址来使它指向你的 I/O 区. 例如, ...
- 列表内容自动向上滚动(原生JS)
效果展示 (鼠标移入,滚动停止:鼠标移出,滚动继续) 实现原理 1. html结构:核心是ul > li,ul外层包裹着div.因为想要内容循环滚动无缝衔接,所以在原有ul后面还要有一个一样内容 ...
- 2019-3-8-为何使用-DirectComposition
title author date CreateTime categories 为何使用 DirectComposition lindexi 2019-3-8 8:56:9 +0800 2018-04 ...
- es6笔记 day2---解构赋值
解构赋值 这个知识点非常有用,特别是在做数据交互的时候(Ajax).那么它是怎么使用的呢? 它就是这么使用的↓ let [a,b,c] = [12,5,6]; 这就是解构赋值 注意:左右两边,结构格 ...
- 网上做题随笔--MySql
网上写写题 提高下自己的能力. Mysql平时写的是真的很少,所以训练一下下. 1.查找重复的电子邮箱 https://leetcode-cn.com/problems/duplicate-email ...
- koa2+koa-art-template利用日期管道实现在jat模板中将时间戳转为日期时间
var sp = require("silly-datetime"); var render = require("koa-art-template"); va ...
- java.lang.NoSuchMethodException: com.hgkj.controler.action.UserAction.newsLoginAction()
java.lang.NoSuchMethodException: com.hgkj.controler.action.UserAction.newsLoginAction() 不久前在学习struts ...
- Android studio 使用git仓库记录
studio 绑定git settings --> verson control -->git 在项目文件目录右击打开git bash here操作界面 查看git项目安装位置 找到id_ ...