六月箴言

万物之中,希望最美;最美之物,永不凋零。—— 斯蒂芬·金

第二周算法记录

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

Two Sum

有缘看到的亲们:文中若有不对之处,还请劳驾之处,谢谢!

LeetCode 【2】 Reverse Integer --007的更多相关文章

  1. LeetCode【7】.Reverse Integer--java实现

    Reverse Integer 题目要求:给定一个int 类型值,求值的反转,例如以下: Example1: x = 123, return 321      Example2: x = -123, ...

  2. 【LeetCode算法-7】Reverse Integer

    LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...

  3. 【Leetcode】【Easy】Reverse Integer

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

  4. 【LeetCode7】Reverse Integer★

    题目描述: 解题思路: 反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法. Java代码: 方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后, ...

  5. 【Leetcode-easy】Reverse Integer

    思路:取绝对值,反转,并判断反转的结果是否大于最大整数,需要注意的细节:判断时需要这样:result > (Integer.MAX_VALUE - v) / 10 否则result * 10 + ...

  6. LeetCode: 【L4】N-Queens 解题报告

    [L4]N-Queens 解题报告 N-Queens Total Accepted: 16418 Total Submissions: 63309 My Submissions The n-queen ...

  7. leetcode第七题Reverse Integer (java)

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

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

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

  9. LeetCode 【1】 Two Sum --001

    5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ...

随机推荐

  1. delphi TClientDatset资料

    第十一章 TClientDataSet 与TTable.TQuery一样,TClientDataSet也是从TDataSet继承下来的,它通常用于多层体系结构的客户端.TClientDataSet最大 ...

  2. linux计划任务以某个用户身份执行

    # Example of job definition: # .---------------- minute (0 - 59) # |  .------------- hour (0 - 23) # ...

  3. Python3之使用枚举类

    当我们需要定义常量时,一个方法是用大写变量通过整数来定义,例如月份 JAN = 1 FEB = 2 MAR = 3 APR=4 May=5 Jun=6 Jul=7 Aug=8 Sep=9 Oct=10 ...

  4. Python3之多重继承

    继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. Animal类层次的设计,假设我们要实现以下4中动物 Dog-狗狗 Bat-蝙蝠 Parrot-鹦鹉 Ostrich-鸵鸟 ...

  5. js前端 多条件筛选查询

    一.前言 在做项目中,遇到多条件筛选案例.实现完成以后,我将我做的代码分享在这里,希望可以帮助到其他朋友. 二.效果截图 三.实现代码 首先我先类型.类别.职位分成三块来处理,如果传到服务器端的话,就 ...

  6. 学习笔记:oracle之win10安装卸载oracle 11gR2步骤及常见问题解决

    1.win10下安装oracle11g 1.1 工具原料 oracle11g安装包(64位) 1.2 步骤方法 1.在Oracle官网下载安装包,下载后,得到的文件如图所示: 2.将两个文件进行解压缩 ...

  7. stub_status监控Nginx使用情况!

    stub_status监控Nginx使用情况! 查看nginx安装的模块! # /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.8.1 bu ...

  8. 电子防抖(EIS)无效的相关修改

    [DESCRIPTION] 电子防抖(EIS)无效的相关修改 [SOLUTION] 电子防抖(EIS)无效,根据不同的版本,可以先查看是否已经做了相关修改.1. MT6580/MT6735平台请参考如 ...

  9. VC++类型转换

    一.其他数据类型转换为字符串 短整型(int) itoa(i,temp,10):///将i转换为字符串放入temp中,最后一个数便是十进制 itoa(i,temp,2):///按二进制方式转换 长整型 ...

  10. python学习-50 pickle模块

    pickle模块 与json方法是一样的 import pickle dic = {'} print(type(dic)) a = pickle.dumps(dic) print(type(a)) f ...