1. Divide Two Integers My Submissions QuestionEditorial Solution

    Total Accepted: 66073 Total Submissions: 421509 Difficulty: Medium

    Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

思路:每次减去b(同时b不断的翻倍,从1,2,2^2…)

为什么这样是可行的,因为如果a=nb+x

那么n作为一个整数一定是可以表示成 2m+2m−1+...+21+20

所以从20,21。。。依次减是可行的

#define IMAX numeric_limits<int>::max()
#define IMIN numeric_limits<int>::min()
class Solution {
public:
int divide(int dividend,int divisor) {
assert(divisor!=0);
int sign = (dividend<0&&divisor>0||dividend>0&&divisor<0)?-1:1;
unsigned int a = dividend<0?-dividend:dividend;
unsigned int b = divisor<0?-divisor:divisor;
unsigned int c = b;
int multi=0;
while(a>=b){
int i=1;
while(a>=b){
a -=b;
multi+=i;
if(b<IMAX>>1){
b=b<<1;
i<<=1;
}
}
b=c;
}
if(sign>0&&multi==IMIN)return IMAX;
else return multi*sign;
}
};

59. Divide Two Integers的更多相关文章

  1. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  2. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  3. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  4. [LintCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. 62. Divide Two Integers

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...

  6. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  7. Java for LeetCode 029 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  8. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  9. LeetCode29 Divide Two Integers

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

随机推荐

  1. Prometheus之告警规则的编写

    Prometheus之告警规则的编写 一.前置知识 二.需求 三.实现步骤 1.编写告警规则 2.修改prometheus.yml执行告警规则的位置 3.配置文件截图 4.页面上看告警数据信息 5.查 ...

  2. qwt使用细节

    在使用QWT进行二维曲线绘制,使用方法如下: class Plot: public QwtPlot { Q_OBJECT -- } 报错:error LNK2001: 无法解析的外部符号"p ...

  3. Linux入门需要搞清楚的思路问题

    很多同学接触linux不多,对linux平台的开发更是一无所知. 而现在的趋势越来越表明,作为一个优秀的软件开发人员,或计算机it行业从业人员,="" 掌握linux是一种很重要的 ...

  4. python fnmatch & glob

    1,转载:Python模块学习 - fnmatch & glob - Dahlhin - 博客园 (cnblogs.com) 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名 ...

  5. [转]DDR3基本概念5 - DDR仿真中出现的Memory overflow错误的处理

    ERROR: Memory overflow. Write to Address 7000fe with data xxxxxxxxxxxxxxxx4634899aabe03499 will be l ...

  6. 王爽汇编第五章,[bx]和loop指令

    目录 王爽汇编第五章,[bx]和loop指令 [bx]和loop指令 例子: 王爽汇编第五章,[bx]和loop指令 [bx]和loop指令 [bx]之前我们介绍寄存器的时候,已经很详细的说明过了,b ...

  7. 『学了就忘』Linux基础命令 — 20、文件操作的相关命令

    目录 1.touch 命令 2.stat命令 3.cat命令 4.more命令 5.less命令 6.head命令 7.tail命令 1.touch 命令 touch命令用于创建空文件或修改文件时间, ...

  8. Linux 启动/关闭 oracle 数据库

    1.启动 1.1 启动监听 :lsnrctl start 1.2 启动数据库:sqlplus /nolog    conn /as sysdba(或者两句一起:sqlplus sys/ as sysd ...

  9. seq2seq之双向解码

    目录 背景介绍 双向解码 基本思路 数学描述 模型实现 训练方案 双向束搜索 代码参考 思考分析 文章小结 在文章<玩转Keras之seq2seq自动生成标题>中我们已经基本探讨过seq2 ...

  10. 什么是SimpleNVR流媒体服务器软件?

    SimpleNVR是一款新兴流媒体服务器应用软件,占用内存少,无插件.跨平台,应用非常广泛,操作简单易上手,同时还支持一键观看,十分便捷.另外,跟其他一般流媒体服务器不同,SimpleNVR支持开发者 ...