LeetCode_29. 两数相除Divide Two Integers|商的二进制表示与除数的关系

Problem description

Given two integers: dividend and divisor, return dividend/divisor without using multiplication, division, and mod operator.

The integer division should truncate toward zero, which means just discard fractional part.

Example:

Input: dividend = 10, divisor = -3
Output: -3
Explanation: 10/(-3) = -3.3333..., which should be truncated to -3.

Notes:
The dealing with an environment which could only store integers within 32-bit signed integer range: ([-2^{31},\ 2^{31}-1]).

If the quotient is greater than (2^{31}-1), return (2^{31}-1).

If the quotient is less than (-2^{31}), return (-2^{31}).

Problem analysis

Firstly, we should consider the sign of divisor and dividend, in order to ignore different sign, we can turn them to same sign first. In this question, the integer bound are 32-bit signed integers, so we can only turn the divisor and dividend into negative(otherwise turn (-2^{32}) to positive will overflow).

Now let’s consider the division between two negative number.

We can easily come up with a naive solution: continually minus divisor to dividend till dividend become larger than divisor. In this process, we count how many divisor are subtracted.

But it’s obviously inefficient, we can minus multiple divisor one time! Suppose (m/n=11), (11) is 1011 in binary, which means:

[m/n\Rightarrow m-1\times n\times 2^4-0\times n\times 2^3-1\times n\times 2^2-1\times n\times 2^1 ]

We can find the smallest (n\cdot2^k) such that (n\cdot2^k>m), then minus (m) with (n\cdot2^k), and continually minus (n\cdot2^{k-1},\ n\cdot2^{k-2},\ …) till (n

Original: https://www.cnblogs.com/liuzhch1/p/leetcode29-divide-two-integers.html
Author: liuzhch1
Title: LeetCode_29. 两数相除Divide Two Integers|商的二进制表示与除数的关系

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/620700/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 基于STC51单片机的自动门铃

    基于STC51单片机的自动门铃 设计要求: 利用红外对管检测是否有人进出 在触发红外对管后,使用PWM驱动蜂鸣器,使其发出叮咚叮咚的声音 设计概述: ​ 按照设计要求,为了直观的说…

    数据结构和算法 2023年6月12日
    062
  • 类暗黑破坏神属性系统思路

    序言 暗黑破坏神,流放之路,火炬之光等经典RPG游戏有令人眼花缭乱的角色属性词缀和相应的机制,搭配修改角色属性的装备,技能,Buff等形成很多有趣的流派。此文提供一种类似游戏的角色…

    数据结构和算法 2023年6月8日
    0111
  • D. Same GCDs【CF 1295】

    思路: 我们知道b = [a, a + m], 我们需要满足Gcd(a, m) = Gcd(b, m), 假设g = Gcd(a, m),那么Gcd(a, m) = Gcd(a +…

    数据结构和算法 2023年6月7日
    055
  • 归并排序详解

    一种稳定的排序算法,可以用来求逆序对~ 时间复杂度为 (O)(()(n) (log) (n)()),是一种稳定的排序。 思想 归并排序是一种基于分治思想的排序算法,总的来说有三个步…

    数据结构和算法 2023年6月8日
    074
  • [LC735]行星碰撞

    题目描述 给定一个整数数组 asteroids,表示在同一行的行星。对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗…

    数据结构和算法 2023年6月8日
    083
  • 主席树(区间第k小的数)

    题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板。 1 #include 2 #include 3 #include…

    数据结构和算法 2023年6月7日
    068
  • 分布式ID生成方案

    分布式ID策略 为什么要用分布式ID? 在我们业务数据量不大的时候,单库单表完全可以支撑现有业务,数据再大一点搞个 MySQL 主从同步读写分离也能对付。 但随着数据日渐增长,主从…

    数据结构和算法 2023年6月8日
    0119
  • 常用数据结构之数组

    1.背景 数组是最最基本的数据存储方式 数据结构从根本来看其实就2中数组和链表其他都是在这两种的基础上扩展出来的比如:队列-数组链表都能实现栈-数组链表都能实现哈希表-数组和队列实…

    数据结构和算法 2023年6月12日
    068
  • 数据结构 2

    树上分块。 第一种是随机撒点,在树上随机撒 (\frac{n}{S}) 个点,关键点间期望距离不超过 (S)。优势很明显,当 (S) 取根号的时候,可以处理出所有关键点间的信息,然…

    数据结构和算法 2023年6月12日
    057
  • 友情链接

    posted @2022-02-12 22:04 cjwen6 阅读(13 ) 评论() 编辑 Original: https://www.cnblogs.com/cjwen6/p…

    数据结构和算法 2023年6月12日
    092
  • 深入C++05:运算符重载

    📕运算符重载 1.复数类 运算符重载目的:使对象运算表现得和编译器内置类型一样; 复数类例子 #include using namespace std; class CComple…

    数据结构和算法 2023年6月12日
    085
  • [总结]2022-2-9

    春节后第一场比赛,状态不好。 P1心路历程 看到T1马上想到了勾股定理,但没有很好的做法。T2认为是比较好拿部分分的,T3认为可以转化为树形dp来做,但确实不可以。T4也实在没有什…

    数据结构和算法 2023年6月8日
    087
  • G&GH05 删除文件和.gitignore

    平台: Windows 10 学习笔记 转载请注明出处 欢迎留言 本系列文章是 git & github 的入门教程. 本系列文章优势: 实际开发过程中, 我们经常会希望不…

    数据结构和算法 2023年6月12日
    063
  • LOJ数列分块 9 题解

    (1.) 题意 给定一个长度 (n) 序列,每次查询区间 (l, r) 的众数。 (2.) 思路 如果边界是 ([l,r]),(l) 在第 (a) 块,(r) 在第 (b) 块,可…

    数据结构和算法 2023年6月12日
    0100
  • 线段树的可持久化

    可持久化 能够保留每一个历史版本的数据结构。 那么可持久化线段树就是能保留历史版本的线段树。 原谅我之前一直叫它可持续化线段树 。 一般来说,可持久化线段树本质其实是可持久化数组,…

    数据结构和算法 2023年6月12日
    0103
  • 数论-最小公倍数、整数的唯一分解定理、一次不定方程

    最小公倍数 定义:a1,…an(n≥2),m 为a1,…an的公倍数,[a1,a2,…an]代表为a1,…an的最小公倍数 用数学公…

    数据结构和算法 2023年6月7日
    068
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球