六月箴言

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

第二周算法记录

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. 学习TypeScript 笔记

    TypeScript 什么是TypeScript TypeScript 是 JavaScript 的一个超集,支持 ECMAScript 6 标准. TypeScript 由微软开发的自由和开源的编程 ...

  2. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  3. tp5.1 where 时间查询

    $where_time = []; if ($_GET['s_time'] && !isset($_GET['e_time'])){ $where_time = ['add_time' ...

  4. C#反射回顾笔记

    一 .反射概述 反射提供描述程序集.模块和类型的对象(Type 类型). 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后调用其方法或访问其字段和属性. 如果代码中 ...

  5. DB2函数简单示例

    DB2中的函数原理同其他编程语言中的函数,均为输入几个参数,同时返回一个值. 下面的例子演示一个寻找某一次考试中成绩最好的学生的姓名. 首先,我们新建一个表SCORE用于表示考试,并插入几条数据: D ...

  6. PowerDesigner的安装和数据库创建

    PowerDesigner安装方法:  http://dev.firnow.com/course/3_program/java/javajs/20090908/174375.html 安装完这2个软件 ...

  7. vim 中与编码有关的选项

    在 Vim 中,有四个与编码有关的选项,它们是:fileencodings.fileencoding.encoding 和 termencoding.在实际使用中,任何一个选项出现错误,都会导致出现乱 ...

  8. linux下的进程间通信之消息队列

    概念: 进程彼此之间可以通过IPC消息进行通信.进程产生的每条消息都被发送到一个IPC消息队列中,这条消息一直存放在队列中,直到另一个进程将其读走为止. 优点:可以通过发送消息来几乎完全避免命名管道的 ...

  9. Egret入门学习日记 --- 第三篇 (书中 3.4 内容)

    第三篇 (书中 3.4 内容) 今天还是要把昨天项目运行后,EXML文件里的界面没有出现的问题解决了才行. 去了群里,没人回.去了官网看文档,看不懂. 不过倒是看到了一个好东西: 还挺便宜啊,一个月要 ...

  10. 解决安装eclipse时出现"Failed to load JNI shared library"

    下午远程帮别人弄了很久的eclipse,安装时老是出现如图的字样 最后在安装配置文件中找到问题所在,这个最新版本的eclipse需要jdk1.8的环境,由于系统的jdk是1.7,版本过低从而导致安装失 ...