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. [ajaxupload] - 上传文件同时附件参数值

    $.ajax({ url: '/excel/importExcel?instance='+"品种1", type: 'POST', data: formData, 上面前端通过?拼 ...

  2. Unity3D学习笔记(三十三):矩阵

    矩阵 矩阵就是一行和列组织起来的矩形数字块. 矩阵可以理解为是向量的数组.   矩阵的维度和记法 矩阵的维度是包含多少行多少列!例如1行2列的矩阵 记法:矩阵m中,对于第1行第2列的元素,我们记为m1 ...

  3. pgAdmin的数据导入之CSV

    在向数据库批量导入数据时,可以参考此过程 1.对于现有的Excel文件,首先应另存为 .csv文件,记住分割符(逗号分割),在后边导入用到. 2.用记事本打开保存后的csv文件,以utf-8格式另存为 ...

  4. XMatch: 您的部门管理助手

    本博客为XMatch项目宣传博客. XMatch: 您的部门管理助手 目录 一.产品概述 二.产品功能 三.产品的创新特色 四.推广方案 五.宣传图 一.产品概述 当前社团的各方面管理工作主要都由手工 ...

  5. 2、corosync集群初步

    配置高可用集群 配置环境:两台centos7 192.168.184.141  192.168.184.142 corosync v2 + pacemaker corosync v2:vote sys ...

  6. HDU 1247 Hat’s Words(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

  7. shp与json互转(转载)

    转自:http://blog.sina.com.cn/s/blog_673c98be0102v78i.html 对于搞GIS开发的,这2种数据格式太重要了. 一.shp转json 这个要容易些,方法也 ...

  8. linux_nmon监控方法

    一.介绍 Nmon 工具是 IBM 提供的免费的在AIX与各种Linux操作系统上广泛使用的监控与分析工具.该工具可将服务器的系统资源耗用情况收集起来并输出一个特定的文件,并可利用 excel 分析工 ...

  9. 抗性基因数据库CARD介绍

    随着抗生素药物的发现及使用,越来越多的耐药菌株由此产生.而耐药菌株的发展则会增加疾病治疗的难度和成本,因此耐药微生物的研究则显得尤为重要.目前,通过对耐药基因的鉴定挖掘能够一定程度上帮助我们揭开耐药机 ...

  10. Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...