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. Seata整合SpringBoot和Mybatis

    Seata整合SpringBoot和Mybatis 一.背景 二.实现功能 三.每个服务使用到的技术 1.账户服务 2.订单服务 四.服务实现 1.账户服务实现 1.引入jar包 2.项目配置 3.建 ...

  2. pyqgis环境配置

    配置pyqgis开发环境时,很多网上教程写的非常繁琐,这里仅仅找了一个最简单的配置方法,使用pycharm的IDE,安装QGIS软件后,在pycharm的ProjectInterpreter里面填写Q ...

  3. 【做题记录】[NOIP2016 普及组] 魔法阵

    P2119 魔法阵 2016年普及组T4 题意: 给定一系列元素 \(\{X_i\}\) ,求满足以下不等式的每一个元素作为 \(a,b,c,d\) 的出现次数 . \[\begin{cases}X_ ...

  4. (转载)linux chmod命令用法

    chmod----改变一个或多个文件的存取模式(mode) chmod [options] mode files   只能文件属主或特权用户才能使用该功能来改变文件存取模式.mode可以是数字形式(八 ...

  5. PHP笔记1__基础知识

    客户端: 美妙的网页组成(都是由浏览器解释): 1.HTML 2.CSS--给HTML化妆 3.客户端脚本编程语言(JavaScript等)--特效  服务器端: 1.Web服务器Apache/Ngi ...

  6. openstack 后期维护(四)--- 删除僵尸卷

    前言: 在长时间使用openstack之后,删除虚机后,经常会有因这样那样的问题,导致卷处于僵尸状态,无法删除! 状态一: 虚机已近删除,然而卷却挂在到了 None上无法删除 解决办法: 1.# ci ...

  7. native连接远程mysql数据库

    1.环境 CentOS7.mysqld 8.0.19 2.登录数据库 #mysql -u root -p 2.修改root登录地址为%(任何IP) mysql> update user set ...

  8. node 读取文件内容并响应

    node 读取文件内容并响应 const http = require('http'); const fs = require('fs') //创建 Server const server = htt ...

  9. 亚马逊开发者用户授权 AWS

    在开发之前最好的方法是先拿到官网的API文档简单的预览一遍 这里有个中文文档:AWS 开发中文文档 需要准备: 注册成为开发者 创建 AWS 账户 创建 IAM 用户 创建 IAM 策略 创建 IAM ...

  10. 001.AD域控简介及使用

    一 AD概述 1.1 AD简介 域(Domain)是Windows网络中独立运行的单位,域之间相互访问则需要建立信任关系. 当一个域与其他域建立了信任关系后,2个域之间不但可以按需要相互进行管理,还可 ...