LeetCode7-ReverseInteger

LeetCodeeasyOverflow

题目

题目所在链接为 LeetCode-7:ReverseInteger

题目描述

给出一个32位的有符号整数, 反向输出一个整型数字

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

备注

假设我们正在处理一个只能在32位有符号整数范围内存储整数的环境:[−231, 231 − 1]。出于此问题的目的,假设当反向整数溢出时,函数返回0.

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.

例如 $2^{31} -1 = 2,147,483,647$ 如果 设计的返回的值应该是$ 7,463,847,412 $ 溢出 返回0 .

解题

解题思路

  1. 通过余数和除法获取 从尾部 获取数据的每一位,依次成将数据乘10 得到新的数据
  2. 判断符号, 判断是否溢出

时间复杂度: $ O(log(x)) $

空间复杂度: $ O(1) $

具体实现

class Solution {
public:
int reverse(int x) {
if (x == 0)
return 0;
// 输入long long 型 数据 避免溢出
long long sum = 0;
while (x!=0)
{
// 将每一位的数据 升位 加上余数
sum = sum * 10 + x % 10; // 输入数字 降位
x = x / 10; // 判断溢出
if (sum > INT_MAX || sum < INT_MIN)
return 0;
}
return int(sum);
}
};

运行结果

使用C/C++实现

运行结果 2019-03-27

Runtime: 8 ms, faster than 99.38% of C++ online submissions for Reverse Integer.

Memory Usage: 8.2 MB, less than 99.80% of C++ online submissions for Reverse Integer.

改进优化

改进思路

题目底下最好的优化在了空间上的节省, 不用设置 long long 类型的数据, 采用int 型数据, 但是每次判断依次结果

很多数据会导致溢出 直接抛弃掉 可以节省大量的时间

整体流程如下:

  1. 如果结果会溢出, 如果是正值 则 $temp = rev*10+pop $ 则

    $$ rev > \frac{INTMAX}{10} || (rev == \frac{INTMAX}{10} && pop>7) $$

  2. 如果结果会溢出, 如果是负值 则 $temp = rev*10+pop $ 则

    $$ rev <> \frac{INTMIN}{10} || (rev == \frac{INTMIN}{10} && pop<-8) $$

  3. 其中 $ pop = x%10 $

改进的实现

class Solution {
public:
int reverse(int x) {
int rev = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0;
if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
return rev;
}
};

运行结果

Runtime: 4 ms, faster than 100.00% of C++ online submissions for Reverse Integer.

Memory Usage: 8.1 MB, less than 99.80% of C++ online submissions for Reverse Integer.

LeetCode7-ReverseInteger的更多相关文章

  1. LeetCode-7.reverse-integer 【翻转字符串】【数学】

    PS: 第一次写文章好累啊,没想到这么短的文章写完这么累,大家给我点反馈,多给我留言啊.

  2. leetcode — reverse-integer

    /** * Source : https://oj.leetcode.com/problems/reverse-integer/ * * Created by lverpeng on 2017/7/4 ...

  3. 【LeetCode7】Reverse Integer★

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

  4. leetcode7

    public class Solution { public int Reverse(int x) { ; ) { fuhao = -; } try { x = Math.Abs(x); } catc ...

  5. ReverseInteger

    public class ReverseInteger { public static int reverse(int x) { long ret = 0; //如果是个位数,直接返回. if(x/1 ...

  6. Leetcode7 : Reverse Integer 整数反转问题

    问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...

  7. ReverseInteger:实现int整数的反转

    原文链接 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 ...

  8. leetCode:reverseInteger 反向整数 【JAVA实现】

    反向整数 给定一个 32 位有符号整数,将整数中的数字进行反转,如果超出整数的最大或者最小范围返回0 更多文章查看个人博客 个人博客地址:反向整数 方法一 利用StringBuilder的revers ...

  9. LeetCode7:Reverse Integer

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

随机推荐

  1. Java String与char

    1. char类型 + char 类型 = 字符对应的ASCII码值相加(数字): char类型 + String 类型 = 字符对应的ASCII码值相加(数字) + String 类型: Strin ...

  2. C++ 模板和 C# 泛型的区别

    C# Generics and C++ templates are both language features that provide support for parameterized type ...

  3. 从一个慢查询到MySQL字符集编码

    从一个慢查询到MySQL字符集编码 目录 从一个慢查询到MySQL字符集编码 1. 问题起源 2. MySQL字符集和字符集排序规则 2.1 字符集相关概念 2.2 MySQL中的字符集和字符集排序规 ...

  4. 7L-双线链表实现

    链表是基本的数据结构,尤其双向链表在应用中最为常见,LinkedList 就实现了双向链表.今天我们一起手写一个双向链表. 文中涉及的代码可访问 GitHub:https://github.com/U ...

  5. redis 练习 a的数据库数据迁移到b数据库

    思路 1.从a redis中获取所有的key 2.判断key的类型 3.根据key的类型,判断使用的是set/hset类型 4.set到b redis中(写入到b redis中)

  6. python3(二十四) subClas

    """ 继承的多态 """ __author__ = 'shaozhiqi' # -----------------父类---------- ...

  7. python3(六) for while

    # Python的循环有两种,一种是for...in循环,依次把list或tuple中的每个元素迭代出来 names = ['Michael', 'Bob', 'Tracy'] for name in ...

  8. 【Selenium07篇】python+selenium实现Web自动化:PO模型,PageObject模式!

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第七篇博 ...

  9. std::string::substr函数

    string substr (size_t pos = 0, size_t len = npos) const;

  10. std::forward和std::move

    std::forward完美转发 保证参数原来的属性(用在template的参数是引用的时候):左值引用在被转发之后仍然保持左值属性,右值引用在被转发之后依然保持右值属性 void show(int& ...