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/606696/

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

(0)

大家都在看

  • wget命令8种实用用法

    大家好,我是良许。 wget 是一个可以从网络上下载文件的免费实用程序,它的工作原理是从 Internet 上获取数据,并将其保存到本地文件中或显示在你的终端上。 这实际上也是大家…

    Linux 2023年6月14日
    089
  • 【Javaweb】在项目中添加MyBatis依赖等

    pom.xml 仓库 如果你没有配置阿里云仓库镜像源,可以到这里来找 https://mvnrepository.com/ 如果你配置了阿里云仓库镜像源,可以来这里找 https:…

    Linux 2023年6月14日
    0101
  • Apache JMeter安装及使用

    Apache JMeter是Apache组织开发的基于Java的压力测试工具。用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域。 它可以用于测试静态和…

    Linux 2023年6月8日
    0117
  • ELK收集MySQL慢日志并告警

    采用的是 filebeat采集日志, Redis做日志存储, logstash消费处理日志,将处理过的日志存储到 ES, kibana做日志展示, Elastalert做监控告警长…

    Linux 2023年5月27日
    098
  • 【小记】腾讯云 Linux 虚拟机如何正确修改 hosts 文件

    如果直接修改 /etc/hosts 文件,重启后设置会丢失还原,原因是腾讯云虚拟机默认使用了 Cloud-Init 进行初始化操作。 参见:https://cloud.tencen…

    Linux 2023年6月13日
    092
  • 实验2:Open vSwitch虚拟交换机实践

    实验2:Open vSwitch虚拟交换机实践 一、实验目的 能够对Open vSwitch进行基本操作; 能够通过命令行终端使用OVS命令操作Open vSwitch交换机,管理…

    Linux 2023年6月7日
    0120
  • 爬取与数据存储

    ch5. 数据存储 文件存储 JSON文件存储 关系型数据库存储 Mysql 1. JSON文件存储 1. JSON中的对象和数组 *对象 ​ 格式为 {key1:value1, …

    Linux 2023年6月7日
    078
  • 技术漫谈之——Jectpack Compose

    最近Jetpack Compose发布了Beta版本,抽时间了解了一下Compose带来的改变和其中的一些原理。本文不会讲解具体API,只是比较随意的分享自己的一些疑问以及在探寻答…

    Linux 2023年6月13日
    098
  • Redis的RDB持久化

    posted @2022-02-24 16:11 天宇轩-王 阅读(34 ) 评论() 编辑 Original: https://www.cnblogs.com/dalianpai…

    Linux 2023年5月28日
    094
  • jenkins 设置钉钉机器人+jenkins调用shell脚本使用钉钉机器人自定义发消息并通知指定人

    两种钉钉通知方式,一种是使用安装的钉钉插件来通知,但是这个不好定义通知内容,没办法控制发送条件,只要配置了,不管构建参数(分支,渠道,配置),都会发通知,第二种是使用脚本的方式来通…

    Linux 2023年5月28日
    091
  • 【Linux】socket通信编程

    socket通信 * – socket简介 – socket操作API函数 – 代码实现 socket简介 网络层的”ip地址&#8…

    Linux 2023年6月13日
    093
  • Linux之export命令

    镜像下载、域名解析、时间同步请点击阿里云开源镜像站 export命令用于将shell变量输出为环境变量,或者将shell函数输出为环境变量。 一个变量创建时,它不会自动地为在它之后…

    Linux 2023年5月27日
    0121
  • 项目开发流程与开发模式

    企业项目开发流程 商城 1.1 B2C 直销商城 商家与会员直接交易 ( Business To Customer ) 1.2 B2B 批发商城 商家与商家直接交易 1.3 B2B…

    Linux 2023年6月14日
    0117
  • MS17-010永恒之蓝漏洞利用

    MS17-010永恒之蓝漏洞利用 原理 永恒之蓝漏洞是方程式组织在其漏洞利用框架中一个针对SMB服务进行攻击的漏洞,该漏洞导致攻击者在目标系统上可以执行任意代码。SMB服务在Win…

    Linux 2023年6月14日
    086
  • 每周一个linux命令(ip)

    基础环境 Ip命令介绍 ip命令是一个能够给linux系统设置网络相关信息的命令,通过ip命令可以设置ip地址、子网掩码、网关、路由信息,本节主要讲解ip地址的查看、临时ip地址、…

    Linux 2023年6月8日
    0115
  • 微服务,【容器亚健康状态】问题,研究和解决

    —【前言】— 我问:”程序有『亚健康状态』吗?” 一个正常的人,应该这样回答:”什么?程序,亚健康。。。?你神经病吧?我…

    Linux 2023年6月14日
    088
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球