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 (两数之和) 题干英文版: ...
随机推荐
- 开启windows ping端口功能
1.打开控制面板,选择程序 2.选择打开和关闭windows功能 3.将Tenet客户端选项勾上 4.运行栏中输入cmd,进入到命令窗口 5.输入命令 ping ip地址 端口号 ...
- 项目中学习ReactiveCocoa的使用方法
一.注册控制器 控制器上的一个属性 @property (weak, nonatomic) IBOutlet UIBarButtonItem *signInBtn; 在 viewDidLoad 方法中 ...
- mysl创建用户+授权+增、删、改查
1.mysql的root用户无法给普通用户授权问题处理 update mysql.user set Grant_priv='Y' where User='root' and Host='%': flu ...
- react 问题记录三
7.三元表达式 是否显示提交按钮(值运算用一对大括号包起来): {this.state.isTrue ? <FormItem style={{textAlign: 'right',marginT ...
- Flutter 原生TabBar切换标签页示例
效果图: 代码如下: import 'package:flutter/material.dart'; class TabsTestPage extends StatefulWidget { _Tabs ...
- 字符串匹配算法---BF
Brute-Force算法,简称BF算法,是一种简单朴素的模式匹配算法,常用语在一个主串string 内查找一个子串 pattern的出现位置. 核心思想: i 遍历主串string i 每自增一次, ...
- 波特词干(Porter Streamming)提取算法无代码单纯理解
最近写东西提到这个算法,要看一下,结果网上都是直接根据代码解释,对于我这种菜鸟在刚开始看一个算法的时候真心不想直接看代码学.奈何都是各种语言的代码,么得办法.先走了一遍,有了大致的了解,翻译成自己的话 ...
- 微服务发展规划(PS 大概分层未细化到具体系统)
网关层 标准化认证业务前台 前置HTML 基于VUE的浏览器端渲染等特性,完全可以看成一个独立的层业务中台 a. 各个系统的业务功能,以业务功能为单位拆分出的业务服务. b. 配合业务前台所产生的聚合 ...
- web系统整体优化
关于web系统整体优化提速总结 关于web系统整体优化提速总结 一.背景 随着公司业务的拓展,随之而来就是各种系统横向和纵向的增加,PV.UV也都随之增加,原有的系统架构和模式慢慢遇上了瓶颈,需要 ...
- Java线程安全队列Queue实现原理
原文链接:https://www.cnblogs.com/DreamRecorder/p/9223016.html 在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列.J ...