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

If it is overflow, return MAX_INT.

两数相除,不能用*,/,%  那么只能用位移运算了,注意边界条件防止溢出,代码如下:

 class Solution {
public:
int divide(int dividend, int divisor) {
int sign1 = dividend > ? : -;
int sign2 = divisor > ? : -;
long long d1 = abs(static_cast<long long>(dividend));
long long d2 = abs(static_cast<long long>(divisor));
int res = ;
while(d1 >= d2){
long long tmp = d2;
for(int i = ; d1 >= tmp; ++i){
d1 -= tmp;
tmp <<= ;
res += 1LL << i;
}
}
if(sign1 == sign2){
if(res == INT_MIN)
return INT_MAX;
return res;
}else
return res * -;
}
};

PS:这题溢出情况相当恶心。

LeetCode OJ:Divide Two Integers(两数相除)的更多相关文章

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

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  2. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

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

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

  4. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

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

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

  6. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  7. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. LeetCode(29): 两数相除

    Medium! 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor ...

  9. 【LeetCode每天一题】Divide Two Integers(两整数相除)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  10. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

随机推荐

  1. Linux学习笔记(11)linux网络管理与配置之一——配置路由与默认网关,双网卡绑定(5-6)

    Linux学习笔记(11)linux网络管理与配置之一——配置路由与默认网关,双网卡绑定(5-6) 大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配置DNS ...

  2. mysql 练习题(Day44)

    init.sql文件内容 /* 数据导入: Navicat Premium Data Transfer Source Server : localhost Source Server Type : M ...

  3. 转:MVC遇上bootstrap后的ajax表单验证

    使用bootstrap后他由他自带的样式has-error,想要使用它就会比较麻烦,往常使用jqueyr.validate的话只有使用他自己的样式了,而且有模型在使用模型验证更方便点.怎么解决呢? 当 ...

  4. clipboard

    我们在网页上放置一个复制按钮,主要用来方便用户复制链接之类的复杂文本,以往的做法是,通过JS依靠Flash,甚至借助jQuery庞大的js库来实现文本复制到剪贴板的.今天我要给大家介绍的是一款极现代的 ...

  5. Xcode 解决日志打印不全问题

    Xcode 出了8.0后,代码运行日志过长时会出现打印不全的问题. 这可能是Xcode优化的一项,不过这也给开发带来的不必要的麻烦.下面的宏定义可以解决这一问题. #ifdef DEBUG #defi ...

  6. SQL生成一串随机数

    SELECT RIGHT (CONVERT(VARCHAR(20),CONVERT(DECIMAL(20,15),rand())),15) AS c_random_number

  7. poj2250

    #include<string.h> #include<stdio.h> #include<algorithm> #include<vector> #i ...

  8. Spark 实现自定义对象sequenceFile方式存储,读写示例(scala编写)

    package com.fuge.bigdata.datahub.analysis import java.io.{DataInput, DataOutput} import com.fuge.big ...

  9. Mybatis常见问题

    1.#和$的区别和联系 1.1#是占位符,会对Sql进行预编译,相当于?:$是做Sql拼接,有sql注入的隐患 1.2#不需要关注数据类型,Mybatis自动实现类型转换,$必须自己判断数据类型联系 ...

  10. Spring注解(环境)

    以数据库为例: 引入 c3p0数据源maven坐标 数据库驱动 @Configuration @PropertySource("classpath:/db.config.properties ...