leetcode-algorithms-29 Divide Two Integers

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

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2

Note:

Both dividend and divisor will be 32-bit signed integers.

The divisor will never be 0.

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

解法

class Solution
{
public:
int divide(int dividend, int divisor)
{
if (divisor == 0 || (dividend == INT_MIN && divisor == -1))
return INT_MAX; int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
long long ldvd = labs(dividend);
long long ldvs = labs(divisor);
int res = 0;
while (ldvd >= ldvs)
{
long long temp = ldvs;
long long multiple = 1;
while (ldvd >= (temp << 1))
{
temp <<= 1;
multiple <<= 1;
}
ldvd -= temp;
res += multiple;
}
return sign == 1 ? res : -res;
}
};

链接: leetcode-algorithms 目录

leetcode-algorithms-29 Divide Two Integers的更多相关文章

  1. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  2. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  3. 【LeetCode】29. Divide Two Integers

    题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...

  4. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  5. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

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

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

  7. Java [leetcode 29]Divide Two Integers

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

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

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

  9. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

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

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

随机推荐

  1. PredNet --- Deep Predictive coding networks for video prediction and unsupervised learning --- 论文笔记

    PredNet --- Deep Predictive coding networks for video prediction and unsupervised learning   ICLR 20 ...

  2. java web项目在linux部署、启动,查看系统配置常用的linux命令总结

    本文为博主原创,未经允许不得转载: 以下为在工作中常用的linux命令进行了总结,主要在项目安装及启动,及编辑部署文件时应用较多1.gz文件是一种压缩文件. 以·tar.gz为扩展名的是一种压缩文件, ...

  3. 以结算价交易TAS和以市价交易TAM

    CME Group的合约规格中提到TAS和TAM交易,如:Gold Futures Contract Specs Gold 期货 合约规格 Trading at Settlement (TAS) is ...

  4. Entity Framework Core一键生成实体命令

    打开Vs中工具——Nug包管理器——程序包管理控制台 设置启动项目为存储实体模型的类库或控制台 Scaffold-DbContext  "数据库连接字符串" Microsoft.E ...

  5. python学习 day02打卡

    今天主要学习的内容: 1.while 循环 :  语法: while 条件 : 循环体 #判断条件是否成立.如果成立执行循环体.然后再次判断条件...直到条件不成立石跳出循环 else : 当条件不成 ...

  6. Centos7安装JDK8以及环境配置

    下载,选择centos7 64位版本 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...

  7. 力扣(LeetCode)389. 找不同

    给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...

  8. php 建站 多域名配置 自定义重定向

    1. 申请一个域名 , 当多个域名使用. 比如 申请一个顶级域名为 .com 后缀的一级域名 :mine.com, 一般允许绑定四五个二级域名,比如 www.mine.com  . mine.mine ...

  9. ubuntu16.04 安装docker-ce,docker-compose

    2018.11.14更新 参考https://blog.csdn.net/qq_38199832/article/details/77803645 sudo curl -sSL https://dow ...

  10. 用sql plus时,显示协议适配器错误

    1.在桌面右击我的电脑图标——选择栏中选择管理,点击并进入计算机管理 2.进入计算机管理界面后,点击服务和应用程序,然后在右边栏目选择服务,双击进入服务进程 3.进入服务进程后,鼠标下滑,一直下滑找到 ...