LeetCode 【2】 Reverse Integer --007
六月箴言
万物之中,希望最美;最美之物,永不凋零。—— 斯蒂芬·金
第二周算法记录
007 -- 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.
题干中文版:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
解题思路:如果不考虑数值范围,题目非常好实现,依赖于整数除法和求余即可实现
因为本体是要求必须是32位的有符号整数,需要判断溢出条件:
设当前计算结果为result,下一位为tempInt。
1、大于最大
从result * 10 + tempInt > MAX_VALUE这个溢出条件来看
当出现 result > MAX_VALUE / 10 且 还有tempInt需要添加时,则一定溢出
当出现 result == MAX_VALUE / 10 且 tempInt > 7 时,则一定溢出,7是2^31 - 1的个位数
2、小于最小
从result * 10 + tempInt < MIN_VALUE这个溢出条件来看
当出现 result < MIN_VALUE / 10 且 还有tempInt需要添加 时,则一定溢出
当出现 result == MAX_VALUE / 10 且 tempInt < -8 时,则一定溢出,8是-2^31的个位数
具体实现为:
func reverse(_ x: Int) -> Int {
var originInt = x
guard originInt != 0 else {
print("originInt = 0")
return 0
}
var result = 0
var tempInt = 0
while (originInt != 0) {
tempInt = originInt%10
originInt = originInt/10
if (result > Int32.max/10 || (result == Int32.max / 10 && tempInt > 7)) {
print("最大溢出")
return 0
}
if (result < Int32.min/10 || (result == Int32.min / 10 && tempInt < -8) ){
print("最小溢出")
return 0
}
result = result*10 + tempInt
}
return result
}
1032 / 1032 test cases passed.
Status: Accepted
Runtime: 4 ms
Memory Usage: 20.4 MB

备注:关于时间和空间复杂度还不太会分析
往期Leetcode
有缘看到的亲们:文中若有不对之处,还请劳驾之处,谢谢!
LeetCode 【2】 Reverse Integer --007的更多相关文章
- LeetCode【7】.Reverse Integer--java实现
Reverse Integer 题目要求:给定一个int 类型值,求值的反转,例如以下: Example1: x = 123, return 321 Example2: x = -123, ...
- 【LeetCode算法-7】Reverse Integer
LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...
- 【Leetcode】【Easy】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
- 【LeetCode7】Reverse Integer★
题目描述: 解题思路: 反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法. Java代码: 方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后, ...
- 【Leetcode-easy】Reverse Integer
思路:取绝对值,反转,并判断反转的结果是否大于最大整数,需要注意的细节:判断时需要这样:result > (Integer.MAX_VALUE - v) / 10 否则result * 10 + ...
- LeetCode: 【L4】N-Queens 解题报告
[L4]N-Queens 解题报告 N-Queens Total Accepted: 16418 Total Submissions: 63309 My Submissions The n-queen ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- LeetCode 【1】 Two Sum --001
5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ...
随机推荐
- win10下安装Kafka
去kafka官网(http://kafka.apache.org/downloads.html)下最新包(目前是2.3.0),不分操作系统,直接点二进制压缩包链接跳过去下载即可 -> 解压到你指 ...
- LeetCode_172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...
- LeetCode_119. Pascal's Triangle II
119. Pascal's Triangle II Easy Given a non-negative index k where k ≤ 33, return the kth index row o ...
- Spark2.x学习笔记:5、Spark On YARN模式
https://blog.csdn.net/chengyuqiang/article/details/77864246
- Mac 高效 软件
彻底卸载软件: cleanmymac 软件转移: AppDelete,选择一个软件归档,换台电脑从归档安装 finder类chrme标签页: XtraFinder
- java的mock工具:mockito
https://site.mockito.org https://github.com/mockito/mockito https://github.com/hehonghui/mockito-doc ...
- (1)、JEasyUI 之 Datagrid的Combobox 显示 textField 值的问题
(1).JEasyUI 之datagrid的Combobox显示textField值的问题 官方的datagrid Demo Row Editing in DataGrid 中field 是否如下定义 ...
- OAuth 2.0 授权认证详解
一.认识 OAuth 2.0 1.1 OAuth 2.0 应用场景 OAuth 2.0 标准目前被广泛应用在第三方登录场景中,以下是虚拟出来的角色,阐述 OAuth2 能帮我们干什么,引用阮一峰这篇理 ...
- 物联网防火墙himqtt源码之MQTT协议分析
物联网防火墙himqtt源码之MQTT协议分析 himqtt是首款完整源码的高性能MQTT物联网防火墙 - MQTT Application FireWall,C语言编写,采用epoll模式支持数十万 ...
- Redis List集合 使用
列表类型(list)用于存储一个有序的字符串列表,常用的操作是向队列两端添加元素或者获得列表的某一片段.列表内部使用的是双向链表(double linked list)实现的,所以向列表两端添加元素的 ...